转自:
Java Collections.replaceAll()方法具有什么功能呢?
下文笔者讲述Collections.replaceAll()方法的功能简介说明,如下所示:
Collections.replaceAll()方法的功能
用于替换集合中的元素
Collections.replaceAll()方法的语法
public static boolean replaceAll(List l, Type old_ele, Type new_ele);
参数
列表l:待进行替换的列表
Type old_ele:将列表中替换的旧元素
Type new_ele:将列表中替换旧元素的新元素
例:
package com.java265.other;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test15 {
/**
* java265.com 示例 Collections.replaceAll方法示例分享
*/
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(11);
list.add(22);
list.add(33);
list.add(44);
// 显示ArrayList
System.out.println("Array List: " + list);
Collections.replaceAll(list, 44, 1200);
// 反转的list
System.out.println("Collections.reverse(list): " + list);
}
}
------运行以上代码,将输出以下信息------
Array List: [11, 22, 33, 44]
Collections.reverse(list): [11, 22, 33, 1200]