Add an element to the specified location with the add method to the ArrayList "array" used last time.
ArrayList<String> array = new ArrayList<String>();
array.add ("Japanese"); array.add ("English"); array.add ("French"); array.add ("Chinese"); array.add ("German");
System.out.println(array);
[Japanese, English, French, Chinese, German]
Insert the element "Italian" into index 1.
array.add (1, "Italian"); System.out.println(array);
The output in the list is as follows.
[Japanese, Italian, English, French, Chinese, German]
The element is inserted at the specified location, and the elements after that are shifted to the back. By the way, if you try to insert an element by specifying a location that does not exist An exception called "IndexOutOfBoundsException" occurs.
Recommended Posts