This question already has answers here:
Which Java Collection should I use?
(6 answers)
Closed 8 years ago.
I am writing a Java program which creates sequences and save them.
I'm looking for the most fitted data structure to save the sequences.
I don't know in advance the length of the series, or how many series I will have, and the series can be in different length.
what structure should I use?
You can use a List (i.e. ArrayList or LinkedList) of Strings for example. If you want to store more information about the sequence I would recommend to write a class named Sequence with a String and your additional information in it.
The data structure basically depends on the type of data you are going to store, and since you are saying that the length is not known in advance, I think you should have a look at Collections in java and then decide which one to use.
Maybe if you provide us a sample data, we might be able to help you better.
It depends on how you want to access the items (sequentially or randomly) but an ArrayList or a LinkedList could be a good start.
Here's a discussion of both: When to use LinkedList over ArrayList?
Related
This question already has answers here:
How can we ensure the immutability of a ArrayList defined in a Immutable Object?
(3 answers)
Closed 1 year ago.
If I want another class to be able to access the content of an arraylist, for example looping through it to see what's inside without allowing it to actually add or delete any elements, is that possible?
In my mind, I'm thinking there should be some way to send an immutable version of the ArrayList.
If that is possible, can someone show me an example please?
Thank you.
Plenty of ways...
The simplest would be to wrap your ArrayList in an unmodifiable list, using:
Collections.unmodifiableList(yourArrayList)
This returns an unmodifiable view of the list.
Note that this doesn't mean the items themselves cannot be modified.
Other alternatives could be:
Converting the list to an array (with toArray(T[] a), and passing that to Arrays.asList(), which would also send back an unmodifiable list.
If using some other libraries in your code, like Guava, you could invoke ImmutableList.copyOf() or other variations.
I know what arrays are and how to use them. However, I don't know how they are implemented. I was trying to figure out if I can try to implement an array-like data structure using Java but I couldn't.
I've searched online but didn't find anything useful.
Is it even possible to implement an array-like data structure in Java? Is it possible in other languages? if so how (without using arrays of course)?
EDIT: what I want to know is how to implement an array data structure without using arrays?
Arrays are contiguous sections within memory, so to create an array you would need to reserve a chunk of memory which is of size n * sizeof(type), where n is the amount of items you would like to store and the sizeof(type) would return the size, in bytes which the JVM would need to represent that given type.
You would then store a reference (pointer) to the first location of your memory segment, say 0x00, and then you use that as a base to know how much you need to move to access the elements, so a[n] would be equal to doing 0x00 + (n * sizeof(type)).
The problem with trying to implement this in Java is that Java does not allow pointer manipulation, so I do not think that building your own array type would be possible since you cannot go down to that level.
That being said, you should be able to create a linked data structure, where the nth element points to the (n + 1)th element.
Other problems why you should try other languages, such as C# (check unsafe operations), C++ or C:
To my knowledge, Java does not have a sizeof function (see this).
Java does not allow operator overloading. So you cannot define your own indexing operators such as [index]. You would probably need to do something like array.getElementAt(0) to get the first element.
As #ug_ recommended, you could take a look at the Unsafe class. But also as he recommended, I do not think that you should do pointer arithmetic with a language which has pointer abstraction as one of its core ideas.
If what you want is something like this:
MyArray ma = new MyArray(length);
ma[0] = value;
Then you can't do this in Java but you can in other languages. Look for "operator overloading".
I'm wondering if your thinking of a structs, vectors or link lists. These are all similar to arrays but are different.
Structs are not really in java, but you can implement them.
Read up on Structs here:
www.cplusplus.com/doc/tutorial/structures/
An example Structs used in java:
Creating struct like data structure in Java
I think what you are really looking for though are vectors. They are very similar to an array, but their not one.
Vectors info:
www.cplusplus.com/reference/vector/vector/
Array compared to vector:
https://softwareengineering.stackexchange.com/questions/207308/java-why-do-we-call-an-array-a-vector
I recommend a link list. Its kinda the same idea of an array, but without knowing your exact size. It is easier to implement.
Link lists:
en.wikipedia.org/wiki/Linked_list
All these come down to the situation on what need them for. Saying , "what I want to know is how to implement an array data structure without using arrays?" is kinda open ended.
This question already has answers here:
What do < and > mean such as implements Comparable<BigInteger>?
(2 answers)
Closed 9 years ago.
So I see people putting <> after declaring a collection. I know that it is used to specify which data type the collection contains. I haven't seen it used in any other cases so I was just wondering what it is called and if their are any other ways of using this technique? Thanks
Angled brackets/Generics are used for defining the data type that can be stored in the collection. Without generics, you can store entities of type Object, which means anything as all the classes extend from Object. But in business scenario we may not need such a generic collection and would like to avoid putting different kind of objects in a collection. For example if you have a collection of names, you may not want numbers to be stored in such a collection. To restrict this you can define collection with , this will mandate the coder to store only the String type values in the collection.
Java Generics. You call the thing inside the angled brackets a type parameter.
From Java Tutorials: JDK 5.0 introduces several new extensions to the Java programming language. One of these is the introduction of generics.
See http://docs.oracle.com/javase/tutorial/extra/generics/intro.html
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sorting ArrayList of Objects by Object attribute
Basically I have an ArrayList that stores objects, each of those objects has a field that stores an integer. I want to store the objects in my ArrayList by acending order of that integer.
Is there an easy way to do this, I've looked around the Java API doc but can't find anything that looks suitable, sorry if this is trivial but it sounds like it should be simple enough if I can just find the right documentation.
Thanks
Create your custom comparator class implementing java.util.Comparator and use java.util.Collections.sort(list, comparator).
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Java unmodifiable array
Immutable array in Java
How do I make an array read only so that the elements inside it can only be read but cannot be modified,added or deleted. need to do this in JAVA. Please help. I think merely the use of final keyword wont help.Need to do something more than that at the code level. Thanks in advance!
Short answer is you can't -- final will only guarantee you that the reference to the array itself won't be changed. You can do this with a List though, as the Collections class provides a method for creating a List that cannot be modified (Collections.unmodifiableList) -- that is only if you can change your application to use List rather than array.