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.
Related
This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 1 year ago.
On which basis should we choose to use List, LinkedList, ArrayList in Java?.
Also please explain which one is better to use?.
List is an interface, it should be the type of your variables or the arguments of your methods.
The other two are implementations of this interface.
LinkedList is optimized for inserts and deletes.
ArrayList is optimized for random access.
So, you can choose based on the kind of work you are going to do.
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.
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?
This question already has answers here:
Arrays.asList() doubt?
(8 answers)
Closed 9 years ago.
I was trying to understand Streams in Java8 and intermittently I stumbled upon an interesting thing in the source code of Java8: ArrayList seems to be implemented twice:
The obvious one: java.util.ArrayList
The non-obvious one: java.util.Arrays.ArrayList, which is a private class.
One odd difference is that the normal version is way bigger, and implements List<E>, whereas Arrays.ArrayList does not do so (directly).
Why is it defined twice? And why with the same name?
Actually its there ever since Arrays.asList() introduced. Array's ArrayList is view of the underlying array. If the Array gets changed the ArrayList will get effected and viceversa.
The main benefit, No additional space required because it wont copy the array to a new object (ArrayList), also no additional time to copy the elements.
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).