**Collection** (集合上层接口)
1、存储单个数据值,多个元素的集合
2、常用方法
3、遍历方式
1)、foreach
2)、迭代器Iterator
Map 多个元素的集合,每个元素由键值对组成
List 接口
有序可重复
新增 : 一些根据索引操作的方法
添加null元素
1. 泛型: 规定集合存储数据的类型 | 规定数据的类型
2. 表示 : <数据类型> 数据类型只能为引用数据类型
3. 位置 : 定义在数据类型的后面
4. 优点 : 帮助增强程序的稳定性与可读性
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Class_List {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("wuhu");
list.add(null);
list.add(null);
list.add("aiya");
List<String > list2=List.of("1,3,3,4,5,0","再来一个");
list.addAll(1,list2);
System.out.println(list2);
System.out.println(list);
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.lastIndexOf(null));
System.out.println(list.indexOf("wuhu"));
System.out.println(list.indexOf(5));
System.out.println(list.isEmpty());
List<Integer> listof=List.of(1,2,3,34,5,6);
System.out.println(listof);
System.out.println(list);
list.remove(2);
System.out.println(list);
list.set(1,"直接修改");
System.out.println(list);
System.out.println(list.subList(1, 3));
System.out.println(list);
System.out.println(Arrays.toString(list.toArray()));
List<Object> test=new ArrayList<Object>();
test.add("wuh");
test.add(666);
test.add(1);
test.add(true);
System.out.println(test.get(1));
}
}
List 接口的实现类 : 有序 可重复
ArrayList
底层实现: 可变数组
特点:
根据索引做查询效率高
增删效率低,设计到新数组的创建,数据的拷贝等问题
应用场景: 大量做查询,少量做增删的时候适合使用ArrayList
扩容:
新增内容:
遍历方式:
联系:定义ArrayList,存储自定义的javabean类型学生类型数据,简单操作
package com.sxt.Class_Collection;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
public class Class001_ArrayList {
public static void main(String[] args) {
Student stu1=new Student("张三",1001,18,"男");
Student stu2=new Student("李四",1002,16,"女");
Student stu3=new Student("王五",1003,19,"男");
Student stu4=new Student("赵六",1004,20,"女");
Student stu5=new Student("田七",1005,19,"男");
List<Student> list= new ArrayList<Student>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
ListIterator listIterator= list.listIterator();
for (;listIterator.hasNext();){
System.out.println(list.get(listIterator.nextIndex()));
listIterator.next();
}
System.out.println(list.indexOf(stu3));
}
}
class Student{
private String name;
private int ID;
private int age;
private String sex;
public void sleep(){
System.out.println("我在睡觉");
}
public Student() { }
public Student(String name, int ID, int age, String sex) {
this.name = name;
this.ID = ID;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", ID=" + ID +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return ID == student.ID &&
age == student.age &&
Objects.equals(name, student.name) &&
Objects.equals(sex, student.sex);
}
@Override
public int hashCode() {
return Objects.hash(name, ID, age, sex);
}
}
LinkedList :
有序 可重复
底层结构: 双向链表
特点:
根据索引查询效率低(链表结构本身是不存在索引的,但是模拟索引)
做增删效率高
应用场景:
大量做增删少量做查询的时候适合使用
新增功能:
新增了一些可以直接操作头尾的方法
链表(数据以字节为单位):
单向链表: 数据+下一个数据地址(注:记录链表头节点)
双向链表: 上一个数据地址+数据+下一个数据地址(注:记录链表头节点和链表尾节点)
练习:自定MyLinkedList类,并实现简单操作:
package com.sxt.Class_Collection;
public class LinkedList_Test {
public static void main(String[] args) {
MyLinkedList list=new MyLinkedList();
System.out.println(list.size());
list.add("你好");
System.out.println(list.toString());
System.out.println(list.size());
list.add("不好");
System.out.println(list.toString());
System.out.println(list.size());
list.add("这样");
list.add("怎么样");
System.out.println(list.toString());
list.delete(2);
System.out.println(list.toString());
}
}
class MyLinkedList{
private Node head;
private int size;
public void add(Object value){
Node node=new Node(value,null);
if(head==null && size==0){
head=node;
size++;
return;
}
Node temp=head;
while (temp.getNext()!=null){
temp=temp.getNext();
}
temp.setNext(node);
size++;
}
public void delete(int index){
int count=0;
int arrive=index;
Node temp=head;
while (temp!=null){
if(count==arrive-1){
Node x=temp.getNext().getNext();
temp.setNext(x);
return;
}
temp=temp.getNext();
count++;
}
}
public int size(){
return size;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder("[");
Node temp=head;
while (temp!=null){
sb.append(temp.getData()+", ");
temp=temp.getNext();
}
String str=sb.substring(0,sb.length());
if(str.endsWith(", ")){
str= sb.substring(0,str.length()-2);
}
return str+"]";
}
public MyLinkedList() { }
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
class Node{
private Object data;
private Node next;
public Node() {
}
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
@Override
public String toString() {
return "Node{" +
"data=" + data +
", next=" + next +
'}';
}
}