标签:out inf 集合 asn span 规则 get remove 图片
移除指定下标或相同对象(对象的equal方法判断)的第一个元素;有限精确匹配,传入数字优先判断下标
原理:List在remove的时候,就是调用equals方法来判断两个元素是否相等的,并且是调用remove方法参数的equals:
import java.util.ArrayList; /** * @ClassName ArrayListRemoveExample * @projectName: object1 * @author: Zhangmingda * @description: XXX * date: 2021/4/10. */ public class ArrayListRemoveExample { private static class TestObj{ @Override public boolean equals(Object obj) { return true; } } public static void main(String[] args) { ArrayList arrayList = new ArrayList(); arrayList.add("张三"); arrayList.add("李四"); arrayList.add("王五"); arrayList.add("赵六"); System.out.println(arrayList); arrayList.remove(1); //精确匹配原则,数字优先匹配下标 arrayList.remove("王五"); //其次匹配对象 System.out.println(arrayList); //测试移除原理,是调用需要移除对象的equal方法判断是否和已有元素相等 arrayList.remove(new TestObj()); System.out.println(arrayList); //张三被移除 } }
import java.util.ArrayList; import java.util.function.UnaryOperator; /** * @ClassName ArrayListReplaceAllExample * @projectName: object1 * @author: Zhangmingda * @description: XXX * date: 2021/4/10. */ public class ArrayListReplaceAllExample { public static void main(String[] args) { ArrayList arrayList = new ArrayList(); arrayList.add(1); arrayList.add(2); arrayList.add(3); //数据处理 arrayList.replaceAll( o -> { int tmpNum = (int)o; return tmpNum * tmpNum; }); System.out.println(arrayList); //[1, 4, 9] } }
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.NavigableMap; /** * @ClassName ArrayListSortExample * @projectName: object1 * @author: Zhangmingda * @description: XXX * date: 2021/4/10. */ public class ArrayListSortExample { public static void main(String[] args) { List nums = new ArrayList(); nums.add(3); nums.add(2); nums.add(4); nums.add(1); System.out.println(nums); //[3, 2, 4, 1] //自定义排序 nums.sort(new Comparator() { @Override public int compare(Object o, Object t1) { return (int)o - (int)t1; } }); System.out.println(nums); //[1, 2, 3, 4] nums.sort((o1, o2) -> (int)o2 - (int)o1); //lambda方式 System.out.println(nums); //[4, 3, 2, 1] } }
import java.util.ArrayList; import java.util.ListIterator; /** * @ClassName ArrayListListIteratorExample * @projectName: object1 * @author: Zhangmingda * @description: XXX * date: 2021/4/10. */ public class ArrayListListIteratorExample { public static void main(String[] args) { ArrayList persons = new ArrayList(); persons.add("张三"); persons.add("李四"); persons.add("王五"); ListIterator listIteratorPersons = persons.listIterator(); listIteratorPersons.add("赵六"); //游标位置0 System.out.println(persons);//[赵六, 张三, 李四, 王五] while (listIteratorPersons.hasNext()){ System.out.println(listIteratorPersons.next()); } listIteratorPersons.add("赵六");//游标位置: 对象.length-1 System.out.println(persons); //[赵六, 张三, 李四, 王五, 赵六] } }
List persons = Arrays.asList("李一桐", "刘亦菲", "鞠婧祎"); persons.forEach(person -> System.out.println(person)); persons.add("易烊千玺"); //这行代码直接报错
java 数据类型:集合接口Collection之List~ArrayList:remove移除;replaceAll改变原有值;sort排序;迭代器listIterator();
标签:out inf 集合 asn span 规则 get remove 图片
原文地址:https://www.cnblogs.com/zhangmingda/p/14640789.html