ArrayList.set()

ArrayList

ArrayList.set(int position , element) Update an element at given position of an ArrayList.


position : Required , Integer , Position where element is to be replaced.
element : (required ) , new element to be placed at given position.
It returns the replaced element of the ArrayList.

Note : first position inside the ArrayList is 0 ( zero ).

Examples with output add()
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("HTML");
System.out.println(languages); //[Java, PHP, HTML]
languages.set(1,"Python");
System.out.println(languages); //[Java, Python, HTML]
PHP is replaced with Python at position number 1 in above code.
We can collect the element as returned value.
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("HTML");
System.out.println(languages); //[Java, PHP, HTML]
String replaced = languages.set(0,"JQuery");
System.out.println(replaced); //Java
System.out.println(languages); //[JQuery, PHP, HTML]
If you try to replace the 4th element ( 3rd position ) then we will get error message (Index 3 out of bounds for length 3) as input position is beyond the last element of the ArrayList.

ArrayList Tutorials



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer