ArrayList.remove()

ArrayList

ArrayList.remove(Object o) Removes the first matching value of the object .
Returns true if the element is removed otherwise false.

Examples with output

add()
We will remove one element by using value .
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]

languages.remove("Python");
System.out.println(languages); //[Java, PHP, HTML]
As we get boolean output, we can use if else to show messages.
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]

if(languages.remove("Python")) {
	System.out.println("Python is removed");
}else {
	System.out.println("Python is NOT removed");
}

System.out.println(languages); //[Java, PHP, HTML]
Output
[Java, PHP, Python, HTML]
Python is removed
[Java, PHP, HTML]

Using position of the elements

We can input the postion ( starting from 0 ) of the element to be removed.
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]

languages.remove(1);
System.out.println(languages); //[Java, PHP, HTML]

languages.remove(1);
System.out.println(languages); //[Java, HTML]

Removes the first matching only

Here PHP is removed at first matching only.
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("Python");
languages.add("HTML");
languages.add("PHP");
System.out.println(languages); //[Java, PHP, Python, HTML,PHP]

languages.remove("PHP");
System.out.println(languages); //[Java, Python, HTML, PHP]
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