java打印list_java打印list中的元素
[size=small]⽅法1) System.out.println(list);
该⽅法最终会调⽤java.String()⽅法,遍历list中的元素
⽅法2) System.out.Array()));
⽅法3)使⽤org.apachemons.lang.flectionToString()
Assists in String() methods.
This class enables a good and consistent toString() to be built for any class or object. This class aims to simplify the process by:
allowing field names
handling all types consistently
handling nulls consistently
outputting arrays and multi-dimensional arrays
enabling the detail level to be controlled for Objects and Collections
handling class hierarchies
To use this class write code as follows:
public class Person {
String name;
int age;
boolean smoker;
...
public String toString() {
return new ToStringBuilder(this).
append("name", name).
append("age", age).
append("smoker", smoker).
toString();
}
}
This will produce a toString of the format: Person@7f54[name=Stephen,age=29,smoker=false]
To add the superclass toString, use appendSuper. To append the toString from an object that is delegated to (or any other object), use appendToString.
Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are usually private, the method, reflectionToString, uses AccessibleObject.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than testing explicitly.
A typical invocation for this method would look like:
public String toString() {
flectionToString(this);
}
You can also use the builder to debug 3rd party objects:
System.out.println("An object: " + flectionToString(anObject));
The exact format of the toString is determined by the ToStringStyle passed into the constructor. Since:
1.0
Version:
$Id: ToStringBuilder.java 905636 2010-02-02 14:03:32Z niallp $
Author:
java的tostring方法Apache Software Foundation
Gary Gregory
Pete Gieser
[/size]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。