我想打印数组列表中的所有元素。 Eclipse没有显示错误,但是没有显示我在控制台中添加的元素。你能告诉我我做错了什么吗? 控制台显示:
Typ:Droide ID(标识号):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();
}
}
答案 0 :(得分:4)
当前,您的问题是HumanoiderRoboter
不会覆盖toString
方法,而该方法会导致HumanoiderRoboter@4e25154f
的问题。因此,如果您覆盖toString
方法,它将打印您放置在其中的对象内容:
...
@Override
public String toString() {
return "Typ: " + type + ", ID: " + id + ", Name: " + name;
}
...
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
因此,如果您现在执行System.out.println(theObject)
,例如将得到如下结果:
类型:一些,ID:5,名称:NiceRoboter
如果要将整个数组作为一个String
,则可以使用Arrays#toString
方法:
System.out.println(Arrays.toString(yourList.toArray()));
答案 1 :(得分: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/
中的命名约定答案 2 :(得分:-4)
覆盖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 + "]";
}
}