This Java program demonstrates how to get a sublist from an ArrayList, display the elements of the sublist, remove an element from the sublist, and print the original ArrayList. Here's how the code works:
Note that the subList() method returns a view of the original ArrayList, which means that any changes made to the sublist are reflected in the original ArrayList, and vice versa.
import java.util.ArrayList; import java.util.List; public class Get_SubList { public static void main(String[] args) { ArrayList<String> col_list = new ArrayList<String>(); col_list.add("Blue"); col_list.add("Green"); col_list.add("Pink"); col_list.add("Black"); col_list.add("Red"); col_list.add("orange"); col_list.add("White"); List<String> cl = col_list.subList(1,5); //display elements of sublist. System.out.println("Display Elements of Sublist Contains... "); for(int i=0; i< cl.size() ; i++) { System.out.println(cl.get(i)); } //remove one element from sub list Object obj = cl.remove(2); System.out.println("Removed from sub list is : "+obj); //print original ArrayList System.out.println("After removing from SubList ArrayList contains.. "); for(int i=0; i< col_list.size() ; i++) { System.out.println(col_list.get(i)); } } }
Display Elements of Sublist Contains... Green Pink Black Red Removed from sub list is : Black After removing from SubList ArrayList contains.. Blue Green Pink Red orange White
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions