欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ArrayList的数据删除及排序

程序员文章站 2022-03-06 18:42:05
今天为萌新解决ArrayList删除数据的问题,同时引起了几个问题、关于ArrayList集合删删除数据使用迭代器、排序。话不多说,直接上代码演示。1,User.javapublic class User { private int uid; private String name; private String gylx; @Override public String toString() { return "User{" +...

今天为萌新解决ArrayList删除数据的问题,同时引起了几个问题、关于ArrayList集合删删除数据使用迭代器、排序。话不多说,直接上代码演示。

1,User.java

public class User {
    private int uid;
    private String name;
    private String gylx;

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", name='" + name + '\'' +
                ", gylx='" + gylx + '\'' +
                '}';
    }

    public User(int uid, String name, String gylx) {
        this.uid = uid;
        this.name = name;
        this.gylx = gylx;
    }

    public User() {
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGylx() {
        return gylx;
    }

    public void setGylx(String gylx) {
        this.gylx = gylx;
    }
}

2,MyqxinDemo01.java 测试类

public class MyqxinDemo01 {
    public static void main(String[] args) {
    	// 准备数据
        User user1 = new User(1, "小祖1", "001");
        User user2 = new User(2, "小祖2", "001");
        User user3 = new User(3, "小祖3", "002");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
    }
}

3,方式一,使用增强for删除(这个会报并发修改异常,不会再使用)

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖1", "001");
        User user2 = new User(2, "小祖2", "001");
        User user3 = new User(3, "小祖3", "002");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        run1(ulist);
    }


    public static void run1(ArrayList<User> list) {
        // TODO 使用增强for删除 gylx 为 "001" 的数据
        for (User user : list) {
            if ("001".equals(user.getGylx())){  // 防止空指针
                list.remove(user);
            }
        }
        System.out.println(list);
    }
}

异常如图:
ArrayList的数据删除及排序

4,方式二,普通for循环删除(这种方式虽然不会抛异常但是相同两个要删除的元素出现只能删除一个,会出现漏掉要删除的元素,不会再使用)

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖1", "001");
        User user2 = new User(2, "小祖2", "001");
        User user3 = new User(3, "小祖3", "002");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        run2(ulist);
    }


    public static void run2(ArrayList<User> list) {
        // TODO 使用普通for删除 gylx 为 "001" 的数据
        for (int i = 0; i < list.size(); i++) {
            if("001".equals(list.get(i).getGylx())){
                list.remove(i);
            }
        }
        System.out.println(list);
    }
}

结果如图:
ArrayList的数据删除及排序

5,方式三,迭代器删除(常用)

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖1", "001");
        User user2 = new User(2, "小祖2", "001");
        User user3 = new User(3, "小祖3", "002");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        run3(ulist);
    }


    public static void run3(ArrayList<User> list) {
        // TODO 使用迭代器f删除 gylx 为 "001" 的数据
        Iterator<User> iterator = list.iterator();
        while (iterator.hasNext()){     // 判断是否有下一个元素,不移动指针
            if ("001".equals(iterator.next().getGylx())){   // next() 向下移动指针,并返回指针指向的元素
                iterator.remove();     // 删除指针指向的元素
            }
        }
        System.out.println(list);
    }
}

结果如图:
ArrayList的数据删除及排序

关于ArrayList的排序

1,采用的默认的排序方式(升序)

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖1", "001");
        User user3 = new User(3, "小祖3", "002");
        User user2 = new User(2, "小祖2", "001");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        System.out.println(ulist);
    }
}

结果如图:
ArrayList的数据删除及排序

2,使用降序排序方式(默认规则)

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖3", "001");
        User user2 = new User(2, "小祖2", "001");
        User user3 = new User(3, "小祖1", "002");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        Collections.sort(ulist, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return -1;  // -1 降序  1 升序
            }
        });
        System.out.println(ulist);
    }
}

结果如图:
ArrayList的数据删除及排序

3,根据name排序(降序)

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖3", "001");
        User user2 = new User(2, "小祖2", "001");
        User user3 = new User(3, "小祖1", "002");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        Collections.sort(ulist, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o2.getName().compareTo(o1.getName());    // o2.compareTo(o1) 降序 o1.compareTo(o2)  升序
            }
        });
        System.out.println(ulist);
    }
}

结果如图:
ArrayList的数据删除及排序

4,根据gylx排序(降序–如果出现相同的两条数据,默认升序)

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖3", "001");
        User user2 = new User(2, "小祖2", "001");
        User user3 = new User(3, "小祖1", "002");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        Collections.sort(ulist, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o2.getGylx().compareTo(o1.getGylx());    // o2.compareTo(o1) 降序 o1.compareTo(o2)  升序
            }
        });
        System.out.println(ulist);
    }
}

结果如图:
ArrayList的数据删除及排序

5,根据uid排序(降序)

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖3", "001");
        User user2 = new User(2, "小祖2", "001");
        User user3 = new User(3, "小祖1", "002");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        Collections.sort(ulist, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o2.getUid() - o1.getUid();    // o2-o1 降序 o1-o2 升序
            }
        });
        System.out.println(ulist);
    }
}

结果如图:
ArrayList的数据删除及排序

6,升序

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖3", "777");
        User user2 = new User(2, "小祖2", "999");
        User user3 = new User(3, "小祖1", "888");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        ulist.sort(Comparator.comparingInt(o -> Integer.parseInt(o.getGylx())));
        System.out.println(ulist);
    }
}

7,降序

public class MyqxinDemo01 {
    public static void main(String[] args) {
        User user1 = new User(1, "小祖3", "777");
        User user2 = new User(2, "小祖2", "999");
        User user3 = new User(3, "小祖1", "888");
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(user1);
        ulist.add(user2);
        ulist.add(user3);
        ulist.sort((o1,o2) -> Integer.parseInt(o2.getGylx()) - Integer.parseInt(o1.getGylx()));  //o2-o1 降序 o1-o2 升序
        System.out.println(ulist);
    }
}

本文地址:https://blog.csdn.net/qq_45752401/article/details/111941461

相关标签: java基础 java