-2

i want to print all elements of my array list. Eclipse does not show an error, but it doesnt show the elements that i added in console. Can you please tell me what i did wrong? The console shows:

Typ:Droide ID:8282 NameR2D2 HumanoiderRoboter@15db9742 HumanoiderRoboter@6d06d69c HumanoiderRoboter@7852e922 HumanoiderRoboter@4e25154f

Roboter Class:

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 Class:

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();

    }

}
Amit Kumar
  • 2,685
  • 2
  • 37
  • 72
Lukas
  • 1
  • 1

3 Answers3

4

Currently your problem is the HumanoiderRoboter doesn't overwrite the toString method which results the HumanoiderRoboter@4e25154f stuff. So if you overwrite the toString method it will print your object stuff you put in there:

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

Default toString method from Object looks like that:

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

So now if you do System.out.println(theObject) it will for example result something like this:

Typ: some, ID: 5, Name: NiceRoboter

And if you want the complete array as one String you can use the Arrays#toString method:

System.out.println(Arrays.toString(yourList.toArray()));
CodeMatrix
  • 2,124
  • 1
  • 18
  • 30
2

In your Roboter class override toString() method like this:

public class Roboter {

  //-----member fields,methods

  //Add this method
  @Override
  public String toString(){
    return "{name:"+this.Name+"}";
  }
}

Also read this link for naming convention to follow in Java https://www.geeksforgeeks.org/java-naming-conventions/

Amit Kumar
  • 2,685
  • 2
  • 37
  • 72
-3

Override toString() method in Roboter class.

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

}
Sanjay
  • 2,481
  • 1
  • 13
  • 28