java中怎么创建对象数组

如题所述

首先我们需要创建一个class:

class Student{  
    String name;  
    double score;  
    String num;  
      
    Student(String n,double s,String m){  
        name=n;  
        s=score;  
        num=m;  
    }  
  
    public static void printInfo(){  
        System.out.println(num+","+name+","+score);  
    }  
  
}

接下来我们对此类进行数组的创建:

//1  
Student stu[];<span style="white-space:pre">      </span>//声明数组。  
stu=new Student [3];<span style="white-space:pre">    </span>//创建数组,这里是创建的一个引用的数组,每一个引用并没有确切的地址。  
for(int i=0;i<3;i++){<span style="white-space:pre">    </span>//为数组创建对象,也就是说为创建的引用关联到确切的地址。  
    stu[i]=new Student();  
}  
//2  
Student stu[]=new Student [3];  
for(int i=0;i<3;i++){  
    stu[i]=new Student();  
}  
//3  
Student stu[]=new Student{new Student(sjl,87,01),new Student(ljs,98,02),new Student(lls,92,03)};

温馨提示:内容为网友见解,仅供参考
第1个回答  2017-05-18
你这个试试对对象数组的一个声明,并没有示例话,所以会报空指针异常
这个数组对象都是现用现初始化的
Animals [] an=new Animals[5];//这只是个对象类型数组的声明
用的时候需要
for(int i=0;i<5;i++)
an[i]=new Animals();
这样你明白了吧
你前面的那个光声明了数组,但是没有调用Animals的构造函数,你数组里的每个元素都是一个对象,使用前必须要先实例化
如果你只是用户输入长度,
Animals [] an=new Animals[n];
声明时是可以用变量的
或者你直接Animals [] an=new Animals[100];定义一个大数组,要用的时候再new Animals();实例化,或者用LinkedList<Animals> an=new LinkedList<Animals>();定义一个动态数组本回答被网友采纳
第2个回答  2008-06-25
Student st =new Student[5];
首先数组定义错误 st是一个Student数组 初始化的时候错了,应该是:
Student[] st =new Student[5];
其次:
Student st =new Student[5];
st[1].setStudent("WangZhen", 20, 1, 1.76);
st[2].setStudent("YangZhen", 21, 2, 1.66);
st[3].setStudent("Wangqiang", 19, 3, 1.86);
st[4].setStudent("XiaoMing", 18, 4, 1.70);
st[5].setStudent("XiaoHong", 22, 5, 1.74);
调用对象数组中Student对象的方法前要对数组中的Student对象进行初始化,应该在
Student st =new Student[5];后加上
for (int i = 0; i < st.length; i++) {
st[i] = new Student();
}
3:java中数组下标从0开始,长度为5的数组进行循环应该是从0到4,如果用st[5]会出现数组越界。
我简单改了下你的代码:
public static void main(String args[]) throws IOException {
Student[] st = new Student[5];
for (int i = 0; i < st.length; i++) {
st[i] = new Student();
}
st[0].setStudent("WangZhen", 20, 1, 1.76);
st[1].setStudent("YangZhen", 21, 2, 1.66);
st[2].setStudent("Wangqiang", 19, 3, 1.86);
st[3].setStudent("XiaoMing", 18, 4, 1.70);
st[4].setStudent("XiaoHong", 22, 5, 1.74);
for (int i = 0; i < 5; i++)
st[i].display();
System.out.println("name");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
for (int i = 0; i < 5; i++) {
if (st[i].name.equals(str)) {
System.out.println("result");
st[i].display();
}
}

}
第3个回答  2008-06-25
//如果你是初学JAVA,建议你看看这段代码,里面有一些基本技巧
//有疑问可以问我!

import java.io.*;

public class StudentInformation {

public static void main(String args[]) throws IOException {
Student[] st = new Student[5];// 第一个错误
int i = 0;
st[i++] = new Student().setStudent("WangZhen", 20, 1, 1.76);
st[i++] = new Student().setStudent("YangZhen", 21, 2, 1.66);
st[i++] = new Student().setStudent("Wangqiang", 19, 3, 1.86);
st[i++] = new Student().setStudent("XiaoMing", 18, 4, 1.71);
st[i++] = new Student().setStudent("XiaoHong", 22, 5, 1.74);
for (i = 0; i < st.length; i++)
st[i].display();
System.out.println("请输入要查询学生的姓名");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
for (i = 0; i < st.length; i++) {
if (st[i].name.equals(str)) {
System.out.println("所要查找学生为");
st[i].display();
}
}

}

}

class Student {
String name;

int age;

int num;// 学号

double high;// 身高

public void display() {
System.out.println("姓名:" + name + "\t年龄:" + age + "\t身高:" + high + "\t学号:" + num);
}

public Student setStudent(String n, int a, int nu, double h) {
name = n;
age = a;
num = nu;
high = h;
return this;
}
}
第4个回答  2008-06-25
Student []st =new Student[5];
for(int i=0;i++;i<st.length){
st[i]=new Student();
}
st[1].setStudent("WangZhen", 20, 1, 1.76);
st[2].setStudent("YangZhen", 21, 2, 1.66);
st[3].setStudent("Wangqiang", 19, 3, 1.86);
st[4].setStudent("XiaoMing", 18, 4, 1.70);
st[5].setStudent("XiaoHong", 22, 5, 1.74);

对象数组中的对象没有实例化

把 setStudent中的内容放到构造方法中在实例化的时候传值比较好