我试图打印出一个列表的所有元素,但它是打印对象的指针,而不是值。
这是我的打印代码…
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));
}
有人能告诉我为什么它不打印元素的值吗?
当前回答
下面的代码很紧凑,避免了示例代码中的循环(并提供了漂亮的逗号):
System.out.println(Arrays.toString(list.toArray()));
然而,正如其他人指出的那样,如果您没有为列表中的对象实现合理的toString()方法,您将得到您所观察到的对象指针(实际上是哈希码)。无论它们是否在列表中,都是如此。
其他回答
我写了一个转储函数,它基本上打印出对象的公共成员,如果它没有覆盖toString()。可以很容易地将其扩展为调用getter。 Javadoc:
将给定对象转储到系统。Out,使用以下规则: 如果对象是Iterable,则它的所有组件都将被转储。 如果Object或它的一个超类覆盖了toString(),则“toString”将被转储 否则,该方法将为对象的所有公共成员递归调用
/**
* Dumps an given Object to System.out, using the following rules:<br>
* <ul>
* <li> If the Object is {@link Iterable}, all of its components are dumped.</li>
* <li> If the Object or one of its superclasses overrides {@link #toString()}, the "toString" is dumped</li>
* <li> Else the method is called recursively for all public members of the Object </li>
* </ul>
* @param input
* @throws Exception
*/
public static void dump(Object input) throws Exception{
dump(input, 0);
}
private static void dump(Object input, int depth) throws Exception{
if(input==null){
System.out.print("null\n"+indent(depth));
return;
}
Class<? extends Object> clazz = input.getClass();
System.out.print(clazz.getSimpleName()+" ");
if(input instanceof Iterable<?>){
for(Object o: ((Iterable<?>)input)){
System.out.print("\n"+indent(depth+1));
dump(o, depth+1);
}
}else if(clazz.getMethod("toString").getDeclaringClass().equals(Object.class)){
Field[] fields = clazz.getFields();
if(fields.length == 0){
System.out.print(input+"\n"+indent(depth));
}
System.out.print("\n"+indent(depth+1));
for(Field field: fields){
Object o = field.get(input);
String s = "|- "+field.getName()+": ";
System.out.print(s);
dump(o, depth+1);
}
}else{
System.out.print(input+"\n"+indent(depth));
}
}
private static String indent(int depth) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<depth; i++)
sb.append(" ");
return sb.toString();
}
这取决于List中存储的对象类型,以及它是否有toString()方法的实现。System.out.println(list)应该打印所有标准java对象类型(String, Long, Integer等)。在这种情况下,如果我们使用自定义对象类型,那么我们需要重写自定义对象的toString()方法。
例子:
class Employee {
private String name;
private long id;
@Override
public String toString() {
return "name: " + this.name() +
", id: " + this.id();
}
}
测试:
class TestPrintList {
public static void main(String[] args) {
Employee employee1 =new Employee("test1", 123);
Employee employee2 =new Employee("test2", 453);
List<Employee> employees = new ArrayList(2);
employee.add(employee1);
employee.add(employee2);
System.out.println(employees);
}
}
用于String数组的列表
list.forEach(s -> System.out.println(Arrays.toString((String[]) s )));
下面的代码很紧凑,避免了示例代码中的循环(并提供了漂亮的逗号):
System.out.println(Arrays.toString(list.toArray()));
然而,正如其他人指出的那样,如果您没有为列表中的对象实现合理的toString()方法,您将得到您所观察到的对象指针(实际上是哈希码)。无论它们是否在列表中,都是如此。
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; } }