Skip to content

Commit 7c331e1

Browse files
Chapter 16 arrays btp170603 (#5)
* 170537-Generic * 0528-Generic * Chapter-15-Generic-btp0530 * Chapter-16-Arrays-btp170603
1 parent 9604e80 commit 7c331e1

8 files changed

Lines changed: 253 additions & 0 deletions

src/btp/oneP/ArrayOfGenerics.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package btp.oneP;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class ArrayOfGenerics {
8+
9+
public static void main(String[] args) {
10+
//编译期不能实例化泛型数组,但是可以创建泛型数组的引用
11+
List<String>[] ls;
12+
List[] la = new List[10];
13+
ls = (List<String>[])la;
14+
ls[0] = new ArrayList<String>(Arrays.asList("hehe","keke","xixi"));
15+
//ls[1] = new ArrayList<Integer>();
16+
17+
Object[] objects = ls;
18+
objects[1] = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
19+
System.out.println(Arrays.toString(la));
20+
System.out.println(la.getClass().getName());
21+
System.out.println(Arrays.toString(ls));
22+
System.out.println(ls.getClass().getName());
23+
System.out.println(Arrays.toString(objects));
24+
System.out.println(objects.getClass().getName());
25+
List<BerylliumSphere>[] spheres = (List<BerylliumSphere>[])new List[10];
26+
for(int i=0;i<10;i++){
27+
spheres[i] = new ArrayList<BerylliumSphere>();
28+
}
29+
}
30+
31+
}

src/btp/oneP/ArrayOptions.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package btp.oneP;
2+
3+
import java.util.Arrays;
4+
5+
public class ArrayOptions {
6+
7+
public static void main(String[] args) {
8+
BerylliumSphere[] a = null;
9+
BerylliumSphere[] b = new BerylliumSphere[5];
10+
System.out.println(a);
11+
System.out.println(b);
12+
System.out.println(Arrays.toString(b));
13+
14+
BerylliumSphere[] c = new BerylliumSphere[4];
15+
for(int i=0;i<c.length;i++){
16+
if(null == c[i]){
17+
c[i] = new BerylliumSphere();
18+
}
19+
}
20+
21+
BerylliumSphere[] d = {
22+
new BerylliumSphere(),new BerylliumSphere(),new BerylliumSphere()
23+
};
24+
a = new BerylliumSphere[]{
25+
new BerylliumSphere(),new BerylliumSphere(),new BerylliumSphere()
26+
};
27+
System.out.println("a.length:"+a.length);
28+
System.out.println("b.length:"+b.length);
29+
System.out.println("c.length:"+c.length);
30+
System.out.println("d.length:"+d.length);
31+
32+
a = d;
33+
System.out.println("a.length:"+a.length);
34+
35+
int[] e;
36+
int[] f = new int[4];
37+
System.out.println("f:"+Arrays.toString(f));
38+
int[] g = new int[4];
39+
for(int i=0;i<g.length;i++){
40+
g[i] = i*i;
41+
}
42+
int[] h = {11,47,93};
43+
System.out.println("f.length:"+f.length);
44+
System.out.println("g.length:"+g.length);
45+
System.out.println("h.length:"+h.length);
46+
47+
e = h;
48+
System.out.println("e.length:"+e.length);
49+
e = new int[]{1,2};
50+
System.out.println("e.length:"+e.length);
51+
52+
char[] cs = new char[5];
53+
System.out.println(Arrays.toString(cs));
54+
}
55+
56+
}

src/btp/oneP/ArrayTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package btp.oneP;
2+
3+
public class ArrayTest {
4+
5+
public static void main(String[] args) {
6+
//f({1,2,3});
7+
f(new int[]{1,2,3});
8+
}
9+
10+
static void f(int[] a){
11+
for(int i:a){
12+
System.out.println(i);
13+
}
14+
}
15+
16+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package btp.oneP;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class ContainerComparison {
8+
9+
public static void main(String[] args) {
10+
BerylliumSphere[] spheres = new BerylliumSphere[10];
11+
for(int i=0;i<5;i++){
12+
spheres[i] = new BerylliumSphere();
13+
}
14+
System.out.println(Arrays.toString(spheres));
15+
System.out.println(spheres[4]);
16+
17+
List<BerylliumSphere> sphereList = new ArrayList<BerylliumSphere>();
18+
for(int i=0;i<5;i++){
19+
sphereList.add(new BerylliumSphere());
20+
}
21+
System.out.println(sphereList);
22+
System.out.println(sphereList.get(4));
23+
24+
int[] integers = {0,1,2,3,4,5};
25+
System.out.println(integers);
26+
System.out.println(integers[4]);
27+
28+
List<Integer> intList = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4,5));
29+
intList.add(97);
30+
System.out.println(intList);
31+
System.out.println(intList.get(4));
32+
33+
}
34+
35+
}
36+
37+
class BerylliumSphere{
38+
private static long counter;
39+
private final long id = counter++;
40+
public String toString(){
41+
return "Sphere"+id;
42+
}
43+
}

src/btp/oneP/IceCream.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package btp.oneP;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
public class IceCream {
7+
8+
private static Random rand = new Random(47);
9+
static final String[] FLAVORS = {
10+
"Chocolate","Strawberry","Vanilla","Mint Chip",
11+
"Mocha Almond Fudge","Rum Raisin","Praline","Mud"
12+
};
13+
14+
public static String[] flavorSet(int n){
15+
if(n>FLAVORS.length){
16+
throw new IllegalArgumentException("Set too big");
17+
}
18+
String[] results = new String[n];
19+
boolean[] picked = new boolean[FLAVORS.length];
20+
for(int i=0;i<n;i++){
21+
int t;
22+
do
23+
t = rand.nextInt(FLAVORS.length);
24+
while(picked[t]);
25+
results[i] = FLAVORS[t];
26+
picked[t] = true;
27+
}
28+
return results;
29+
}
30+
31+
32+
public static void main(String[] args) {
33+
for(int i=0;i<7;i++){
34+
System.out.println(Arrays.toString(flavorSet(3)));
35+
}
36+
}
37+
38+
39+
40+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package btp.oneP;
2+
3+
import java.util.Arrays;
4+
5+
public class MultidimensionalPrimitiveArray {
6+
7+
public static void main(String[] args) {
8+
int[][] a = {{1,2,3},{4,5,6}};
9+
System.out.println(Arrays.toString(a));
10+
System.out.println(Arrays.toString(a[1]));
11+
System.out.println(Arrays.deepToString(a));
12+
}
13+
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package btp.oneP;
2+
3+
import java.util.Arrays;
4+
5+
public class ParameterizedArrayType {
6+
7+
public static void main(String[] args) {
8+
Integer[] ints = {1,2,3,4,5};
9+
Double[] doubles = {1.1,2.2,3.3,4.4,5.5};
10+
Integer[] ints2 = new ClassParameter<Integer>().f(ints);
11+
System.out.println(Arrays.toString(ints2));
12+
System.out.println(ints == ints2);
13+
14+
Double[] doubles2 = new ClassParameter<Double>().f(doubles);
15+
System.out.println(Arrays.toString(doubles2));
16+
17+
ints = MethodParameter.f(ints);
18+
System.out.println(Arrays.toString(ints));
19+
doubles = MethodParameter.f(doubles);
20+
}
21+
22+
}
23+
24+
class ClassParameter<T>{
25+
public T[] f(T[] args){
26+
return args;
27+
}
28+
}
29+
30+
class MethodParameter{
31+
public static <T> T[] f(T[] arg){
32+
return arg;
33+
}
34+
}

src/btp/oneP/ThreeWithNew.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package btp.oneP;
2+
3+
import java.util.Arrays;
4+
5+
public class ThreeWithNew {
6+
7+
public static void main(String[] args) {
8+
int[][][] a = new int[2][2][4];
9+
System.out.println(a);
10+
System.out.println(Arrays.toString(a));
11+
System.out.println("a.length:"+a.length);
12+
System.out.println(Arrays.toString(a[0]));
13+
System.out.println("a[0].length:"+a[0].length);
14+
System.out.println(Arrays.toString(a[1]));
15+
System.out.println("a[1].length:"+a[1].length);
16+
System.out.println(Arrays.toString(a[0][1]));
17+
}
18+
19+
}

0 commit comments

Comments
 (0)