Java Collections ReplaceAll()方法与示例

集合类replaceAll()方法 (Collections Class replaceAll() method)

  • replaceAll() method is available in java.util package.

    replaceAll()方法在java.util包中可用。

  • replaceAll() method is used to replace all occurrences of the given old element (old_ele) that exists in a List(l) with the given new element (new_ele).

    replaceAll()方法用于用给定的新元素( new_ele ) 替换 List( l )中存在的给定旧元素( old_ele )的所有出现。

  • replaceAll() method is a static method, so it is accessible with the class name and if we try to access this method with the class object then we will not get an error.

    replaceAll()方法是静态方法,因此可以使用类名进行访问,如果我们尝试使用类对象访问此方法,则不会收到错误。

  • replaceAll() method may throw an exception at the time of replacing the old element with the new element in a list.

    在列表中用新元素替换旧元素时, replaceAll()方法可能会引发异常。

    UnsupportedOperationException: This exception may throw when the given List(l) un-support set operation.

    UnsupportedOperationException :当给定的List( l )不支持set操作时,可能引发此异常。

Syntax:

句法:

    public static boolean replaceAll(List l, Type old_ele, Type new_ele);

Parameter(s):

参数:

  • List l – represents the list in which replacement of the given old_ele is to occur.

    列表l –表示要替换给定old_ele的列表。

  • Type old_ele – represents the old element to be replaced from the given list.

    类型old_ele –表示要从给定列表中替换的旧元素。

  • Type new_ele – represents the new element with which old element is to be replaced in the given list.

    类型new_ele –表示要在给定列表中替换旧元素的新元素。

Return value:

返回值:

The return type of this method is boolean, it returns true when the given old element (old_ele) is no more exists in the given list otherwise it returns false.

此方法的返回类型为boolean ,如果给定列表中不再存在给定的旧元素( old_ele ),则返回true,否则返回false。

Example:

例:

// Java program is to demonstrate the example of
// replaceAll(List l, Type old_ele, Type new_ele)
// method of Collections

import java.util.*;

public class ReplaceAllOfCollections {
    public static void main(String args[]) {
        // Here, we are creating list object
        List < Integer > l = new ArrayList < Integer > ();

        // By using add()method is to add
        // objects in a list 
        l.add(10);
        l.add(20);
        l.add(30);
        l.add(40);
        l.add(50);
        l.add(30);

        // Display list before replaceAll()
        System.out.println("List: " + l);

        // By using replaceAll() method is to
        // replace all 30 with 300 in a list
        Collections.replaceAll(l, 30, 300);

        // Display list after replaceAll()
        System.out.println("Collections.replaceAll(l,30,300): " + l);
    }
}

Output

输出量

List: [10, 20, 30, 40, 50, 30]
Collections.replaceAll(l,30,300): [10, 20, 300, 40, 50, 300]


翻译自: https://www.includehelp.com/java/collections-replaceall-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值