How to Create SubList from LinkedList

package JavaLinkedListExample;

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class CreateSubList {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		LinkedList<String> linkedList = new LinkedList<String>();
		String objInput;

		System.out.println("Insert the elements in List(Enter exit to quit)");
		objInput = scanner.next();

		/*
		 * equalsIgnoreCase method compares this String to another String,
		 * ignoring case considerations.
		 */
		while (!objInput.equalsIgnoreCase("exit")) {
			linkedList.add(objInput);
			objInput = scanner.next();
		}
		/**
		 * size method returns the number of elements in this list.
		 */
		System.out.println("Size of the list :" + linkedList.size());
		System.out.println("Enter fromIndex for subList(Only Integer)");
		int fromIndex = scanner.nextInt();

		System.out.println("Enter toIndex for subList(Only Integer)");
		int toIndex = scanner.nextInt();

		List subList = linkedList.subList(fromIndex, toIndex);

		System.out.println("Elements in Sublist");

		for (Object obj : subList)
			System.out.println(obj);

		System.out.println("Size of Populated List " + subList.size());
	}
}

 

By | 2012-09-25T03:40:12+00:00 September 25th, 2012|Code Base|0 Comments

About the Author:

Leave A Comment