ArrayList.lastIndexOf()

ArrayList

ArrayList.lastIndexOf(element) Position ( Number ) of last matching element present inside the ArrayList.
element : required, the element name.
It returns the position ( number ) of the last occurrence of the input element. -1 is returned if no matching element is found.

Examples with output

add()
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("Python");
System.out.println(languages.lastIndexOf("PHP")); // 1
System.out.println(languages.lastIndexOf("Python")); //2
System.out.println(languages.lastIndexOf("HTML")); // -1
Output ( HTML is not present inside our ArrayList. )
1
2
-1
If multiple elements are present then last matching position is returned.
ArrayList<Integer> marks = new ArrayList<Integer>();
marks.add(55);
marks.add(54);
marks.add(55);
marks.add(54);
System.out.println(marks.lastIndexOf(54)); // 3
To get the first matching position of the element we can use indexOf().

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