Another way to replicate Java collections __java

Source: Internet
Author: User

Question:

Hi

I want to copy a list<integer> to a arraylist<integer> but I can ' t seem to is able to does so ... I don ' t want to cast it ... Below is My Code ... Help


list<integer> data = some data;
arraylist<integer> datacopy = new arraylist<integer> ();
Collections.copy (datacopy, data);


I keep getting the following exception:

Exception in thread ' main ' Java.lang.IndexOutOfBoundsException:Source does not fit in dest


Answer:


Let a ArrayList constructor do job:

Java Code:

1 arraylist<integer> datacopy = newarraylist<integer> (data);
Kind Regards,

Jos


Even can use the AddAll (Collection C) method of ArrayList as. Something like,
Java Code:

1 Datacopy.addall (data);

Goldest



My Code below:


Import java.util.*;
	public class Algorithms1 {private character[] ch = {' P ', ' C ', ' M '};//private character[] CHN = new CHARACTER[3];
	Private List <character>list;
	
	Private List <character>copyoflist;
	Public Algorithms1 () {list = arrays.aslist (CH);
		
		Copyoflist = Arrays.aslist (CHN);
		System.out.println ("Initial List:");
		
		print (list);
		Collections.reverse (list);
		System.out.println ("After calling reverse:");
		
		
		print (list); /** * Use Collections.copy (copyoflist, list), then there will be types of competent mismatch of the exception * then we can copy the array also use the ArrayList constructor to implement * Of course, you have to use the Copy method to use a single line
		The contents of the annotation can also be * This example mainly shows that replication does not rigidly adhere to the Copy method * Sometimes with the constructor, sometimes addall also become * *//collections.copy (copyoflist, list);
		Copyoflist = new ArrayList (list);
		SYSTEM.OUT.PRINTLN ("after copying");
		
		Print (copyoflist);
		Collections.fill (list, ' R ');
		System.out.println ("After calling fill:");
		
		
		
	print (list); List<character> listref {System.out.print ("The List is:"));
		for (Character list:listref) {System.out.printf ("%s", list);
		
		} System.out.println ();
		Compare (LISTREF);
	System.out.println (); public void Compare (List<character> listref) {System.out.printf ("Max:%s, Min:%s\n", Collections.max (LIS
	Tref), Collections.min (Listref));
	public static void Main (string[] argc) {new Algorithms1 ();
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.