ArrayList.add()
ArrayList.add(element) Insert element to ArrayList.
boolean : It returns true on successful addition of element at end, otherwise returns false.
Examples with output
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("Python");
languages.add("HTML");
System.out.println(languages); //[Java, PHP, Python, HTML]
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");
languages.add("HTML");
if(languages.add("C++")) {
System.out.println("Successfully added C++");
}else {
System.out.println("Failed to add C++");
}
System.out.println(languages); //[Java, PHP, Python, HTML,C++]
Output is here
Successfully added C++
[Java, PHP, Python, HTML, C++]
« ArrayList Tutorials « addAll()
« Java
This article is written by plus2net.com team.
plus2net.com