java打印list中的元素

[size=small]方法1) System.out.println(list);
该方法最终会调用java.util.AbstractCollection<E>.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:
1.0
Version:
$Id: ToStringBuilder.java 905636 2010-02-02 14:03:32Z niallp $
Author:
Apache Software Foundation
Gary Gregory
Pete Gieser
[/size]
### 如何在Java中从List移除元素Java中,`ArrayList` 是 `List` 接口的一个实现类,提供了多种方法来删除列表中的元素。以下是几种常见的操作方式: #### 使用 `remove(Object o)` 方法 此方法用于通过对象本身来移除第一个匹配项。如果找到并成功移除该对象,则返回 `true`;否则返回 `false`[^1]。 ```java import java.util.ArrayList; import java.util.List; public class RemoveExample { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); boolean isRemoved = list.remove("B"); // 移除指定的对象 "B" System.out.println(list); // 输出: [A, C] System.out.println(isRemoved); // 输出: true } } ``` #### 使用 `remove(int index)` 方法 当需要基于索引来移除某个特定位置上的元素时可以使用这个方法。它会将位于给定索引处的元素移除,并使后续元素向前移动一位以填补空缺的位置[^2]。 ```java import java.util.ArrayList; import java.util.List; public class IndexRemoveExample { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); int removedValue = numbers.remove(1); // 移除第二个元素 System.out.println(numbers); // 输出: [10, 30] System.out.println(removedValue); // 输出: 20 } } ``` #### 删除重复元素 为了清除集合内的所有副本只保留唯一实例,可以通过转换成 `HashSet` 来达成目标因为哈希集不允许存在任何两个相等成员之间共享相同键的情况发生从而达到去重效果后再变回原来的类型继续使用. ```java import java.util.*; public class DuplicateRemoval { public static void main(String[] args){ List<String> originalList = Arrays.asList("apple", "banana", "orange", "apple", "grape"); Set<String> setWithoutDuplicates = new HashSet<>(originalList); List<String> newListWithNoDupes = new ArrayList<>(setWithoutDuplicates); System.out.println(newListWithNoDupes); // 可能输出顺序不同如:[banana, grape, orange, apple] } } ``` #### 利用迭代器安全地遍历期间修改容器结构 为了避免并发修改异常(ConcurrentModificationException),应该采用显式的 Iterator 对象来进行循环访问的同时执行增删动作: ```java import java.util.*; public class SafeIterationDeletion{ public static void main(String []args){ List<Integer> nums=new LinkedList<>(); Collections.addAll(nums,1,2,3,4,5,-9,-8,-7); Iterator<Integer> iterator=nums.iterator(); while(iterator.hasNext()){ Integer num=iterator.next(); if(num<0){ iterator.remove();//仅在此调用有效不会引发错误 } } System.out.println(nums);//打印结果应该是正整数序列[1, 2, 3, 4, 5] } } ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值