网站建设教程百度网盘做门户类网站报价

当前位置: 首页 > news >正文

网站建设教程百度网盘,做门户类网站报价,磁力搜索器下载,一级门户网站建设费用List、ArrayList与顺序表 List什么是List常用方法介绍List的使用 ArrayList与顺序表线性表顺序表接口的实现 ArrayList简介ArrayList的使用ArrayList的构造ArrayList的常见操作ArrayList的遍历ArrayList的扩容机制 ArrayList的具体使用杨辉三角简单的洗牌算法 ArrayList的问题及… List、ArrayList与顺序表 List什么是List常用方法介绍List的使用 ArrayList与顺序表线性表顺序表接口的实现 ArrayList简介ArrayList的使用ArrayList的构造ArrayList的常见操作ArrayList的遍历ArrayList的扩容机制 ArrayList的具体使用杨辉三角简单的洗牌算法 ArrayList的问题及思考 List 什么是List 在集合框架中List是一个接口继承自Collection。 Collection 也是一个接口该接口中规范了后序容器中常用的一些方法具体如下所示 Iterable 也是一个接口表示实现该接口的类是可以逐个元素进行遍历的具体如下 List的官方文档 在数据结构的角度看List就是一个线性表即n个具有相同类型元素的有限序列在该序列上可以进行增删查改以及变量等操作。 常用方法介绍 方法解释boolean add(E e)尾插 evoid add(int index, E element)将 e 插入到 index 位置boolean addAll(Collection? extends E c)尾插 c 中的元素E remove(int index)删除 index 位置元素boolean remove(Object o)删除遇到的第一个 oE get(int index)获取下标 index 位置元素E set(int index, E element)将下标 index 位置元素设置为 elementvoid clear()清空boolean contains(Object o)判断 o 是否在线性表中int indexOf(Object o)返回第一个 o 所在下标int lastIndexOf(Object o)返回最后一个 o 的下标List E subList(int fromIndex, int toIndex)截取部分 list List的使用 注意List是一个接口并不能直接用来实例化。 如果要使用必须去实例化List的实现类。在集合框架中ArrayList和LinkedList都实现了List接口。 ArrayList与顺序表 线性表 线性表(linear list)是n个具有相同特性的数据元素的有限序列。线性表实际上是一种在实际中广泛使用的数据结构常见的线性表有顺序表、链表、栈、队列… 线性表在逻辑上是线性结构也就是说是一条连续的直线但是在物理结构上并不是连续的线性表在物理上存储时通常以数组和链式结构的形式存储。
顺序表 顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构一般情况下采用数组存储在数组上完成数据的增删查改。 接口的实现 我们通过模拟实现来进一步学习ArrayList public interface IList {// 新增元素,默认在数组最后新增public void add(int data);// 在 pos 位置新增元素public void add(int pos, int data);// 判定是否包含某个元素public boolean contains(int toFind);// 查找某个元素对应的位置public int indexOf(int toFind);// 获取 pos 位置的元素public int get(int pos);// 给 pos 位置的元素设为 valuepublic void set(int pos, int value);//删除第一次出现的关键字keypublic void remove(int toRemove);// 获取顺序表长度public int size();// 清空顺序表public void clear();// 打印顺序表注意该方法并不是顺序表中的方法为了方便看测试结果给出的public void display(); }public class MyArrayList implements IList{public int[] array;public int usedSize;public static final int DEFAULT_CAPACITY 5;public MyArrayList() {this.array new int[DEFAULT_CAPACITY];} }接下来对接口中的方法进行重写 首先从最简单的打印顺序表和获取顺序表长度开始 Override public int size() {return this.usedSize; } Override public void display() {for (int i 0; i usedSize; i) {System.out.print(array[i] );} }接下来实现add(新增元素,默认在数组最后新增) 在实现之前我们需要思考该数组会不会已经放满了我们需要对其检查若是放满我们还需要对其扩容我们默认大小只有5代码并不是简简单单的插入元素这么简单。 public boolean isFull(int[] array){return this.usedSize array.length; }private void grow(){this.array Arrays.copyOf(this.array,this.array.length * 2); }Override public void add(int data) {if(isFull(this.array)){grow();}array[usedSize] data;usedSize; }在 pos 位置新增元素我们需要对pos及后面的元素往后移从而空出位置来插入我们需要从usedSize-1开始往后移而不是pos因为从pos往后会将后面的元素进行覆盖从而丢失数据。还有我们依然需要对数组是否已经放满进行检查而且需要对pos是否合法进行检查负数是不可以的超过数组usedSize是不可以插入的。usedSize这个位置是可以插入的因为规定每次插入数据的位置前驱必须存在也就是说插入位置的前一个不能是空的。 public class PosIllegal extends RuntimeException{public PosIllegal() {}public PosIllegal(String msg) {super(msg);} }private void checkPosOfAdd(int pos) throws PosIllegal{if(pos 0 || pos this.usedSize){throw new PosIllegal(插入位置不合法);} } Override public void add(int pos, int data) {try{checkPosOfAdd(pos);if(isFull(this.array)){grow();}for (int i usedSize - 1; i pos ; i–) {this.array[i 1] this.array[i];}array[pos] data;usedSize;}catch(PosIllegal e){e.printStackTrace();} }接下来实现判定是否包含某个元素和查找某个元素对应的位置的方法。 Overridepublic boolean contains(int toFind) {for (int i 0; i this.usedSize; i) {if(array[i] toFind){return true;}//如果存放的不是整型元素而是引用类型的元素则需要使用equals方法来比较并且重写该方法。}return false;}Overridepublic int indexOf(int toFind) {for (int i 0; i this.usedSize; i) {if(array[i] toFind){return i;}//如果存放的不是整型元素而是引用类型的元素则需要使用equals方法来比较并且重写该方法。}return -1;}获取 pos 位置的元素对于获取元素我们依然需要对pos位置是否合法进行检查但是这一次pos位置不能小于0而且不能大于usedSize -1因为数组从0开始usedSize不可能存放元素对于获取元素我们还应该考虑一点就是当数组为空时我们是不能获取到任何元素的所以我们需要对数组是否为空进行检查当我们发现其为空时我们需要抛出异常因为无论我们返回何整数都有可能在数组中存在所以抛出异常是最好的。 public boolean isEmpty(){return this.usedSize 0; }private void checkEmpty(){if(isEmpty()){throw new EmptyException(顺序表为空);} }private void checkPosOfGet(int pos){if(pos 0 || pos this.usedSize -1){throw new PosIllegal(获取元素的位置不合法);} }Override public int get(int pos) {try{checkEmpty();checkPosOfGet(pos);return array[pos];}catch(PosIllegal e){e.printStackTrace();}catch(EmptyException e){e.printStackTrace();}return -1; }给 pos 位置的元素设为 value这就是更新的意思也就是与得到元素类似需要对其位置进行检查不能不能小于0而且不能大于usedSize -1也要对数组是否为空进行检查。 Override public void set(int pos, int value) {try{checkEmpty();checkPosOfGet(pos);array[pos] value;}catch(PosIllegal e){e.printStackTrace();}catch(EmptyException e){e.printStackTrace();} }删除第一次出现的关键字key首先要对顺序表是否为空进行判断空是没办法删除的。不为空之后我们可以通过遍历查找该元素的下标找不到直接返回找到对其后的元素进行挪动来覆盖最后不要忘了usedSize进行减一。 Overridepublic void remove(int toRemove) {try{checkEmpty();int pos indexOf(toRemove);if(pos -1){return;}for (int i pos; i this.usedSize - 1; i) {this.array[i] this.array[i1];}this.usedSize–;}catch(EmptyException e){e.printStackTrace();}}清空顺序表对于清空顺序表在int数组中我们可以对其usedSize置为0后面在add也只是覆盖但是如果是引用类型这样会造成内存泄漏因为数组中依然有一段地址指向一个空间而这个空间并没有什么作用所以应该将其置为null。 Override public void clear() {this.usedSize 0;/for (int i 0; i this.usedSize; i) {this.array[i] null;}/ }到这里我们就将ArrayList中常用的方法模拟实现了。下面为完整代码和测试代码 public interface IList {// 新增元素,默认在数组最后新增public void add(int data);// 在 pos 位置新增元素public void add(int pos, int data);// 判定是否包含某个元素public boolean contains(int toFind);// 查找某个元素对应的位置public int indexOf(int toFind);// 获取 pos 位置的元素public int get(int pos);// 给 pos 位置的元素设为 valuepublic void set(int pos, int value);//删除第一次出现的关键字keypublic void remove(int toRemove);// 获取顺序表长度public int size();// 清空顺序表public void clear();// 打印顺序表注意该方法并不是顺序表中的方法为了方便看测试结果给出的public void display(); }public class PosIllegal extends RuntimeException{public PosIllegal() {}public PosIllegal(String msg) {super(msg);} }public class EmptyException extends RuntimeException{public EmptyException() {}public EmptyException(String message) {super(message);} }import java.util.Arrays; public class MyArrayList implements IList{public int[] array;public int usedSize;public static final int DEFAULT_CAPACITY 5;public MyArrayList() {this.array new int[DEFAULT_CAPACITY];}public boolean isFull(int[] array){return this.usedSize array.length;}private void grow(){this.array Arrays.copyOf(this.array,this.array.length * 2);}Overridepublic void add(int data) {if(isFull(this.array)){grow();}array[usedSize] data;usedSize;}private void checkPosOfAdd(int pos) throws PosIllegal{if(pos 0 || pos this.usedSize){throw new PosIllegal(插入位置不合法);}}Overridepublic void add(int pos, int data) {try{checkPosOfAdd(pos);if(isFull(this.array)){grow();}for (int i usedSize - 1; i pos ; i–) {this.array[i 1] this.array[i];}array[pos] data;usedSize;}catch(PosIllegal e){e.printStackTrace();}}Overridepublic boolean contains(int toFind) {for (int i 0; i this.usedSize; i) {if(array[i] toFind){return true;}//如果存放的不是整型元素而是引用类型的元素则需要使用equals方法来比较并且重写该方法。}return false;}Overridepublic int indexOf(int toFind) {for (int i 0; i this.usedSize; i) {if(array[i] toFind){return i;}//如果存放的不是整型元素而是引用类型的元素则需要使用equals方法来比较并且重写该方法。}return -1;}public boolean isEmpty(){return this.usedSize 0;}private void checkEmpty(){if(isEmpty()){throw new EmptyException(顺序表为空);}}private void checkPosOfGet(int pos){if(pos 0 || pos this.usedSize -1){throw new PosIllegal(获取元素的位置不合法);}}Overridepublic int get(int pos) {try{checkEmpty();checkPosOfGet(pos);return array[pos];}catch(PosIllegal e){e.printStackTrace();}catch(EmptyException e){e.printStackTrace();}return -1;}Overridepublic void set(int pos, int value) {try{checkEmpty();checkPosOfGet(pos);array[pos] value;}catch(PosIllegal e){e.printStackTrace();}catch(EmptyException e){e.printStackTrace();}}Overridepublic void remove(int toRemove) {try{checkEmpty();int pos indexOf(toRemove);if(pos -1){return;}for (int i pos; i this.usedSize - 1; i) {this.array[i] this.array[i1];}this.usedSize–;}catch(EmptyException e){e.printStackTrace();}}Overridepublic int size() {return this.usedSize;}Overridepublic void clear() {this.usedSize 0;/for (int i 0; i this.usedSize; i) {this.array[i] null;}/}Overridepublic void display() {for (int i 0; i usedSize; i) {System.out.print(array[i] );}System.out.println();} }public class Test {public static void main(String[] args) {MyArrayList list1 new MyArrayList();IList list2 new MyArrayList();System.out.println(初始有效元素个数);System.out.println(list1.usedSize);System.out.println(打印初始顺序表);list1.display();System.out.println(打印初始数组大小);System.out.println(list1.size());list1.add(1);list1.add(2);list1.add(3);list1.add(4);System.out.println(打印插入元素后的顺序表);list1.display();System.out.println(打印插入元素后的顺序表大小);System.out.println(list1.size());list1.add(2,33);System.out.println(打印在指定位置插入元素后的顺序表);list1.display();//list1.add(44,4);System.out.println(顺序表是否包含某个元素);System.out.println(list1.contains(2));System.out.println(list1.contains(55));System.out.println(查找某个元素的指定位置);System.out.println(list1.indexOf(2));System.out.println(list1.indexOf(44));System.out.println(获取某个位置的元素);System.out.println(list1.get(1));//System.out.println(list1.get(100));System.out.println(更新某个位置的元素);list1.set(0,11);list1.display();System.out.println(删除第一次出现的关键字key);list1.remove(33);list1.display();System.out.println(清空顺序表);list1.clear();list1.display();//结果为//初始有效元素个数//0//打印初始顺序表////打印初始数组大小//0//打印插入元素后的顺序表//1 2 3 4 //打印插入元素后的顺序表大小//4//打印在指定位置插入元素后的顺序表//1 2 33 3 4 //顺序表是否包含某个元素//true//false//查找某个元素的指定位置//1//-1//获取某个位置的元素//2//更新某个位置的元素//11 2 33 3 4 //删除第一次出现的关键字key//11 2 3 4 //清空顺序表} }ArrayList简介 在集合框架中ArrayList是一个普通的类实现了List接口具体框架图如下 说明 ArrayList是以泛型的方式实现的使用时必须要先实例化ArrayList实现了RandomAccess接口表明ArrayList支持随机访问ArrayList实现了Cloneable接口表明ArrayList是可以clone的ArrayList实现了Serializable接口表明ArrayList是支持序列化的和Vector不同ArrayList不是线程安全的在单线程下可以使用在多线程中可以选择Vector或者CopyOnWriteArrayListArrayList底层是一段连续的空间并且可以动态扩容是一个动态类型的顺序表。 ArrayList的使用 ArrayList的构造 方法解释ArrayList ()无参构造ArrayList (Collection? extends E c)利用其他 Collection 构建 ArrayListArrayList (int initialCapacity)指定顺序表初始容量 public class Test {public static void main(String[] args) {//ArrayList创建//构造一个空的列表ListInteger list1 new ArrayList();//构造一个具有10个容量的列表ListInteger list2 new ArrayList(10);list2.add(1);list2.add(2);list2.add(3);//list2.add(hello); //编译失败ListInteger本身就限定了list2中只能存储整型元素//list3构造好之后与list中的元素一致ArrayListInteger list3 new ArrayList(list2);//避免省略类型否则任意类型的元素都可以存放使用时很麻烦List list4 new ArrayList();list4.add(1);list4.add(hello);} }ArrayList的常见操作 方法解释boolean add(E e)尾插 evoid add(int index, E element)将 e 插入到 index 位置boolean addAll(Collection? extends E c)尾插 c 中的元素E remove(int index)删除 index 位置元素boolean remove(Object o)删除遇到的第一个 oE get(int index)获取下标 index 位置元素E set(int index, E element)将下标 index 位置元素设置为 elementvoid 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 class Test {public static void main(String[] args) {ListString list new ArrayList();list.add(food);list.add(book);list.add(clothes);list.add(drink);System.out.println(list); //[food, book, clothes, drink]//获取list中有效元素的个数System.out.println(list.size()); //4//获取和设置index位置上的元素注意index必须介于[0,size)间System.out.println(list.get(1)); //booklist.set(1,BOOK);System.out.println(list.get(1)); //BOOK//在list的index位置插入指定元素index及后续的元素统一往后搬移一个位置list.add(1,shoes);System.out.println(list); //[food, shoes, BOOK, clothes, drink]//删除指定元素找到了就删除该元素之后的元素统一往前搬移一个位置list.remove(shoes);System.out.println(list); //[food, BOOK, clothes, drink]//删除list中的index位置上的元素注意index不用超过list中有效元素个数否则会抛出下标越界异常list.remove(list.size() - 1); System.out.println(list); //[food, BOOK, clothes]//检测list中是否包含指定元素包含返回true否则返回falseif(!list.contains(drink)){list.add(drink);}System.out.println(list); //[food, BOOK, clothes, drink]//查找指定元素第一次出现的位置indexOf从前往后找lastIndexOf从后往前找list.add(bag);System.out.println(list); //[food, BOOK, clothes, drink, bag]System.out.println(list.indexOf(bag)); //4System.out.println(list.lastIndexOf(bag)); //4//使用list中[0,4)之间的元素构成一个新的subList返回但是和ArrayList共用一个elementData数组,//也就的引用指向同一个空间当你修改subList中的元素List指向的空间中的元素自然也改变了。ListString ret list.subList(0,4);System.out.println(ret); //[food, BOOK, clothes, drink]System.out.println(list); //[food, BOOK, clothes, drink, bag]ret.set(0,FOOD);System.out.println(list); //[FOOD, BOOK, clothes, drink, bag]System.out.println(ret); //[FOOD, BOOK, clothes, drink]list.clear();System.out.println(list.size()); //0} }ArrayList的遍历 ArrayList可以使用三种方式遍历for循环下标、foreach、使用迭代器 public class Test {public static void main(String[] args) {ListInteger list new ArrayList();list.add(1);list.add(2);list.add(3);list.add(4);System.out.println(list);System.out.println( for循环遍历 );for (int i 0; i list.size(); i) {System.out.print(list.get(i) );}System.out.println();System.out.println( foreach遍历 );for (Integer x : list) {System.out.print(x );}System.out.println();System.out.println( 使用迭代器Iterator输出 );IteratorInteger it1 list.iterator();while(it1.hasNext()){System.out.print(it1.next() );}System.out.println();System.out.println( 使用迭代器ListIterator输出 );ListIteratorInteger it2 list.listIterator();while(it2.hasNext()){System.out.print(it2.next() );}System.out.println();System.out.println( 使用迭代器ListIterator输出 拓展 );ListIteratorInteger it3 list.listIterator(list.size());while(it3.hasPrevious()){System.out.print(it3.previous() );}}//结果为//[1, 2, 3, 4]// for循环遍历 //1 2 3 4 // foreach遍历 //1 2 3 4 // 使用迭代器Iterator输出 //1 2 3 4 // 使用迭代器ListIterator输出 //1 2 3 4 // 使用迭代器ListIterator输出 拓展 //4 3 2 1 }ArrayList的扩容机制 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 codeif (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; }总结 检测是否真正需要扩容如果是调用grow准备扩容预估需要容量的大小 初步预估按照1.5倍大小扩容 如果用户所需大小超过预估1.5倍大小则按照用户所需大小扩容 真正扩容之前检测是否能扩容成功防止太大导致扩容失败使用copyOf进行扩容 ArrayList的具体使用 杨辉三角 杨辉三角 给定一个非负整数 numRows生成「杨辉三角」的前 numRows 行。 在「杨辉三角」中每个数是它左上方和右上方的数的和。 示例 1: 输入: numRows 5 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
public class Test{//ListListInteger 为二维数组public static ListListInteger generate(int numRows) {ListListInteger ret new ArrayList();ListInteger list0 new ArrayList();list0.add(1);ret.add(list0);for (int i 1; i numRows; i) {ListInteger curRow new ArrayList();//处理第一个元素curRow.add(1);//中间for (int j 1; j i; j) {Integer data ret.get(i-1).get(j-1) ret.get(i-1).get(j);curRow.add(data);}//尾部curRow.add(1);ret.add(curRow);}return ret;}public static void main(String[] args) {ListListInteger ret generate(4);/System.out.println(ret);/for (int i 0; i ret.size(); i) {for (int j 0; j ret.get(i).size(); j) {System.out.print(ret.get(i).get(j) );}System.out.println();}//结果为//1 //1 1 //1 2 1 //1 3 3 1 } }简单的洗牌算法 要求 买52张牌洗牌3个人每个人轮流拿五张 public class Card {public int rank; //牌面值public String suit; //花色public Card(int rank, String suit) {this.rank rank;this.suit suit;}Overridepublic String toString() {return { rank suit };} }public class CardDemo {public static final String[] suits {♦,♣,♥,♠};public ListCard buyCard(){ListCard cardList new ArrayList(52);for (int i 1; i 13 ; i) {for (int j 0; j 4; j) {int rank i;String suit suits[j];cardList.add(new Card(rank,suit));}}return cardList;}public void shuffle(ListCard cardlist){Random random new Random();for (int i cardlist.size() - 1; i 0; i–) {int index random.nextInt(i);swap(cardlist,i,index);}}private void swap(ListCard cardList, int i, int j){/Card tmp cardList[i];cardList[i] cardList[j];cardList[j] tmp;/Card tmp cardList.get(i);cardList.set(i,cardList.get(j));cardList.set(j,tmp);}public ListListCard play(ListCard cardList){ListListCard ret new ArrayList();ListCard hand0 new ArrayList();ListCard hand1 new ArrayList();ListCard hand2 new ArrayList();ret.add(hand0);ret.add(hand1);ret.add(hand2);for (int i 0; i 5; i) {for (int j 0; j 3; j) {ret.get(j).add(cardList.remove(0));}}return ret;} }public class Test{public static void main(String[] args) {//买一副52张的牌CardDemo cards new CardDemo();ListCard cardList cards.buyCard();System.out.println(cardList);//洗牌cards.shuffle(cardList);System.out.println(cardList);//3个人每个人轮流拿五张ListListCard players cards.play(cardList);for (int i 0; i players.size(); i) {System.out.println(第(i1) 个人的牌 players.get(i));}//剩下的牌System.out.print(剩下的牌);System.out.println(cardList);}//结果为//[{1♦}, {1♣}, {1♥}, {1♠}, {2♦}, {2♣}, {2♥}, {2♠}, {3♦}, {3♣}, {3♥}, {3♠}, {4♦}, {4♣}, {4♥}, {4♠}, {5♦}, {5♣}, {5♥}, {5♠}, {6♦}, {6♣}, {6♥}, {6♠}, {7♦}, {7♣}, {7♥}, {7♠}, {8♦}, {8♣}, {8♥}, {8♠}, {9♦}, {9♣}, {9♥}, {9♠}, {10♦}, {10♣}, {10♥}, {10♠}, {11♦}, {11♣}, {11♥}, {11♠}, {12♦}, {12♣}, {12♥}, {12♠}, {13♦}, {13♣}, {13♥}, {13♠}]//[{4♠}, {9♥}, {5♣}, {1♦}, {12♣}, {13♥}, {3♦}, {8♣}, {4♦}, {5♠}, {2♠}, {5♦}, {10♥}, {13♦}, {12♥}, {10♦}, {7♥}, {10♠}, {7♣}, {11♦}, {9♦}, {5♥}, {1♠}, {8♠}, {11♥}, {13♣}, {4♥}, {12♦}, {3♥}, {6♠}, {8♦}, {6♥}, {3♠}, {13♠}, {6♦}, {1♥}, {1♣}, {2♦}, {4♣}, {10♣}, {7♠}, {3♣}, {2♣}, {7♦}, {9♠}, {6♣}, {9♣}, {2♥}, {8♥}, {12♠}, {11♣}, {11♠}]//第1个人的牌[{4♠}, {1♦}, {3♦}, {5♠}, {10♥}]//第2个人的牌[{9♥}, {12♣}, {8♣}, {2♠}, {13♦}]//第3个人的牌[{5♣}, {13♥}, {4♦}, {5♦}, {12♥}]//剩下的牌[{10♦}, {7♥}, {10♠}, {7♣}, {11♦}, {9♦}, {5♥}, {1♠}, {8♠}, {11♥}, {13♣}, {4♥}, {12♦}, {3♥}, {6♠}, {8♦}, {6♥}, {3♠}, {13♠}, {6♦}, {1♥}, {1♣}, {2♦}, {4♣}, {10♣}, {7♠}, {3♣}, {2♣}, {7♦}, {9♠}, {6♣}, {9♣}, {2♥}, {8♥}, {12♠}, {11♣}, {11♠}] }ArrayList的问题及思考 ArrayList底层使用连续的空间任意位置插入或者删除元素时需要将该位置后序元素整体往前或者往后搬移故时间复杂度为O(N)增容需要申请新空间拷贝数据释放旧空间会有不小的消耗。增容一般是呈1.5倍的增长势必会有一定的空间浪费。例如当前容量为100满了以后增容到150我们只想继续插入5个数据后面没有数据插入了那么就浪费了45个数据空间。 关于ArrayList我们先了解和学习到这希望这篇文章能帮助到你谢谢你的阅读。