java中的数组
java中使用关键字new创建数组对象,格式为:数组名=new 数组元素的类型[数组元素的个数]
public class Test { public static void main(String[] args) { int[] s; s = new int[5]; for (int i = 0; i < 5; i++) { s[i] = i; System.out.println(s[i]); } } } public class Test { public static void main(String[] args) { int[] s; s = new int[5]; for (int i = 0; i < 5; i++) { s[i] = i; System.out.println(s[i]); } } }
二:元素为引用数据类型的数组中的每一个元素都需要实例化。
public class Test2 { public static void main(String[] args) { Date[] days; days = new Date[3]; for (int i = 0; i < 3; i++) { days[i] = new Date(2004, 4, i + 1); System.out.println(days[i].toString()); } } } class Date { int year, month, day; Date(int y, int m, int d) { year = y; month = m; day = d; } public String toString() { return this.year + "," + this.month + "," + this.day; } } public class Test2 { public static void main(String[] args) { Date[] days; days = new Date[3]; for (int i = 0; i < 3; i++) { days[i] = new Date(2004, 4, i + 1); System.out.println(days[i].toString()); } } } class Date { int year, month, day; Date(int y, int m, int d) { year = y; month = m; day = d; } public String toString() { return this.year + "," + this.month + "," + this.day; } }
补充:软件开发 , Java ,