11:33 am - Wednesday May 24, 2017

Java Program is used to return a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive using subList(int fromIndex,int toIndex) method

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

BACK

 

The subList(int fromIndex,int toIndex) method is used to return a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. The returned List is backed by this List, so changes in the returned List are reflected in this List, and vice-versa.

Example Program


import java.util.ArrayList;
import java.util.List;
import java.util.List;

public class SubList {

	public static void main(String[] args) {
		int size;
		List ls;
		ArrayList al = new ArrayList();
		al.add("Rose");
		al.add("India");
		al.add("Network");
		al.add("Dev");
		al.add(".Com");
		al.add("Network");
		if (al.isEmpty() == false) {
			System.out.println("List is:" + al);
			size = al.size();
			System.out.println("Size of List is:" + size);
			ls = al.subList(1, 4);
			System.out.println("SubList is :" + ls);
			System.out.println("Now the List is:" + al);
			al.set(1, "India and");
			al.set(3, "Manuals");
			System.out.println("After modifying in SubList then the newlist:"
					+ al);
			System.out.println("size os the List" + al.size());
		} else

			System.out.println("List is empty");

	}

}


Output:

List is:[Rose, India, Network, Dev, .Com, Network]

Size of List is:6

SubList is :[India, Network, Dev]

Now the List is:[Rose, India, Network, Dev, .Com, Network]

After modifying in SubList then the newlist:[Rose, India and, Network, Manuals, .Com, Network]

size os the List6

Explanation:

Public list SubLis(int fromindex,int toindex)

This method return a list which contains  part of currentlist.

Parameters:

  • fromIndex–This is the low endpoint (inclusive) of the subList.
  • toIndex–This is the high endpoint (exclusive) of the subList.

Returnvalue:

The method call returns a view of the specified range within this List.

Exception:

  • IndexOutOfBoundsException–This is thrown if the endpoint index value is out of range
  • IllegalArgumentException–This is thrown if the endpoint indices are out of order

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.