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
103 lines (89 loc) · 2.87 KB
/
TestArray.java
File metadata and controls
103 lines (89 loc) · 2.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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("");
}
//Can the length of second dimension be 0? Yes!
int[][] nums3 = new int[4][0];
//int[][] nums3 = new int[0][0]; //First dimension can also have 0 length
System.out.println("nums3: 4 x 0: ");
for (int i = 0; i < nums3.length; ++i) {
System.out.print("Row " + i + ": ");
for (int j = 0; j < nums3[i].length; ++j) {
System.out.print("(" + i + ", " + j + ")=" + nums3[i][j] + " ");
}
System.out.println("");
}
//Array of super types
Animal[] animals = new Animal[3];
for (int i = 0; i < animals.length; ++i) {
animals[i] = new Cat(i, i*2, i+3);
}
for (Animal animal : animals) {
//Should output cat's info
System.out.println("Animal = " + animal);
}
//Cast array of T to array of T's super class
Cat[] cats = new Cat[3];
for (int i = 0; i < cats.length; ++i) {
cats[i] = new Cat(i, i*3, i+4);
}
Animal[] newAnimals = (Animal[])cats;
for (Animal newAnimal : newAnimals) {
//Should output cat's info too
System.out.println("New animal = " + newAnimal);
}
Object[] strings = new String[2];
strings[0] = "hi"; // OK
strings[1] = 100; // A run-time ArrayStoreException is thrown.
}
}