【问题标题】:Print all elements of Array list [duplicate]打印数组列表的所有元素[重复]
【发布时间】:2019-01-25 12:04:37
【问题描述】:

我想打印我的数组列表的所有元素。 Eclipse 没有显示错误,但它没有显示我在控制台中添加的元素。你能告诉我我做错了什么吗? 控制台显示:

类型:机器人 编号:8282 名称R2D2 HumanoiderRoboter@15db9742 HumanoiderRoboter@6d06d69c HumanoiderRoboter@7852e922 HumanoiderRoboter@4e25154f

机器人类:

public class Roboter {
protected String Name;
protected int ID;
protected String typ;

public Roboter(String Name, int ID, String typ) {
    super();
    this.Name = Name;
    this.ID = ID;
    this.typ = typ;
}
public void ausgebenNeu() {
System.out.println("ID:"+ID);
System.out.println("Name:"+Name);
System.out.println("Typ:"+typ);

}

HumanoiderRoboter 类:

import java.util.ArrayList;

public class HumanoiderRoboter extends Roboter {

    String RoboterTyp;

    public HumanoiderRoboter (String Name, int ID, String typ) {    
        super(Name, ID, typ);   
    }

    public void ausgeben() {
        ArrayList<HumanoiderRoboter> Sensoren = new ArrayList<HumanoiderRoboter>();

        Sensoren.add(new HumanoiderRoboter("Sensor1", 4232, "Infrarotsensor"));
        Sensoren.add(new HumanoiderRoboter("Sensor2", 9232, "Lichtsensor"));
        Sensoren.add(new HumanoiderRoboter("Sensor3", 5777, "Touchssensor"));
        Sensoren.add(new HumanoiderRoboter("Sensor4", 3321, "Gyrosensor"));

        System.out.println("Typ:" + typ);
        System.out.println("ID:" + ID);
        System.out.println("Name" + Name);

        for (Roboter ele : Sensoren) {
            System.out.println(ele);
        }
    }

    public static void main(String[] args) {
        HumanoiderRoboter R2 = new HumanoiderRoboter("R2D2", 8282, "Droide");
        R2.ausgeben();

    }

}

【问题讨论】:

  • 将 toString() 方法添加到您的 HumanoiderRoboter
  • 但它做了什么
  • 你需要@override toString() 方法
  • 似乎您的 Roboter 和子类缺少适当的 toString() 方法来满足您的需求。那个 println() 方法不理解如何显示复杂的对象。
  • 在超类Roboter中添加String toString()方法

标签: java arrays element


【解决方案1】:

目前您的问题是 HumanoiderRoboter 不会覆盖导致 HumanoiderRoboter@4e25154f 东西的 toString 方法。因此,如果您覆盖 toString 方法,它将打印您放入其中的对象内容:

...
@Override
public String toString() {
    return "Typ: " + type + ", ID: " + id + ", Name: " + name;
}
...

Object 中的默认 toString 方法如下所示:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

所以现在如果你执行System.out.println(theObject),它会产生如下结果:

类型:some,ID:5,名称:NiceRoboter

如果您想将整个数组作为一个 String,您可以使用 Arrays#toString 方法:

System.out.println(Arrays.toString(yourList.toArray()));

【讨论】:

    【解决方案2】:

    在你的 Roboter 类中重写 toString() 方法,如下所示:

    public class Roboter {
    
      //-----member fields,methods
    
      //Add this method
      @Override
      public String toString(){
        return "{name:"+this.Name+"}";
      }
    }
    

    另请阅读此链接以了解 Java 中要遵循的命名约定 https://www.geeksforgeeks.org/java-naming-conventions/

    【讨论】:

    • 你知道toString 方法应该返回一个字符串吗?你的代码不会编译。
    • 抱歉,更新了我的答案。
    【解决方案3】:

    重写 Roboter 类中的 toString() 方法。

    public class Test extends Roboter {
    
        String RoboterTyp;
    
        public Test(String Name, int ID, String typ) {
            super(Name, ID, typ);
        }
    
        public void ausgeben() {
            ArrayList<Test> Sensoren = new ArrayList<Test>();
    
            Sensoren.add(new Test("Sensor1", 4232, "Infrarotsensor"));
            Sensoren.add(new Test("Sensor2", 9232, "Lichtsensor"));
            Sensoren.add(new Test("Sensor3", 5777, "Touchssensor"));
            Sensoren.add(new Test("Sensor4", 3321, "Gyrosensor"));
    
            System.out.println("Typ:" + typ);
            System.out.println("ID:" + ID);
            System.out.println("Name" + Name);
    
            for (Roboter ele : Sensoren) {
                System.out.println(ele);
            }
        }
    
        public static void main(String[] args) {
            Test R2 = new Test("R2D2", 8282, "Droide");
            R2.ausgeben();
    
        }
    
    }
    
    public class Roboter {
    
        String Name;
        int ID;
        String typ;
        public Roboter(String name, int iD, String typ) {
            super();
            Name = name;
            ID = iD;
            this.typ = typ;
        }
        public String getName() {
            return Name;
        }
        public void setName(String name) {
            Name = name;
        }
        public int getID() {
            return ID;
        }
        public void setID(int iD) {
            ID = iD;
        }
        public String getTyp() {
            return typ;
        }
        public void setTyp(String typ) {
            this.typ = typ;
        }
        @Override
        public String toString() {
           return "Roboter [Name=" + Name + ", ID=" + ID + ", typ=" + typ + "]";
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2014-05-05
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多