首页  >  文章  >  Java  >  Java 实例 - List 元素替换

Java 实例 - List 元素替换

黄舟
黄舟原创
2017-01-22 15:16:521837浏览

以下实例演示了如何使用 Collections 类的 replaceAll() 来替换List中所有的指定元素:

/*
 author by w3cschool.cc
 Main.java
 */

import java.util.*;

public class Main {
   public static void main(String[] args) {
      List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
      System.out.println("List :"+list);
      Collections.replaceAll(list, "one", "hundread");
      System.out.println("replaceAll: " + list);
   }
}

以上代码运行输出结果为:

List :[one, Two, three, Four, five, six, one, three, Four]
replaceAll: [hundread, Two, three, Four, five, six, hundread, three, Four]

以上就是Java 实例 - List 元素替换的内容,更多相关内容请关注PHP中文网(www.php.cn)!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn