现在的位置: 首页 > 综合 > 正文

java中的数组

2013年01月05日 ⁄ 综合 ⁄ 共 509字 ⁄ 字号 评论关闭

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 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;

	}
}

抱歉!评论已关闭.