1 ArrayList类
1.1 ArrayList类概述
- 底层数据结构是数组,查询快,增加和删除慢。
- 线程不安全,效率高。
1.2 ArrayList的应用
- 示例:
package com.xuweiwei; import java.util.ArrayList; import java.util.List; public class ArrayListDemo1 { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("hello"); list.add("world"); list.add("java"); for (int i = 0; i < list.size(); i++) { String str = list.get(i); System.out.println(str); } } }
- 示例:
package com.xuweiwei; public class Student { private String name; private Integer age; public Student() { } public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
package com.xuweiwei; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListDemo2 { public static void main(String[] args) { List<Student> list = new ArrayList<>(); list.add(new Student("张三",20)); list.add(new Student("李四",22)); for(Iterator<Student> iterator = list.iterator();iterator.hasNext();){ Student student = iterator.next(); System.out.println(student); } } }
2 Vector的特有功能
- 方法:添加元素
public void addElement(E obj)
- 方法:返回指定索引处的元素
public E elementAt(int index)
- 方法:返回此集合的枚举
public Enumeration<E> elements()
- 示例:
package com.xuweiwei; import java.util.Vector; public class VectorDemo1 { public static void main(String[] args) { Vector<String> vector = new Vector<>(); vector.add("hello"); vector.add("java"); for (int i = 0; i < vector.size(); i++) { String str = vector.elementAt(i); System.out.println(str); } } }
- 示例:
package com.xuweiwei; import java.util.Enumeration; import java.util.Vector; public class VectorDemo2 { public static void main(String[] args) { Vector<String> vector = new Vector<>(); vector.add("hello"); vector.add("java"); for (Enumeration<String> enumeration = vector.elements(); enumeration.hasMoreElements(); ) { String str = enumeration.nextElement(); System.out.println(str); } } }
3 LinkedList的特有功能
- 方法:将指定的元素插入到链表的开头
public void addFirst(E e)
- 方法:将指定的元素插入到链表的结尾
public void addLast(E e)
- 方法:获取链表的第一个元素
public E getFirst()
- 方法:获取链表的最后一个元素
public E getLast()
- 方法:移除链表的第一个元素
public E removeFirst()
- 方法:移除链表的最后一个元素
public E removeLast()
- 示例:
package com.xuweiwei; import java.util.LinkedList; public class LinkedListDemo1 { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("hello"); list.add("world"); System.out.println(list); list.addFirst("java"); System.out.println(list); } }
- 示例:
package com.xuweiwei; import java.util.LinkedList; public class LinkedListDemo1 { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("hello"); list.add("world"); System.out.println(list); list.addLast("java"); System.out.println(list); } }
- 示例:
package com.xuweiwei; import java.util.LinkedList; public class LinkedListDemo1 { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("hello"); list.add("world"); System.out.println(list); System.out.println(list.getFirst()); } }
- 示例:
package com.xuweiwei; import java.util.LinkedList; public class LinkedListDemo1 { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("hello"); list.add("world"); System.out.println(list); System.out.println(list.getLast()); } }
- 示例:
package com.xuweiwei; import java.util.LinkedList; public class LinkedListDemo1 { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("hello"); list.add("world"); System.out.println(list); System.out.println(list.removeFirst()); System.out.println(list); } }
- 示例:
package com.xuweiwei; import java.util.LinkedList; public class LinkedListDemo1 { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("hello"); list.add("world"); System.out.println(list); System.out.println(list.removeLast()); System.out.println(list); } }
4 ArrayList去重
4.1 去除ArrayList中字符串的重复值
- 示例:
package com.xuweiwei; import java.util.ArrayList; import java.util.List; public class ArrayListDemo4 { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("aa"); list.add("ba"); list.add("b"); List<String> target = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { String str = list.get(i); if(!target.contains(str)){ target.add(str); } } //遍历target for (int i = 0; i < target.size(); i++) { String str = target.get(i); System.out.println(str); } } }
- 示例:
package com.xuweiwei; import java.util.ArrayList; import java.util.List; public class ArrayListDemo4 { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("aa"); list.add("ba"); list.add("b"); list.add("b"); list.add("b"); System.out.println(list); //选择排序思想 for (int x = 0; x < list.size() - 1; x++){ for(int y = x+1;y<list.size();y++){ if(list.get(x).equals(list.get(y)) ){ list.remove(y); y--; } } } System.out.println(list); } }
4.2 去除ArrayList中自定义对象的重复值(对象的成员变量值要都相同)
- 示例:
package com.xuweiwei; import java.util.Objects; public class Student { private String name; private Integer age; public Student() { } public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return Objects.equals(name, student.name) && Objects.equals(age, student.age); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
package com.xuweiwei; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListDemo5 { public static void main(String[] args) { List<Student> old = new ArrayList<>(); old.add(new Student("张三丰",20)); old.add(new Student("张三丰",20)); old.add(new Student("张三丰",21)); old.add(new Student("李四",21)); old.add(new Student("王五",21)); List<Student> newList = new ArrayList<>(); for(Iterator<Student> iterator = old.iterator();iterator.hasNext();){ Student stu = iterator.next(); if(!newList.contains(stu)){ newList.add(stu); } } System.out.println(newList); } }
5
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9338869.html
时间: 2024-11-05 14:37:46