ArrayList.subList()

ArrayList

ArrayList.subList(int startIndex, int endIndex) get the view of elements based on input range of the ArrayList.
startIndex : Integer , required, the starting position of the list.
endIndex : integer, required , the ending position of the list.
It returns the view of the elements within the given range.

Examples with output

add()
ArrayList<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("PHP");
languages.add("HTML");
languages.add("Python");
languages.add("PhotoShop");
languages.add("C++");
System.out.println(languages); //[Java, PHP, HTML, Python, PhotoShop,C++]
System.out.println(languages.subList(1,4)); //[PHP, HTML, Python]
If we use startIndex and endIndex same then we will get blank output.
System.out.println(languages.subList(1,1)); //[]
If we use endIndex more less thean startIndex,
System.out.println(languages.subList(2,1)); // error
If you use boyond the last element index then we will get java.lang.IndexOutOfBoundsException
System.out.println(languages.subList(1,10)); // error
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