-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList16.java
51 lines (41 loc) · 1.5 KB
/
ArrayList16.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* 16. Write a Java program to clone an array list to another array list.
Expectation: the output of the program return an identical array list
Use Collections.copy() to get a copy dest must not be empty. else exception: java.lang.IndexOutOfBoundsException
List.copyOf()
create empty list and do addAll()
Output (Actual):
call clone() directly.
Compile time Warnings:
Note: ArrayList16.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Better create an empty list and do addAll()
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class ArrayList16
{
public static void main(String[] args)
{
ArrayList<String> colors = getColors();
System.out.println("Colors are: "+colors);
List<String> colors2 = new ArrayList<>();
//colors2 = colors.clone(); colors is referenced by List and clone() is not available in List. gives compile error
colors2 = (ArrayList<String>)colors.clone();
System.out.println("After copying: "+colors2);
}
public static ArrayList<String> getColors()
{
ArrayList<String> colorsList = new ArrayList<>();
System.out.println("Adding colors...");
colorsList.add(new String("Red"));
colorsList.add(new String("Blue"));
colorsList.add(new String("Green"));
colorsList.add(new String("Orange"));
colorsList.add(new String("Violet"));
colorsList.add(new String("Yellow"));
colorsList.add(new String("Indigo"));
colorsList.add(new String("White"));
return colorsList;
}
}