ArrayList集合_ArrayList

集合常用操作
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class list3 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(5);
list.add(5);
System.out.println(list);
//添加元素
list.add("50");
list.add(.5);
list.add(0,"java");
list.addAll(1,List.of("aa","bb","cc"));
System.out.println(list);
//删除所有
//list.clear();
Integer n = 5;
//删除下标为5的元素
// list.remove(5);
// System.out.println(list);
// list.remove(n);
// System.out.println(list);
//删除集合中所有5
// list.removeAll(List.of(5));
// System.out.println(list);
//删除集合中所有5
// list.removeIf(e-> e==(Integer) 5);
// System.out.println(list);
//删除集合中的指定字符串
list.removeIf(e->e instanceof String && e.toString().contains("aa"));
System.out.println(list);
//替换元素
list.set(1,500);
System.out.println(list);
//判断元素是否存在
System.out.println(list.contains(5000));
//查询元素位置下标
System.out.println(list.indexOf("cc"));
//遍历数组
for(Object e: list){
System.out.println(e);
}
System.out.println("-----------------");
for(int i = 0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println("-----------------");
for(int i = list.size()-1;i>=0;i--){
System.out.println(list.get(i));
}
//只能读取不能修改
List<Integer> nums = List.of(1,2,3);
System.out.println(nums);

List<Double> ds = new ArrayList<>(List.of( 1d,2d,3d,1d,2d,3d,4d));
ds.add(4.5);
ds.addAll(List.of(10d,20d,30d));
System.out.println(ds);
System.out.println(ds.contains(30d));//contains:包含
//保留括号内的删除其他的
// ds.retainAll(List.of(1d,2d,3d));
// System.out.println(ds);
//方法一
//若集合中的数大于2,要乘以0.5,不大于输出原数
ds.replaceAll(a ->a >= 2 ? a* .5 : a);
System.out.println(ds);
////方法二
// for(int i = 0 ;i<ds.size();i++){
// if(ds.get(i) >=2){
// ds.set(i,ds.get(i) * .5);
// }
// }
// System.out.println(ds);

List<Integer>list2 = new ArrayList<>();
list2.add(111);
list2.add(222);
list2.add(333);
list2.add(444);
list2.addAll(List.of(555,666));
System.out.println(list2);
//方法一
Object[] n2 = list2.toArray();
System.out.println(n2);
System.out.println(Arrays.toString(n2));
//方法二 推荐使用
Integer[] n3 = new Integer[list2.size()];
list2.toArray(n3);
System.out.println(n3);
System.out.println(Arrays.toString(n3));

//将Integer数组,直接转换为List<Integer> 集合 再实例化,就是ArrayList
List<Integer> list3 = new ArrayList<>(Arrays.asList(n3));
list3.add(99);
System.out.println(list3);


}
}
排序(Collections工具类)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class List4 {
public static void main(String[] args) {
//集合字符串的乱序 升序 降序

List<String> list = new ArrayList<>(List.of("java","c","c++","go","ruby","javascrip","python"));
System.out.println("排序前:"+list);

//乱序
Collections.shuffle(list);
System.out.println("排乱序:"+list);
//升序
Collections.sort(list);
System.out.println("排升序:"+list);
//降序
Collections.sort(list,(a,b)->b.compareTo(a));
//Collections.sort(list, Comparator.reverseOrder());
System.out.println("排降序:"+list);
//按字符个数
Collections.sort(list,(a,b)->a.length()-b.length());
System.out.println("排升序:"+list);
Collections.sort(list,(a,b)->b.length()-a.length());
System.out.println("排降序:"+list);

//数字排序
List<Integer> list2 = new ArrayList<>(List.of(10,3,5,23,0,26,9));
System.out.println(list2);
//乱序
Collections.reverse(list2);
System.out.println(list2);
Collections.shuffle(list2);
System.out.println(list2);
//升序
Collections.sort((list2));
System.out.println(list2);
//降序
Collections.sort(list2,(a,b)->b-a);
System.out.println(list2);
}
}