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
66 lines (56 loc) · 1.74 KB
/
TestArray.java
File metadata and controls
66 lines (56 loc) · 1.74 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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);
//Test 2d Array:
//http://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array
int[][] nums = new int[][]{
{},
{1},
{1, 3, 5, 7},
{9, 8, 3, 11, -55},
{4, 4, 8, 12, 55, 3, 8, -3, 4}
};
for (int[] array : nums) {
System.out.println("Original array: " + Arrays.toString(array));
}
int[][] nums2 = new int[4][7];
for (int i = 0; i < nums2.length; ++i) {
for (int j = 0; j < nums2[i].length; ++j) {
System.out.print("(" + i + ", " + j + ")=" + nums2[i][j] + " ");
}
System.out.println("");
}
}
}