有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

oop矢量的所有元素都被最后一个对象java替换

在这里,我试图将对象添加到向量中,并从向量中获取元素。 在for循环向量中给出了所有3个对象的详细信息。 但我想让物体脱离循环。但它只给出了第三个对象的细节

static Vector<Student> vector = null;
static Student student= null;
public static void AskStudentDetails(){
    Scanner input = new Scanner(System.in);
    student = new Student();
    vector = new Vector<Student>();

    for(int i=0; i<MAX_STUDENT; i++){               
        System.out.print("Coursework 01 Marks : ");
        student.setCoursework1(input.nextInt());

        vector.addElement(student); //add object to the vector 
        Student mm = vector.elementAt(0);
        System.out.println(mm.getCoursework1());        
    }
    input.close();
    student = vector.elementAt(1);//assign to the object student 
    System.out.println(student.getCoursework1()); // always print only the value of third object
}

学生。班 公共课学生实现java。伊奥。可序列化{

    private int coursework1;



    public int getCoursework1() {
        return coursework1;
    }
    public void setCoursework1(int coursework1) {
        this.coursework1 = coursework1;
    }

}


共 (0) 个答案