本文共 1676 字,大约阅读时间需要 5 分钟。
public static void main(String[] args) { String [] strs = {"a","b","c","d"}; Test t = new Test(); System.out.println(t.ifcontains(strs, "a")); System.out.println(t.ifcontainsByArrays(strs, "a")); } public boolean ifcontains(String [] strs, String str) { for (String s : strs) { if (s.equals(str)) { return true; } } return false; } public boolean ifcontainsByArrays(String [] strs, String str) { // 注意:Arrays.asList()方法返回的是 java.util.Arrays.ArrayList而不是java.util.ArrayList return Arrays.asList(strs).contains(str); } |
public static void main(String[] args) { List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d")); List<String> list1 = new ArrayList<String>(Arrays.asList("a", "b", "c", "d")); Test1 t = new Test1(); for (String s : t.removeByIterator(list, "c")) { System.out.print(s); } System.out.println(); System.out.println("----------------------------"); for (String s : t.removeByNewList(list1, "c")) { System.out.print(s); } } public List<String> removeByNewList(List<String> list, String str) { List<String> newList = new ArrayList<String>(); for (String s : list) { if (!s.equals(str)) { newList.add(s); } } return newList; } public List<String> removeByIterator(List<String> list, String str) { Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String s = iterator.next(); if (s.equals(str)) { // 移除迭代器返回的元素 iterator.remove(); } } return list; } |
转载地址:http://foncx.baihongyu.com/