2:44 pm - Wednesday October 17, 2018

LinkedList example program to Get SubList using sublist()

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

/**
*@author :CandidJava.com
*@descriptions : This program explain the function and use of SubList () method
*/
import java.util.LinkedList;
import java.util.List;

class GetSubListLinkedList {
	public static void main(String[] args) {
		// creating LinkedList Object
		LinkedList lList = new LinkedList();
		lList.add("1");
		lList.add("2");
		lList.add("3");
		lList.add("4");
		lList.add("5");
		System.out.println("LinkedList contains : " + lList);
		// Sublist() method is used to retrieve the elements from the list
		// according to the index positions
		List lst = lList.subList(1, 4);
		System.out.println("Sublist contains : " + lst);
		// Remove() method is used to remove the specified element from the list
		lst.remove(2);
		System.out.println("Sublist now contains : " + lst);
		System.out.println("Original LinkedList now contains : " + lList);
	}
}

output:

D:\CandidJava\Util\LinkedList>javac GetSubListLinkedList.java

Note: GetSubListLinkedList.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\LinkedList>java GetSubListLinkedList

LinkedList contains : [1, 2, 3, 4, 5]

Sublist contains : [2, 3, 4]

Sublist now contains : [2, 3]

Original LinkedList now contains : [1, 2, 3, 5]

 

 

 

 

 

 

 

 

 

BACK

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply