public class ArrayTest3
{
public static void main(String[] args)
{
Student[] s = new Student[100];
for(int i = 0; i < s.length; i++)
{
s[i] = new Student();
s[i].name = i % 2 == 0 ? "zhangsan" : "lisi";
/* 等价于上面的三元表达式
if(i % 2 == 0)
{
s[i].name = "zhangsan";
}
else
{
s[i].name = "lisi";
}
*/
}
for(int i = 0; i < s.length; i++)
{
System.out.println(s[i].name);
}
}
}
class Student
{
String name;
}
二维数组:平面的二维结构,本质上是数组的数组。
public class ArrayTest4
{
public static void main(String[] args)
{
/*
int[][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[3];
a[2] = new int[1];
*/
int[][] a = new int[][]{{1, 2, 3}, {4}, {5, 6, 7, 8}};
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a[i].length; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}