ArrayList.addAll()

ArrayList() add()

ArrayList.addAll(,ListArray) Insert element of ListArray to our ArrayList.
position (optional ) Position inside main ArrayList where the elements will be added, if not given then elements are added at the end.

boolean : It returns true on successful addition of elements, otherwise returns false.

Examples with output
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("Python");
System.out.println(languages); //[Java, PHP, Python]

ArrayList<String> languages2 = new ArrayList<String>();
languages2.add("HTML");
languages2.add("JavaScript");
languages2.add("CSS");
System.out.println(languages2); //[HTML, JavaScript, CSS]

languages.addAll(languages2);
System.out.println(languages); //[Java, PHP, Python, HTML, JavaScript, CSS] 
In above code change this line
languages.addAll(2,languages2);
Now elements will be added at Position 2 of first ArrayList. The last line output will change to this.
[Java, PHP, HTML, JavaScript, CSS, Python]
After the 2nd element ( PHP ) , the elements are added.

Using if condition to check the status of adding element to ArrayList
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("Python");
System.out.println(languages); //[Java, PHP, Python]

ArrayList<String> languages2 = new ArrayList<String>();
languages2.add("HTML");
languages2.add("JavaScript");
languages2.add("CSS");
System.out.println(languages2); //[HTML, JavaScript, CSS]

if(languages.addAll(languages2)) {
System.out.println("Added all elements at end");	
}else {
System.out.println("Failed to add elements");	
}
System.out.println(languages); //[Java, PHP, Python, HTML, JavaScript, CSS] 
Output is here
[Java, PHP, Python]
[HTML, JavaScript, CSS]
Added all elements at end
[Java, PHP, Python, HTML, JavaScript, CSS]
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