1.线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结 构,常见的线性表:顺序表、链表、栈、队列... 线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物 理上存储时,通常以数组和链式结构的形式存储。

2.顺序表
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
2.1接口的实现
import java.util.Arrays;
public class MyArrayList {
private int[] arr;
private int usedSize;
private static final int DEFAULT_SIZE = 10;
public int getUsedSize() {
return this.usedSize;
}
public MyArrayList() {
this.arr = new int[DEFAULT_SIZE];
}
public void add(int val) {
//检测容量,不够则扩容
if(getUsedSize() == arr.length) {
arr = Arrays.copyOf(this.arr, this.arr.length * 2);
}
this.arr[usedSize] = val;
this.usedSize++;
}
public void add(int val, int pos) throws IndexExpection{
//检测pos位置是否合法
if(pos < 0 || pos > getUsedSize()) {
throw new IndexExpection("下标异常");
}
//检测容量
if(getUsedSize() == arr.length) {
this.arr = Arrays.copyOf(this.arr, this.arr.length * 2);
}
for(int i = getUsedSize() - 1; i >= pos; i--) {
this.arr[i + 1] = this.arr[i];
}
this.arr[pos] = val;
this.usedSize++;
}
public int indexOf(int val) throws EmptyExpection{
if(isEmpty()) {
throw new EmptyExpection("空异常");
}
for(int i = 0; i < getUsedSize(); i++) {
if(this.arr[i] == val) {
return i;
}
}
return -1;
}
public void remove(int val) throws EmptyExpection{
//判空
if(isEmpty()) {
throw new EmptyExpection("空异常");
}
int index = indexOf(val);
if(index == -1) {
System.out.println("没有这个数据");
return;
}
for(int i = index; i < getUsedSize() - 1; i++) {
this.arr[i] = this.arr[i + 1];
}
this.usedSize--;
}
public void clear() throws EmptyExpection{
if(isEmpty()) {
throw new EmptyExpection("空异常");
}
this.usedSize = 0;
}
public boolean contains(int val) throws EmptyExpection{
if(isEmpty()) {
throw new EmptyExpection("空异常");
}
for(int i = 0; i < getUsedSize(); i++) {
if(this.arr[i] == val) {
return true;
}
}
return false;
}
public void set(int pos, int val) throws IndexExpection, EmptyExpection{
if(isEmpty()) {
throw new EmptyExpection("空异常");
}
if(pos < 0 || pos >= getUsedSize()) {
throw new IndexExpection("下标异常");
}
this.arr[pos] = val;
}
public int get(int pos) throws IndexExpection, EmptyExpection{
if(isEmpty()) {
throw new EmptyExpection("空异常");
}
if(pos < 0 || pos >= getUsedSize()) {
throw new IndexExpection("下标异常");
}
return this.arr[pos];
}
public boolean isEmpty() {
return getUsedSize() == 0;
}
public void display() throws EmptyExpection{
if(isEmpty()) {
throw new EmptyExpection("空异常");
}
for(int i = 0; i < getUsedSize(); i++) {
System.out.print(this.arr[i] + " ");
}
}
}
3.ArrayList简介
在集合框架中,ArrayList是一个普通的类,实现了List接口,具体框架图如下:

【说明】
1. ArrayList实现了RandomAccess接口,表明ArrayList支持 随机访问
2. ArrayList实现了Cloneable接口,表明ArrayList是 可以clone的
3. ArrayList实现了Serializable接口,表明ArrayList是 支持序列化的
4. 和Vector不同, ArrayList不是线程安全的,在单线程下可以使用, 在多线程中可以选择Vector或者 CopyOnWriteArrayList
5. ArrayList底层是一段连续的空间,并且可以 动态扩容,是一个动态类型的顺序表
4.ArrayList使用
4.1ArrayList的构造
方法 | 解释 |
ArrayList( ) | 无参构造 |
ArrayList(Collection<? extends E> c) | 利用其他 Collection 构建 ArrayList |
ArrayList(int initialCapacity) | 指定顺序表初始容量 |
public static void main(String[] args) {
// ArrayList创建,推荐写法
// 构造一个空的列表
List<Integer> list1 = new ArrayList<>();
// 构造一个具有10个容量的列表
List<Integer> list2 = new ArrayList<>(10);
list2.add(1);
list2.add(2);
list2.add(3);
// list2.add("hello"); // 编译失败,List<Integer>已经限定了,list2中只能存储整形元素
// list3构造好之后,与list中的元素一致
ArrayList<Integer> list3 = new ArrayList<>(list2);
// 避免省略类型,否则:任意类型的元素都可以存放,使用时将是一场灾难
List list4 = new ArrayList();
list4.add("111");
list4.add(100);
}
4.2ArrayList常见操作
方法 | 解释 |
boolean add(E e) | 尾插 e |
void add(int index, E element) | 将 e 插入到 index 位置 |
boolean addAll(Collection<? extends E> c) | 尾插 c 中的元素 |
E remove(int index) | 删除 index 位置元素 |
boolean remove(Object o) | 删除遇到的第一个 o |
E get(int index) | 获取下标 index 位置元素 |
E set(int index, E element) | 将下标 index 位置元素设置为 element |
void clear() | 清空 |
boolean contains(Object o) | 判断 o 是否在线性表中 |
int indexOf(Object o) | 返回第一个 o 所在下标 |
int lastIndexOf(Object o) | 返回最后一个 o 的下标 |
List<E> subList(int fromIndex, int toIndex) | 截取部分 list |
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("JavaSE");
list.add("JavaWeb");
list.add("JavaEE");
list.add("JVM");
list.add("测试课程");
System.out.println(list);
// 获取list中有效元素个数
System.out.println(list.size());
// 获取和设置index位置上的元素,注意index必须介于[0, size)间
System.out.println(list.get(1));
list.set(1, "JavaWEB");
System.out.println(list.get(1));
// 在list的index位置插入指定元素,index及后续的元素统一往后搬移一个位置
list.add(1, "Java数据结构");
System.out.println(list);
// 删除指定元素,找到了就删除,该元素之后的元素统一往前搬移一个位置
list.remove("JVM");
System.out.println(list);
// 删除list中index位置上的元素,注意index不要超过list中有效元素个数,否则会抛出下标越界异常
list.remove(list.size() - 1);
System.out.println(list);
// 检测list中是否包含指定元素,包含返回true,否则返回false
if (list.contains("测试课程")) {
list.add("测试课程");
}
// 查找指定元素第一次出现的位置:indexOf从前往后找,lastIndexOf从后往前找
list.add("JavaSE");
System.out.println(list.indexOf("JavaSE"));
System.out.println(list.lastIndexOf("JavaSE"));
// 使用list中[0, 4)之间的元素构成一个新的ArrayList返回
List<String> ret = list.subList(0, 4);
System.out.println(ret);
list.clear();
System.out.println(list.size());
}
注意:subList方法返回的只是一个视图,是一个对源列表的映射。这意味着如果对subList返回的列表进行编辑操作,源列表也会收到影响。
4.3ArrayList的遍历
三种遍历方式: for循环+下标、foreach、使用迭代器
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
// 使用下标+for遍历
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
// 借助foreach遍历
for (Integer integer : list) {
System.out.print(integer + " ");
}
System.out.println();
//迭代器1
ListIterator<Integer> it = list.listIterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
//迭代器2
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
}
ArrayList的扩容机制
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add(i);
}
}
ArrayList是一个动态类型的顺序表,即:在插入元素的过程中会自动扩容。
以下是ArrayList源码中扩容方式
Object[] elementData; // 存放元素的空间
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; // 默认空间
private static final int DEFAULT_CAPACITY = 10; // 默认容量大小
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
// 获取旧空间大小
int oldCapacity = elementData.length;
// 预计按照1.5倍方式扩容
int newCapacity = oldCapacity + (oldCapacity >> 1);
// 如果用户需要扩容大小 超过 原空间1.5倍,按照用户所需大小扩容
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
// 如果需要扩容大小超过MAX_ARRAY_SIZE,重新计算容量大小
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// 调用copyOf扩容
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
// 如果minCapacity小于0,抛出OutOfMemoryError异常
if (minCapacity < 0)
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}
【总结】 1. 检测是否真正需要扩容,如果是调用grow准备扩容 2. 预估需要库容的大小 初步预估按照1.5倍大小扩容 如果用户所需大小超过预估1.5倍大小,则按照用户所需大小扩容 真正扩容之前检测是否能扩容成功,防止太大导致扩容失败 3. 使用copyOf进行扩容
[注意]
List<Integer> list = new ArrayList<>(); //顺序表大小为0
第一次add后大小变为了10
当放满10个元素后,扩容1.5倍
扑克牌
public class Poker {
private String suit; //花色
private int rank; //数字
public Poker(String suit, int rank) {
this.suit = suit;
this.rank = rank;
}
@Override
public String toString() {
return suit + " " + rank ;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}
public class Pokers {
public static final String[] SUITS = { "♥️", "♠", "♣", "♦" };
public static List<Poker> buyPokers() {
List<Poker> pokerList = new ArrayList<Poker>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
String suit = SUITS[i];
int rank = j;
Poker poker = new Poker(suit, rank);
pokerList.add(poker);
}
}
return pokerList;
}
public static void swap(List<Poker> pokerList, int i, int j) {
Poker tmp = pokerList.get(i);
pokerList.set(i, pokerList.get(j));
pokerList.set(j, tmp);
}
public static void shuffle(List<Poker> pokerList) {
Random random = new Random();
for (int i = pokerList.size()-1; i > 0; i--) {
// 生成随机数
int index = random.nextInt(i+1);
swap(pokerList, i, index);
}
}
public static void main(String[] args) {
List<Poker> pokerList = buyPokers();
System.out.println("买牌:" + pokerList);
shuffle(pokerList);
System.out.println("洗牌:" + pokerList);
// 揭牌
//描述3个人手中的牌
List<Poker> hand1 = new ArrayList<Poker>();
List<Poker> hand2 = new ArrayList<Poker>();
List<Poker> hand3 = new ArrayList<Poker>();
//如何区分 往哪个人手中发牌
List<List<Poker>> hand = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
//hand.get
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
List<Poker> handTmp = hand.get(j);//来确定是谁的手
handTmp.add(pokerList.remove(0));//每次都是0下标
}
}
for (int i = 0; i < hand.size(); i++) {
System.out.println("第"+(i+1)+"个人的牌是:"+hand.get(i));
}
System.out.println("剩余的牌:"+pokerList);
}
}
杨辉三角
import java.util.ArrayList;
import java.util.List;
public class Test {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
list1.add(1);
ret.add(list1);
// 上述第一行只有一个1,已经添加到ret中,接下来从第二行开始
for (int i = 1; i < numRows; i++) {
// 当前行
List<Integer> curRow = new ArrayList<>();
curRow.add(1);
List<Integer> preRow = ret.get(i-1);//前一行
for (int j = 1; j < i; j++) {
int num = preRow.get(j)+preRow.get(j-1);
curRow.add(j, num);
//不能用set()
}
curRow.add(1);
ret.add(curRow);
}
return ret;
}
public static void main(String[] args) {
Test test = new Test();
int numRows = 10;
List<List<Integer>> lists = test.generate(numRows);
for (int i = 0; i < numRows ; i++) {
List<Integer> list = lists.get(i);
System.out.println(list);
}
}
}