ArrayList中删除重复的字符串元素——ArrayList中删除重复的自定义对象元素
ArrayList???????????
发布日期:2021-04-30 21:12:19
浏览次数:90
分类:精选文章
本文共 3806 字,大约阅读时间需要 12 分钟。
ArrayList?????????????ArrayList?????????????
???ArrayList?????????????(????????)
??????????????????????
?????
package cn.itcast_01;import java.util.ArrayList;import java.util.Iterator;public class ArrayListTest { public static void main(String[] args) { // ?????? ArrayList array = new ArrayList(); // ?????????(???????) array.add("hello"); array.add("world"); array.add("java"); array.add("world"); array.add("java"); array.add("world"); array.add("world"); array.add("world"); array.add("world"); array.add("java"); array.add("world"); // ??????????????? for (int i = 0; i < array.size() - 1; i++) { for (int j = i + 1; j < array.size(); j++) { if (array.get(i).equals(array.get(j))) { array.remove(j); j--; } } } // ???? Iterator it = array.iterator(); while (it.hasNext()) { String s = (String) it.next(); System.out.println(s); } }} - ArrayList?????????????
- ?????????????????????
- ?????
- ?????????
- ????????????????????????
- ?????contains( )???????equals( )???
- ????Student????equals( )????????????????Object?equals( )???
- Object?equals( )??????????????????????
- ???????????????????equals( )??????????
?????????????????(???????????)
?????
?????
package cn.itcast_01;import java.util.ArrayList;import java.util.Iterator;public class ArrayListTest2 { public static void main(String[] args) { // ?????? ArrayList array = new ArrayList(); // ?????? Student s1 = new Student("???", 27); Student s2 = new Student("???", 40); Student s3 = new Student("??", 35); Student s4 = new Student("????", 18); Student s5 = new Student("??", 16); Student s6 = new Student("???", 27); Student s7 = new Student("???", 18); // ???? array.add(s1); array.add(s2); array.add(s3); array.add(s4); array.add(s5); array.add(s6); array.add(s7); // ????? ArrayList newArray = new ArrayList(); // ??????????????? Iterator it = array.iterator(); while (it.hasNext()) { Student s = (Student) it.next(); // ???????????????? if (!newArray.contains(s)) { newArray.add(s); } } // ???? for (int x = 0; x < newArray.size(); x++) { Student s = (Student) newArray.get(x); System.out.println(s.getName() + "---" + s.getAge()); } }}class Student { private String name; private int age; public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }} ????????????ArrayList?????????????????????????????????????????Student??equals??????????????????????????
发表评论
最新留言
表示我来过!
[***.240.166.169]2026年06月17日 05时27分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!