Sometimes nosotros postulate subList from ArrayList inwards Java. For example, nosotros bring an ArrayList of x objects together with nosotros alone postulate v objects or nosotros postulate an object from index 2 to 6, these are called subList inwards Java. Java collection API provides a method to get SubList from ArrayList. In this Java tutorial, nosotros volition run across an event of getting SubList from ArrayList inwards Java. In this program, nosotros bring an ArrayList which contains iv String objects. Later nosotros telephone yell upwardly ArrayList.subList() method to teach business office of that List.
SubList Example Java
Here is consummate code event of getting SubList inwards Java
Java Program to teach the business office of a List
Further Learning
Java In-Depth: Become a Complete Java Engineer
Write a Java programme to notice Square root of a number
Here is consummate code event of getting SubList inwards Java
Java Program to teach the business office of a List
import java.util.ArrayList;
import java.util.List;
/**
* Java programme to teach SubList or a gain of listing from Array List inwards Java
*
*/
public class GetSubListExample {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
//Add elements to Arraylist
arrayList.add("Java");
arrayList.add("C++");
arrayList.add("PHP");
arrayList.add("Scala");
/*
subList Method returns sublist from listing amongst starting index to goal index-1
*/
List<String> lst = arrayList.subList(1,3);
//display elements of sub list.
System.out.println("Sub listing contains : ");
for(int i=0; i< lst.size() ; i++)
System.out.println(lst.get(i));
//remove i chemical part from sub list
Object obj = lst.remove(0);
System.out.println(obj + " is removed from sub list");
//print master ArrayList
System.out.println("After removing " + obj + " from sub list, master ArrayList contains : ");
for(int i=0; i< arrayList.size() ; i++)
System.out.println(arrayList.get(i));
}
}
import java.util.List;
/**
* Java programme to teach SubList or a gain of listing from Array List inwards Java
*
*/
public class GetSubListExample {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
//Add elements to Arraylist
arrayList.add("Java");
arrayList.add("C++");
arrayList.add("PHP");
arrayList.add("Scala");
/*
subList Method returns sublist from listing amongst starting index to goal index-1
*/
List<String> lst = arrayList.subList(1,3);
//display elements of sub list.
System.out.println("Sub listing contains : ");
for(int i=0; i< lst.size() ; i++)
System.out.println(lst.get(i));
//remove i chemical part from sub list
Object obj = lst.remove(0);
System.out.println(obj + " is removed from sub list");
//print master ArrayList
System.out.println("After removing " + obj + " from sub list, master ArrayList contains : ");
for(int i=0; i< arrayList.size() ; i++)
System.out.println(arrayList.get(i));
}
}
Output:
Sub listing contains :
C++
PHP
C++ is removed from sub list
After removing C++ from sub list, master ArrayList contains :
Java
PHP
Scala
Further Learning
Java In-Depth: Become a Complete Java Engineer
Write a Java programme to notice Square root of a number
No comments:
Post a Comment