forked from eaglesky/JavaPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestArray.java
More file actions
47 lines (37 loc) · 1.18 KB
/
TestArray.java
File metadata and controls
47 lines (37 loc) · 1.18 KB
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
import java.util.*;
public class TestArray {
private static int[] returnArray(int a, int b, int c) {
// Using {a, b, c} only won't work!
return new int[]{a, b, c};
}
// Using Enhanced For-Loops
private static void printArray(int[] array) {
System.out.print("Array content: ");
for (int i : array) {
System.out.print(i + " ");
}
System.out.println("");
}
public static void main (String[] args) {
int a1 = 3;
int a2 = 5;
int a3 = 7;
int[] arr = {a1, a2, a3};
System.out.println("Array length = " + arr.length);
printArray(arr);
int[] arr2 = returnArray(2, 4, 6);
printArray(arr2);
System.out.println("Second element = " + arr[1]);
int[] arr3 = new int[4];
printArray(arr3);
List<Integer> convertedList = Arrays.asList(4, 5, 6);
System.out.println("Converted List = " + convertedList);
Integer[] arr4 = {7, 8, 9}; //not int[]!
convertedList = Arrays.asList(arr4);
System.out.println("Converted List = " + convertedList);
//If the content of arr4 changes, so does that of the convertedList,
//because Arrays.asList returns a wrapper of original array, not a copy.
arr4[0] = -1;
System.out.println("Converted List = " + convertedList);
}
}