在JAVA项目中打印内存地址而不是ArrayList的内容

时间:2016-04-13 16:05:12

标签: java arraylist

在这里学习一个学校项目,有点难过。这是一门初学java课程,如果我的解释有点粗糙,请耐心等待。

因此,对于此作业,我将获得一个学生数据的字符串数组,我必须将其拆分以删除逗号并将其作为ArrayList的对象重新创建为ArrayList。我能够分割,删除逗号并将信息传递回我称为孩子们的ArrayList但是当我尝试迭代名为kids的ArrayList时,我得到的是内存地址而不是实际的“分裂”信息字符串。

我的输出是:

学生@ 74a14482

学生@ 1540e19d

学生@ 677327b6

学生@ 14ae5a5

学生@ 7f31245a

在这个阶段,项目还不完整,我需要实施一些方法,但在找到如何正确打印之前我无法完成。

我的研究告诉我,也许我需要使用.toString方法,但不知道在哪里......

Roster.java

public class Roster {

static String[] students = {"1,John,Smith,John1989@gmail.com,20,88,79,59",
            "2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
            "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87",
            "4,Erin,Black,Erin.black@comcast.net,22,91,98,82",
            "5,David,Reeves,deemreeves@gmail.com,33,81,95,89"};

/* Create ArrayList */
static ArrayList<Student> kids = new ArrayList<Student>();

public static void main(String[] args) {
        new Roster();
    }


public Roster(){
    for (int i = 0; i < students.length; i++) {
        String s = students[i];

        /* split Students array and remove commas and place strings into array named parts */
        String[] parts = s.split(",");

        /* assign values from split students array to variables to be passed into a Student Object */
        String StudentID = parts[0];
        String firstname = parts[1];
        String lastname = parts[2];
        String email = parts[3];


        Student a = new Student(StudentID, firstname, lastname, email);
        kids.add(a);

    }

    System.out.println("Why is this printing out as a memory address instead of the contents of the array list!");
    for (Student a : kids){
        System.out.println(Arrays.toString(a));
    }
 }

Student.Java

public class Student {

/* Instance Variables */

String studentID;
String firstname;
String lastname;
String email;
int age;
int grades[];

public Student(String studentID, String firstname, String lastname, String email) {


    this.studentID = studentID;
    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;

}

}

0 个答案:

没有答案