目录
1.ArrayList的初步理解
2.ArrayList的三种构造方法
3.ArrayList的常见操作
4.ArrayList的遍历
5.ArrayList的扩容机制
6.ArrayList的具体使用
7.对ArrayList的思考
一,ArrayList的初步理解
ArrayList是数据结构的一种表现形式,可以理解为类似数组的一个顺序结构,功能更强的数组
二,相关构造方法
(1)不带参数的
ArrayList<Integer> arrayList1=new ArrayList<>();
arrayList1.add(1);
arrayList1.add(2);
arrayList1.add(3);

一是方法的实现,图二是不带参数的arraylist的源码,可以看到它是默认了大小为10
(2)带一个容量的
ArrayList<Integer> arrayList2=new ArrayList<>(15);
arrayList2.add(11);
arrayList2.add(22);
arrayList2.add(33);

arraylist2给到了一个初始的容量15
(3)带集合构造
ArrayList<Integer> arrayList3=new ArrayList<>(arrayList1);
arrayList3.add(999);
System.out.println(arrayList3);

把另一个结构类似的Arraylist传给新的Arraylist,并且把对应的元素全都传给第二个,所以打印出来就是第一个的总和后面一次的add

三,常见操作及其解释
| boolean add(E e) | 尾插e |
| void add(int index, E element) | 将e插入到index位置 |
| boolean addAll (Collection<?extends E>d | 尾插c中的元素 |
| Eremove(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 fromlndex, inttolndex) | 截取部分list |
四,遍历
(1)简单的for循环
ArrayList<Integer> arrayList1=new ArrayList<>();
arrayList1.add(1);
arrayList1.add(2);
arrayList1.add(3);
System.out.println(arrayList1);
for (int i = 0; i < arrayList1.size(); i++) {
System.out.print(arrayList1.get(i));
}
通过get方法和for循环就可以很好的取到arraylist1中的所有元素
(2)foreach循环
ArrayList<Integer> arrayList1=new ArrayList<>();
arrayList1.add(1);
arrayList1.add(2);
arrayList1.add(3);
System.out.println(arrayList1);
for (Integer i:arrayList1){
System.out.print(i);
}
将i作为arraylist1的每一个元素来遍历出来
(3)迭代器
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
// 获取迭代器
Iterator<String> it = list.iterator();
// 循环:只要有下一个就继续
while (it.hasNext()) {
// 取出下一个元素
String s = it.next();
System.out.print(s);
}
用迭代器(Iterator)遍历 ArrayList
五,扩容机制

这是ArrayList的扩容的源码,下面那个是在你默认add方法的时候进行的加一个空间,上面的是你超过默认容量的一个扩容,是进行一个1.5倍的扩容
六,具体的使用
我用一个洗牌算法来进行一个简单的演示
(1)第一步定义一个Card类,有数字和花色
public class Card {
public int rank; // 牌面值
public String suit; // 花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return String.format("[%s %d]", suit, rank);
}
}
(2)展示所有的牌
public static final String[] SUITS = {"♠", "♥", "♣", "♦"};
public static List<Card> buycard(){
List <Card> cards=new ArrayList<>();
for (int i = 0; i < SUITS.length; i++) {
for (int j = 1; j < 13 ; j++) {
Card card=new Card(j,SUITS[i]);
cards.add(card);
}
}
return cards;
}
(3)将牌打乱,进行一次洗牌(本质就是通过下标来交换牌,上面负责产生随机的下标)
public static void randoms(List<Card> cards){
Random random = new Random();
for (int i = cards.size()-1; i > 0 ; i--) {
int index = random.nextInt(i);//[0,i)
swap(cards,i,index);
}
}
public static void swap(List<Card> cards,int i,int index){
Card tmp=cards.get(i);
cards.set(i,cards.get(index));
cards.set(index,tmp);
}
(4)三个人轮流摸五张牌(将所有牌定义为一个hands数组,三个人分别定义成为hand012数组,
可以看成一个二维数组)
List<Card> hand0=new ArrayList<>();
List<Card> hand1=new ArrayList<>();
List<Card> hand2=new ArrayList<>();
List<List<Card>> hands=new ArrayList<>();
hands.add(hand0);
hands.add(hand1);
hands.add(hand2);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Card card=cards.remove(0);
hands.get(j).add(card);
}
}
System.out.println(hand0);
System.out.println(hand1);
System.out.println(hand2);
七,思考
(1)Arraylist是连续的排列,所以删除和增加需要挪动其余的n-1个数据,所以时间复杂度为O(N),复杂度较高
(2)Arraylist的扩容默认为1.5倍,如果我只需要多一两个就会造成大量的空间浪费
610



被折叠的 条评论
为什么被折叠?



