-3

读取数组后,我在打印数组时遇到问题。打印后,打印的是内存地址,而不是数组的值。我能做些什么呢?

public class MyClass
{
     Student St = new Student();

     Student[]Array1 = new Student[10];


     void AddList()
     {

            Scanner Scan = new Scanner(System.in);

            for (int i=0; i<Array1.length & i<ArrayF1.length; i++)
            {
                System.out.println("Enter Student NAME Number " + (i+1) + ":");
                Array1[i] = new Student();
                Array1[i].setName(Scan.next());


                //System.out.println("Enter Student MARK Number " + (i+1) + ":");
                //St.setMark(Scan.nextFloat());
            }

    }

这是我的打印方法。打印的结果是这样的

(studentproject.Student@1a758cb)

 void PrintList()
 {

            for (int i=0; i<Array1.length; i++)
            {

                System.out.println(Array1[i]);

            }
 }

这是我的学生类,我有我所有的 setter 和 getter 方法......所以我有 3 个类,我如何使用这 3 个类,其中一个获取数据,另一个打印标记数据,第三个类打印学生姓名数据...我该怎么做...我做了一些代码,但我不知道它是否正确...谢谢您的帮助...

public class Student
{
private String Name;
private float Mark;

/**
 * @return the Name
 */
public String getName() {
    return Name;
}

/**
 * @param Name the Name to set
 */
public void setName(String Name) {
    this.Name = Name;
}

/**
 * @return the Mark
 */
public float getMark() {
    return Mark;
}

/**
 * @param Mark the Mark to set
 */
public void setMark(float Mark) {
    this.Mark = Mark;
}

}
4

2 回答 2

3

只需覆盖类中的toString()方法Student,并在打印实例时返回要打印的适当字符串。

它可能看起来像: -

@Override
public String toString() {
    return "Name: " + studentName;
}

toString()目前,调用类方法的默认实现,Object您看到的是从该方法返回的格式,其形式为 -Type@hashCode

于 2012-12-29T14:51:06.007 回答
0

在这里,我添加了一些如何toString()覆盖方法的内容

public class Student {

    private String name;
    private int id;
    float mark;

    public Student() {
    }

    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getMark() {
        return mark;
    }

    public void setMark(float mark) {
        this.mark = mark;
    }

    @Override
    public String toString() {
        return "Student[ID:" + id + ",Name:" + name + ",Mark:"+mark+"]";
    }

    public void printStudentInfo() {
        // print all the details of student
    }
    public static void main(String[] args) {
        Student[] students = new Student[10];
        Scanner scanner = new Scanner(System.in);

        for (int i = 0; i < students.length; i++) {
            System.out.println("Enter Student Name " + (i + 1) + ":");
            String name = scanner.nextLine();
            Student student = new Student(name, i + 1);
            System.out.println("Enter Student MARK Number " + (i + 1) + ":");
            float mark = scanner.nextFloat();
            student.setMark(mark);
            students[i]=student;
        }
        for(Student student:students) {
            // by default toStirng method is called
            System.out.println(student);
            //or you can call like
            //student.printStudentInfo();
        }
    }
}
于 2012-12-29T15:06:26.717 回答