|
JAVA程序员必读:基础篇(4)对象和简单数据对象
|
|
编译:ZSC/太平洋网络学院
|
|
|
对象和简单数据对象
4.4.3 数组的数组
数组可以容纳数组。以下的例程ArrayOfArraysDemo创建一个数组并使用一个初始化程序来包含四个子数组:
public class ArrayOfArraysDemo {
public static void main(String[] args) {
String[][] cartoons =
{
{ "Flintstones", "Fred", "Wilma", "Pebbles",
"Dino" },
{ "Rubbles", "Barney", "Betty", "Bam Bam" },
{ "Jetsons", "George", "Jane", "Elroy", "Judy",
"Rosie", "Astro" },
{ "Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma",
"Fred", "Daphne" }
};
for (int i = 0; i < cartoons.length; i++) {
System.out.print(cartoons[i][0] + ": ");
for (int j = 1; j < cartoons[i].length; j++)
{
System.out.print(cartoons[i][j] + " ");
}
System.out.println();
}
}
}
这里注意,所有的子数组都有不同的长度。子数组的名字是否cartoons[0]和cartoons[1]等等。
[上一页] [下一页]
|