The addAll method of ArrayList happens to work in Oracle's JDK (and OpenJDK). But it is not guaranteed to. The Javadoc for addAll says:
The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)
The question is actually not as pointless as some of the answers imply. Just "trying it" is not a sufficient test that something works correctly, as this case proves.
In fact, when thinking about whether list.addAll(list) works, you need to ask yourself how it might be implemented. If it was simply iterating over list and adding each element to the end of list, you'd get a ConcurrentModificationException (or else it would go into an infinite loop). That's why the Javadoc doesn't guarantee that such a call works. As it happens, Oracle's implementation copies the passed list to an array first, and then copies each element of the array to the end of the list. This means that calling addAll for a list might not be as efficient as doing it yourself without creating an extra copy.
What is the best way to add items to a list?
Is there a way to add elements to an Array List all at once?
swing - How to append a list to another list in java - Stack Overflow
How to append elements at the end of ArrayList in Java? - Stack Overflow
What is a linked list node in Java?
- The data part that stores the element's value, and
- a reference (or pointer) to the next node in the sequence, facilitating the link in the list.
What is a linked list in Java?
A linked list in Java is a data structure that consists of a sequence of elements, each contained in a node. Nodes are chained together using pointers, with each node pointing to the next node in the sequence. The use of pointers allows for efficient insertion and deletion of elements as it does not require shifting elements, which is required when relying on simple arrays.
What are linked lists good for in Java?
Linked lists in Java (java.util.LinkedList) are useful in scenarios where frequent insertion and deletion of elements is required, as these operations can be performed more efficiently than in arrays, because there's no need to shift elements. Linked lists (java.util.LinkedList) are also useful when the size of the data structure needs to be dynamically allocated.
Videos
The addAll method of ArrayList happens to work in Oracle's JDK (and OpenJDK). But it is not guaranteed to. The Javadoc for addAll says:
The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)
The question is actually not as pointless as some of the answers imply. Just "trying it" is not a sufficient test that something works correctly, as this case proves.
In fact, when thinking about whether list.addAll(list) works, you need to ask yourself how it might be implemented. If it was simply iterating over list and adding each element to the end of list, you'd get a ConcurrentModificationException (or else it would go into an infinite loop). That's why the Javadoc doesn't guarantee that such a call works. As it happens, Oracle's implementation copies the passed list to an array first, and then copies each element of the array to the end of the list. This means that calling addAll for a list might not be as efficient as doing it yourself without creating an extra copy.
The main drawback with myList.add(myList); is that it won't compile, because myList isn't an Integer.
You can, however, use:
myList.addAll(myList);
Ideone demo
or
myList.addAll(new ArrayList<>(myList));
if you want to do it safely.
using stream API
ArrayList<Integer> list= Stream.of(1,2,3).collect(Collectors.toCollection(ArrayList::new));
or without it
ArrayList<Integer> list1=new ArrayList<>(); list1.add(1); list1.add(2); list1.add(3);
For example I can do this with a basic integer array:
int[] test1 = new int[]{2, 1, 5, 1, 3, 2};But with an Array list I have to add them one at a time like this
List<Integer> test1 = new ArrayList<Integer>(); test1.add(2); test1.add(1); test1.add(3);
?
Here is the syntax, along with some other methods you might find useful:
//add to the end of the list
stringList.add(random);
//add to the beginning of the list
stringList.add(0, random);
//replace the element at index 4 with random
stringList.set(4, random);
//remove the element at index 5
stringList.remove(5);
//remove all elements from the list
stringList.clear();
I ran into a similar problem and just passed the end of the array to the ArrayList.add() index param like so:
public class Stack {
private ArrayList<String> stringList = new ArrayList<String>();
RandomStringGenerator rsg = new RandomStringGenerator();
private void push(){
String random = rsg.randomStringGenerator();
stringList.add(stringList.size(), random);
}
}
Thanks for all your help so far on Java. I am moving on to a more complicated problem. In order to solve this, I want to create a list that holds numbers (list of numbers), and then I want to run a function through that list and add each element.
I am very new the lists, so to start off, how can I define a list, and add items the list? If direct questions are not welcomed, feel free to direct me to some resources!
Thank you!