java中数组与泛化学习日志

Java 中的数组:
首先要记得:java需要用户手动的使用new语句实例化数组中的每个元素,否则java无法识别数组,例子如下:
public class Test {
public static void main(String[] args) {
Student[] a; // Variable.
a = new Student[3]; // Array object.the length is 3
public Car(String name, int numberOfDoors) throws BadCarException {
// If create a car with less than one door must throw a BadCarException
if (numberOfDoors < 1) {
throw new BadCarException(“A car must have at least one door!”);
} else {
this.name = name;
this.doors = new Door[numberOfDoors];//create the variable as array
/*
* the array must create new door one by one by using the new operator Java does
* not do this automatically for you because it cannot guess how you want to
* create the elements of the array (which constructor to use, etc.)
*/

		for (int i = 0; i < numberOfDoors; i++) {
			doors[i] = new Door();
		}
	}
}

System.out.println("length: " + a.length);
// Creating all the array elements one by one:
for(int i = 0; i < a.length; i++) {
a[i] = new Student("Student " + i);
}
for(int i = 0; i < a.length; i++) {
// a[i] is of type Student.
System.out.println("name: " + a[i].getName());
}
}
}

在第二个例子中,程序中加入了exception的报错机制,倘若程序正常,那么首先创建数组变量:this.doors=new Door[numberofDoors];
然后使用for循环一一实例化Door数据中的元素
//Variable must provide either dimension expressions or an array initializer
所以numberofDoors必须存在。

第二个数组中常见的内容是有关增强的for循环:
增强的for循环中间冒号,前面是创建的临时变量,后面是数组名
for(Student s: a) {
System.out.println("name: " + s.getName());
}
for(int i = 0; i < a.length; i++) {
Student s = a[i];
System.out.println("name: " + s.getName());
}
增强for循环易读易写,同时也能够减少角标的错误

关于数组的扩充,可以使用一个新的数组,在继承原有的数组上扩充
public void expandCar() {
Door[] door2 = new Door[this.doors.length + 2];// create a new array
// assign the values in the old array to the new array
for (int i = 0; i < this.doors.length; i++) {
door2[i] = doors[i];
}
// the following two statements are used to add the new two doors
door2[doors.length] = new Door();
door2[doors.length + 1] = new Door();
doors = door2;// assign the value back
}

另外创建数组的方法,使用arraylist的方法:
public Car(String name, int numberOfDoors) throws BadCarException {
// If create a car with less than one door must throw a BadCarException
if (numberOfDoors < 1) {
throw new BadCarException(“A car must have at least one door!”);
} else {
this.name = name;
this.doors = new ArrayList(numberOfDoors);// create the variable as array
for(int i=0;i<numberOfDoors;i++){
doors.add(new Door());
}
/*
* the arraylist must create new door one by one by using the new operator Java does
* not do this automatically for you because it cannot guess how you want to
* create the elements of the array (which constructor to use, etc.)
*/
}
}
在arraylist中,使用add添加元素,<>包含的是泛化类型,可以是创建出的任何类,Door必须是一个已创造出来的class
希望上面的例子和肤浅的讲解,可以给学习路上的你一点启发,加油吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值