该方法最终会调用java.util.AbstractCollection.toString()方法,遍历list中的元素 方法2) System.out.println(Arrays.toString(list.toArray())); 方法3)使用org.apache.commons.lang...
[size=small]方法1) System.out.println(list);
该方法最终会调用java.util.AbstractCollection.toString()方法,遍历list中的元素
方法2) System.out.println(Arrays.toString(list.toArray()));
方法3)使用org.apache.commons.lang.builder.ToStringBuilder.reflectionToString()
Assists in implementing Object.toString() 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() {
return ToStringBuilder.reflectionToString(this);
}
You can also use the builder to debug 3rd party objects:
System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));
The exact format of the toString is determined by the ToStringStyle passed into the constructor.
Since:
Version:
$Id: ToStringBuilder.java 905636 2010-02-02 14:03:32Z niallp $
Author:
Apache Software Foundation
Gary Gregory
Pete Gieser
[/size]
本文主要介绍,java中list集合移除元素的那些坑,今天小编就和大家一起来了解一下吧!一、问题案例1.1、for循环移除public static void main...
本文主要介绍,java中list集合移除元素的那些坑,今天小编就和大家一起来了解一下吧!
一、问题案例
1.1、for循环移除
public static void main(String[] args) {	Listlist = new ArrayList();	list.add("11");	list.add("11");	list.add("12");	list.add("13");	list.add("14");	list.add("15");	list.add("16");	System.out.println("原始list元素:"+ list.toString());	CopyOnWriteArrayListcopyList = new CopyOnWriteArrayList