3:59 pm - Saturday February 25, 2017

This Program is used to copying the number of lists using Collections.ncopies() 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

This Program is used to copying the number of lists using Collections.ncopies() Method

Example Program


import java.util.*;

public class CollectionsDemo {
	public static void main(String[] args) {
		// create a list with n copies
		List list = Collections.nCopies(3, "java");

		// create an iterator
		Iterator itr = list.iterator();

		System.out.println("Values are :");
		while (itr.hasNext()) {
			System.out.println(itr.next());
		}
	}
}

 

 

Output:

  • java

  • java

  • java

Declaration:

  • public static <T> List<T> nCopies(int n, T o)

Parameters:

  • n– The number of elements in the returned list.

  • o– The element to appear repeatedly in the returned list.

Return Value:

  •             The method call returns an immutable list consisting of n copies of the specified object.

 

Exception:

  •             IllegalArgumentException–This is thrown if n < 0.

 

BACK

Filed in: Z-A All Java Codes

Comments are closed.