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 ();
}
}