blob: 00ffc6fbbd0f6cde861315839dbf58433ddb8c32 (
plain)
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
|
/* Listing4309.java */
import java.lang.reflect.*;
public class Listing4309
{
public static void createArray1()
{
//Erzeugt ein eindimensionales int-Array
Object ar = Array.newInstance(Integer.TYPE, 3);
for (int i = 0; i < Array.getLength(ar); ++i) {
Array.set(ar, i, new Integer(i));
System.out.println(Array.getInt(ar, i));
};
}
public static void createArray2()
{
//Erzeugt ein zweidimensionales String-Array
Object ar = Array.newInstance(String.class, new int[]{7, 4});
for (int i = 0; i < Array.getLength(ar); ++i) {
Object subArray = Array.get(ar, i);
for (int j = 0; j < Array.getLength(subArray); ++j) {
String value = "(" + i + "," + j + ")";
Array.set(subArray, j, value);
System.out.print(Array.get(subArray, j) + " ");
}
System.out.println();
};
}
public static void main(String[] args)
{
createArray1();
System.out.println("--");
createArray2();
}
}
|