List接口的子类
【Java基础】集合框架
【Java基础】Collection子接口:List
ArrayList
- 底层数据结构是数组,查询快,增删慢;
- 线程不安全,效率高;
ArrayList的案例
1、存储字符串并遍历
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo1 {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//把字符串添加到集合
array.add("hello");
array.add("world");
array.add("java");
//遍历集合
for (int i = 0; i < array.size(); i++) {
String s = array.get(i);
System.out.println(s);
}
}
}
2、存储自定义对象并遍历
//定义学生类Student-->省略
//测试类:
import java.util.ArrayList;
import java.util.Iterator;
class ArrayListDemo2 {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("张三", 18);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王五", 22);
Student s4 = new Student("赵六", 24);
//把学生添加到集合
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//遍历集合
Iterator<Student> it = array.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
}
}
ArrayList的练习
1、去除集合中字符串的重复值(字符串的内容相同)
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListTest1 {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//添加多个字符串元素(包含内容相同的)
array.add("hello");
array.add("world");
array.add("world");
array.add("world");
array.add("java");
array.add("world");
array.add("java");
//创建新集合
ArrayList<String> newArray = new ArrayList<String>();
//遍历旧集合,获取得到每一个元素
Iterator<String> it = array.iterator();
while (it.hasNext()) {
String s = it.next();
//拿这个元素到新集合去找,看有没有
if (!newArray.contains(s)) {
//没有就添加到新集合
newArray.add(s);
}
}
//遍历新集合
for (int i = 0; i < newArray.size(); i++) {
String s = (String) newArray.get(i);
System.out.println(s);
}
}
}
要求: 不能创建新集合,就在原集合上操作;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListTest1 {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//添加多个字符串元素(包含内容相同的)
array.add("hello");
array.add("world");
array.add("world");
array.add("world");
array.add("java");
array.add("world");
array.add("java");
//拿0索引的依次和后面的比较,有就把后的干掉
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<String> it = array.iterator<String>();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
2、去除集合中自定义对象的重复值(对象的成员变量值都相同)
注意: contains()方法的底层依赖的是equals()方法,而学生类中没有equals()方法,默认使用的是Object的equals()方法;Object()的equals()默认比较的是地址值,按照需求应比较成员变量的值,就需要重写equals()方法(自动生成即可);
//定义学生类Student-->省略
//测试类
class ArrayListTest2 {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("张三", 18);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王五", 22);
Student s4 = new Student("赵六", 24);
Student s5 = new Student("张三", 18);
Student s1 = new Student("张三", 18);
Student s6 = new Student("李四", 22);
//把学生添加到集合
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//创建新集合
ArrayList<Student> newArray = new ArrayList<Student>();
//遍历旧集合,获取得到每一个元素
Iterator<Student> it = array.iterator<Student>();
while (it.hasNext()) {
String s = it.next();
//拿这个元素到新集合去找,看有没有
if (!newArray.contains(s)) {
//没有就添加到新集合
newArray.add(s);
}
}
//遍历新集合
for (int i = 0; i < newArray.size(); i++) {
Student s = newArray.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
}
}
Vector
- 底层数据结构是数组,查询快,增删慢;
- 线程安全,效率低;
Vector的特有功能
(1)添加功能
public void addElement(E obj) --> add()
(2)获取功能
public E elementAt(int index) --> get()
public Enumeration<E> elements() --> Iterator iterator() //返回的是实现类的对象
boolean hasMoreElements() --> boolean hasNext()
E nextElement() --> E next()
Vector的案例
1、存储字符串并遍历
import java.util.Vector;
public class VectorDemo1 {
public static void main(String[] args) {
//创建集合对象
Vector<String> v = new Vector<String>();
//把字符串添加到集合
v.addElement("hello");
v.addElement("world");
v.addElement("java");
//遍历集合
for (int i = 0; i < v.size(); i++) {
String s = v.elementAt(i);
System.out.println(s);
}
}
}
2、存储自定义对象并遍历
//创建学生类Student-->省略
//测试类:
import java.util.Enumeration;
import java.util.Vector;
class VectorDemo2 {
public static void main(String[] args) {
//创建集合对象
Vector<Student> c = new Vector<Student>();
//创建学生对象
Student s1 = new Student("张三", 18);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王五", 22);
Student s4 = new Student("赵六", 24);
//把学生添加到集合
v.addElement(s1);
v.addElement(s2);
v.addElement(s3);
v.addElement(s4);
//遍历集合
Enumeration<Student> en = v.elements();
while (en.hasMoreElements()) {
Student s = en.nextElement();
System.out.println(s);
}
}
}
LinkedList
- 底层数据结构是链表,查询慢,增删快;
- 线程不安全,效率高;
LinkedList的特有功能
(1)添加功能
public void addFirst(E e)
public void addLast(E e)
(2)获取功能
public E getFirst()
public E getLast()
(3)删除功能
public E removeFirst()
public E remoceLast()
LinkedList的案例
1、存储字符串并遍历
import java.util.LinkedList;
import java.util.Iterator;
public class LinkedListDemo1 {
public static void main(String[] args) {
//创建集合对象
LinkedList<String> link = new LinkedList<String>();
//把字符串添加到集合
link.add("hello");
link.add("world");
link.add("java");
//遍历集合
Iterator<String> it = link.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s);
}
}
}
2、存储自定义对象并遍历
//定义学生类Student-->省略
//测试类:
import java.util.LinkedList;
import java.util.Iterator;
class LinkedListDemo2 {
public static void main(String[] args) {
//创建集合对象
LinkedList<Student> link = new LinkedList<Student>();
//创建学生对象
Student s1 = new Student("张三", 18);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王五", 22);
Student s4 = new Student("赵六", 24);
//把学生添加到集合
link.add(s1);
link.add(s2);
link.add(s3);
link.add(s4);
//遍历集合
Iterator<Student> it = link.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
}
}
LinkedList的练习
请用LinkedList模拟栈数据结构的集合并测试
注意: 要定义一个集合类,只不过内部可以使用LinkedList来实现;
自定义的栈集合:
//自定义的栈集合类:
import java.util.LinkedList;
public class MyStack {
private LinkedList link;
public MyStack() {
link = new LinkedList();
}
public void add(Object obj) {
link.addFirst(obj);
}
public Object get() {
return link.removeFirst();
}
public boolean isEmpty() {
return link.isEmpty();
}
}
自定义的栈集合的测试:
//自定义的栈集合的测试类:
public class MyStackDemo {
public static void main(String[] args) {
MyStack ms = new MyStack();
ms.add("hello");
ms.add("world");
ms.add("java");
while(!ms.isEmpty()){
System.out.println(ms.get());
}
}
}