【问题标题】:(Java) I cannot loop over my arraylist object?(Java) 我不能遍历我的 arraylist 对象?
【发布时间】:2021-08-26 06:18:07
【问题描述】:

我正在尝试遍历一个 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;
    }
}

【问题讨论】:

    标签: java loops object arraylist


    【解决方案1】:

    在您的 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;
        }
    }
    

    【讨论】:

    • 其实就是toString的值,继承自Object类。
    • @Stultuske 是的,但我不想将继承拖入解释中。这本身就是一个完全不同的话题
    • @Toerktumlare 您已经在其中拖动继承,建议覆盖 toString。原始打印类的名称@HexValueOf(hashCode) docs.oracle.com/javase/10/docs/api/java/lang/…
    • @Stultuske 如果您对我的回答不满意,请随意编写自己的回答。
    【解决方案2】:

    在您的 for-loop 中迭代您的 Item 列表(也许您应该使用多态性重新定义您的 ArrayList),您每次都会得到一个对象 item 而不是详细信息,因此选择您的执行变量的名称

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

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


    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 + "]";
        }
    }
    

    希望这会对您有所帮助。如果它为您带来更多详细信息,请确保您对答案投赞成票!

    【讨论】:

    • 如果有帮助,请务必更新答案!
    • 这比公认的答案有何贡献?问题不在于任何“优化”。这些优化的基准又在哪里?
    • 带来更多价值是“主观的”,栈溢出不是论坛。它是一个问答网站。人们提出具体的问题,并得到具体的答案。此提供的答案中的“优化”是主观的,与实际提出的问题无关。
    • 不,还有关于学习一些概念以及为什么使用这些概念的问题。这不仅仅是弹出给你的解决方案:)。
    • 您是否注意到观看人数很重要?它还涉及有多少人会解决这个问题,有多少人会找到解决问题的方法,而不仅仅是发布问题的人。让我们在接下来的 6 个月内检查结果:)。
    猜你喜欢
    • 2015-07-22
    • 2019-04-19
    • 2011-02-02
    • 2016-06-04
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2018-08-04
    相关资源
    最近更新 更多