(Java) 我无法遍历我的 arraylist 对象?

时间:2021-06-09 08:59:20

标签: java loops object arraylist

我正在尝试遍历一个 ArrayList 对象,但我只得到以下内容:

姓名和教育: com.company.Item@69d9c55

我不知道为什么会发生这种情况。我正在使用我在线学习的 MOOC 课程中提供的代码,但我正在尝试在我自己的小型测试项目中重做此代码,因为我想将我学到的知识付诸实践。

代码如下: 主要

package com.company;

import java.util.Scanner;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
    // write your code here

        Scanner scan = new Scanner(System.in);

        ArrayList<Item> itemList = new ArrayList<>();
        Item item;
        while(true) {
            System.out.println("Name plz?");
            String name = scan.nextLine();
            if (name.isEmpty()) {
                break;
            }

            System.out.println("Education plz?");
            String education = scan.nextLine();
            if (education.isEmpty()) {
                break;
            }

            item = new Item(name, education);
            if (!itemList.contains(item)) {
                itemList.add(item);
            }

        }

        System.out.println("Name & Education: ");
        for(Item detail : itemList) {
            System.out.println(detail);
        }

    }
}

和主类

package com.company;

public class Item {
    String name;
    String education;

    public Item(String name, String education) {
        this.name = name;
        this.education = education;
    }
}

2 个答案:

答案 0 :(得分:0)

在您的 for 循环中,您正在打印 detail 对象。

for(Item detail : itemList) {
    System.out.println(detail);
}

// prints
com.company.Item@69d9c55

第一部分是包名(认为是文件夹),@ 后面的随机数是对象的散列值。所以基本上是类的一种简短的文本表示。

有两种方法可以解决这个问题:

向对象添加 getter 以获取要打印的值:

public class Item {
    String name;
    String education;

    public Item(String name, String education) {
        this.name = name;
        this.education = education;
    }

    public String getName() {
        return name;
    }

    public String getEducation() {
        return education;
    }
}

// Then in your loop fetch the values
for(Item detail : itemList) {
    System.out.println(detail.getName());
    System.out.println(detail.getEducation());
}

或更高级的版本,它覆盖了对象中的 toString 函数。每个对象都有一个名为 toString 的函数,它默认打印对象哈希。您可以通过将函数命名为 toString,然后使用 @Override 注释该函数来对此进行自定义。然后在该函数中,当您尝试打印对象时,您可以编写要打印的自定义字符串。

public class Item {
    String name;
    String education;

    public Item(String name, String education) {
        this.name = name;
        this.education = education;
    }

    @Override
    public String toString() {
        return name + ", " + education;
    }
}

答案 1 :(得分:-1)

在您的 for 循环 中迭代您的 Item 列表(也许您应该使用多态性重新定义您的 ArrayList)您将每次获得一个对象 item 而不是细节,所以选择执行变量的名称

如果您还没有实现或覆盖item.getEducation(),您应该从这个对象项目中调用item.getName()或/和item.getEducation() >public String toString() 方法用于您的 Item 类或 @Override public String toString() 来自自动继承的 java.lang.Object< /strong> 类。

话虽如此,让我们修复和优化您的代码:


Main.java

package com.company;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
    // write your code here

        Scanner scan = new Scanner(System.in);
        // Relying on the diamond operator
        List<Item> itemList = new ArrayList<>();
        Item item;
        while(true) {
            System.out.println("Name plz?");
            String name = scan.nextLine();
            if (name.isEmpty()) {
                break;
            }

            System.out.println("Education plz?");
            String education = scan.nextLine();
            if (education.isEmpty()) {
                break;
            }

            item = new Item(name, education);
            if (!itemList.contains(item)) {
                itemList.add(item);
            }

        }

        System.out.println("Name & Education: ");
        for(Item item : itemList) {
            // Just call toString-method of Item class
            System.out.println(item.toString());
        }
    }
}

Item.java

package com.company;

public class Item {
    String name;
    String education;

    public Item(String name, String education) {
        this.name = name;
        this.education = education;
    }
    
    public String getName() {
        return this.name;
    }
    
    public String getEducation() {
        return this.education;
    }
    // you could also add some setters for these attributes if you need some later on
    
    @Override
    public String toString() {
        return "[Name: " + this.name + ", Education: " + this.education + "]";
    }
}

希望这对您有所帮助。如果答案为您带来更多细节,请确保您点赞!