【set】 This method is used when rewriting the elements of ArrayList. Enter the location and value you want to change as shown below. Variable name.set (index, value to be rewritten)
In the ArrayList "array" used in Java (add) times Rewrite the element at the location specified by the set method.
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]
Change "Chinese" in Index 3 to "Korean".
array.set (3, "Korean"); System.out.println(array);
The output in the list is as follows.
[Japanese, English, French, Korean, German]
The value at the specified location is rewritten.
Like the add method, if you try to rewrite the value by specifying a location that does not exist An exception called "IndexOutOfBoundsException" occurs.
Recommended Posts