我试图打印出一个列表的所有元素,但它是打印对象的指针,而不是值。
这是我的打印代码…
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
有人能告诉我为什么它不打印元素的值吗?
我试图打印出一个列表的所有元素,但它是打印对象的指针,而不是值。
这是我的打印代码…
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
有人能告诉我为什么它不打印元素的值吗?
当前回答
You haven't specified what kind of elements the list contains, if it is a primitive data type then you can print out the elements. But if the elements are objects then as Kshitij Mehta mentioned you need to implement (override) the method "toString" within that object - if it is not already implemented - and let it return something meaning full from within the object, example: class Person { private String firstName; private String lastName; @Override public String toString() { return this.firstName + " " + this.lastName; } }
其他回答
System.out.println(list);//toString() is easy and good enough for debugging.
AbstractCollection的toString()将非常干净和容易做到这一点。AbstractList是AbstractCollection的子类,因此不需要for循环,也不需要toArray()。
返回此集合的字符串表示形式。字符串表示形式由集合元素的列表组成 它们由迭代器返回的顺序,用方括号括起来 (“[]”)。相邻元素用字符“,”(逗号)分隔 和空间)。通过将元素转换为字符串 String.valueOf(对象)。
如果您正在使用列表中的任何自定义对象,例如Student,则需要重写它的toString()方法(重写这个方法总是好的)以获得有意义的输出
请看下面的例子:
public class TestPrintElements {
public static void main(String[] args) {
//Element is String, Integer,or other primitive type
List<String> sList = new ArrayList<String>();
sList.add("string1");
sList.add("string2");
System.out.println(sList);
//Element is custom type
Student st1=new Student(15,"Tom");
Student st2=new Student(16,"Kate");
List<Student> stList=new ArrayList<Student>();
stList.add(st1);
stList.add(st2);
System.out.println(stList);
}
}
public class Student{
private int age;
private String name;
public Student(int age, String name){
this.age=age;
this.name=name;
}
@Override
public String toString(){
return "student "+name+", age:" +age;
}
}
输出:
[string1, string2]
[student Tom age:15, student Kate age:16]
列表中的对象必须实现toString,以便将有意义的内容打印到屏幕上。
下面是一个快速测试,可以看出区别:
public class Test {
public class T1 {
public Integer x;
}
public class T2 {
public Integer x;
@Override
public String toString() {
return x.toString();
}
}
public void run() {
T1 t1 = new T1();
t1.x = 5;
System.out.println(t1);
T2 t2 = new T2();
t2.x = 5;
System.out.println(t2);
}
public static void main(String[] args) {
new Test().run();
}
}
当它执行时,打印到屏幕的结果是:
t1 = Test$T1@19821f
t2 = 5
因为T1没有重写toString方法,所以它的实例T1输出的结果不是很有用。另一方面,T2重写了toString,所以我们控制了它在I/O中使用时打印的内容,我们在屏幕上看到了一些更好的东西。
或者你可以简单地使用Apache Commons实用程序:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ArrayUtils.html#toString-java.lang.Object-
List<MyObject> myObjects = ...
System.out.println(ArrayUtils.toString(myObjects));
List<String> textList= messageList.stream()
.map(Message::getText)
.collect(Collectors.toList());
textList.stream().forEach(System.out::println);
public class Message {
String name;
String text;
public Message(String name, String text) {
this.name = name;
this.text = text;
}
public String getName() {
return name;
}
public String getText() {
return text;
}
}
list.stream().map(x -> x.getName()).forEach(System.out::println);