文章目录
前言
hello,大家好,我是阿旭啊。今天给大家带来数据结构第四篇:ArrayList。希望能给大家一些小帮助~
一.ArrayList的介绍
ArrayList底层是一个顺序表!!!
1.ArrayList的构造
2.ArrayList常见操作
二.ArrayList使用代码
1.ArrayList实例化
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
List<String> list2 = new ArrayList<>(arrayList);//利用其他Collection来构建
//要注意此时构造方法的泛型参数必须是它本身或者是子类,此处的泛型参数为Sring
List<String> list1 = new ArrayList<>();//不带参数的构造方法
}
2.ArrayList方法代码
代码如下(示例):
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
System.out.println("============================");
System.out.println(arrayList.add("abc"));//添加一个元素返回值为true
System.out.println(arrayList);
System.out.println("============================");
ArrayList<String> arrayList1 = new ArrayList<>();
arrayList1.add("def");
arrayList1.add("hello");
arrayList1.add("def");
arrayList.addAll(arrayList1);//添加arrayList1中的所有元素
System.out.println(arrayList);
System.out.println("============================");
arrayList.add(1,"abcd");//对应位置插入数据
System.out.println(arrayList);
System.out.println("============================");
String a = arrayList.remove(1);//删除对应位置元素,且可以得到删除的元素
System.out.println(arrayList);
System.out.println(a);
System.out.println("============================");
System.out.println(arrayList.remove("abc"));//删除对应元素,返回为true
System.out.println(arrayList);
System.out.println("============================");
String b = arrayList.get(0);//获取0下标的元素
arrayList.set(1, "abc");//设置1下标元素为:abc
System.out.println(b);
System.out.println(arrayList);
System.out.println("============================");
arrayList.clear();//清空列表
System.out.println(arrayList);
System.out.println("============================");
System.out.println(arrayList.contains("abc"));//判断元素是否在内
System.out.println("============================");
System.out.println(arrayList1.indexOf("def"));//返回第一个元素所在下标
System.out.println("============================");
System.out.println(arrayList1.lastIndexOf("def"));//返回最后一个元素所在下标
System.out.println("============================");
List<String> list = arrayList1.subList(0, 2);//截取arrayList1的前两个元素到list
System.out.println(list);
list.set(0, "abc");//截取的list元素被改变
System.out.println();
System.out.println(list);
System.out.println("============================");
System.out.println(arrayList1);//被截取的arrayList1元素对应改变
System.out.println("============================");
}
运行结果:
============================
true
[abc]
============================
[abc, def, hello, def]
============================
[abc, abcd, def, hello, def]
============================
[abc, def, hello, def]
abcd
============================
true
[def, hello, def]
============================
def
[def, abc, def]
============================
[]
============================
false
============================
0
============================
2
============================
[def, hello]
[abc, hello]
============================
[abc, hello, def]
============================
sublist截取时候是公用同一个地址,所以一个改变另一个也会改变。
3.ArrayList的遍历的四种方式
代码实现:
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("abc");
arrayList.add("def");
System.out.println("============================");
System.out.println(arrayList);//直接打印
System.out.println("============================");
for (int i = 0; i < arrayList.size(); i++) {//fori循环打印
System.out.println(arrayList.get(i));
}
System.out.println("============================");
for (String x:arrayList) {//foreach循环打印
System.out.println(x);
}
System.out.println("============================");
Iterator<String> it = arrayList.listIterator();//迭代器循环打印
while (it.hasNext()){
System.out.println(it.next());
}//如果有下一位,那么打印下一位
System.out.println("============================");
}
运行结果:
============================
[abc, def]
============================
abc
def
============================
abc
def
============================
abc
def
============================
三.ArrayList的具体使用
1.删除str1中的str2(使用集合去做)
例:
str1: welcome to bit str2: come
输出结果:wl t bit
思路:创建一个arraylist,取str1的每个字符,判断是否在str2内,不过不在那么存入arraylist,判断完毕后按照格式打印出来
代码实现:
public static void func(String str1, String str2){
ArrayList<Character> arrayList = new ArrayList<>();
for (int i = 0; i < str1.length();i++){
char ch = str1.charAt(i);
if(!str2.contains(ch + "")){
arrayList.add(ch);
}
}
for (Character x: arrayList){
System.out.print(x);
}
}
public static void main(String[] args) {
func("welcome to bit", "come");
}
输出结果:
wl t bit
2.简单的洗牌算法
首先,我们需要创建一个牌类Card,需要有花色,点数,以及构造方法,get和set方法和toString方法:
public class Card {
private String suit;
private int rank;
public Card(String suit, int rank) {
this.suit = suit;
this.rank = 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;
}
@Override
public String toString() {
return suit +" "+ rank;
}
}
然后,我们知道扑克牌有四种花色,这是固定的,我们给他定义成常量字符串。
public static final String[] SUITS= {"♥","♠","♣","♦"};
然后我们定义一个创建牌的方法buyCard方法,先创建一个Cards来保存创建好的牌,然后每类牌有十三张,我们用一个循环就可以解决,在循环里创建一张牌并保存在Cards中:
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(SUITS[i], j);
cards.add(card);
}
}
return cards;
}
有了牌当然要洗牌啦,我们来完成洗牌函数shuffle。我们可以定义一个随机值,然后从第一张牌开始每一张牌与这个随机值进行交换就可以完成乱序。要注意定义的随机值范围应该不包括选定的交换的牌,比如选定第一张牌,随机的牌里就不能有第一张牌,又因为是数组的缘故,所以范围为(2,53):
public static List<Card> shuffle(List<Card> cards){
Random random = new Random();
for (int i = 0; i < cards.size() - 1; i++){
int j = random.nextInt(i + 1,cards.size());
Card card = cards.get(i);
cards.set(i, cards.get(j));
cards.set(j, card);
}
return cards;
}
洗玩牌之后应该要进行发牌工作,首先定义三的人的牌组分别为hand1,hand2,hand3:
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
然后我们规定每个人发五张牌且为轮流发牌(不是一人直接发五张,是一张一张发)。为了实现这个想法我们需要将三个人的牌组放到一个数组里,我们定义了hands:
List<List<Card>> hands = new ArrayList<>();
hands.add(hand1);
hands.add(hand2);
hands.add(hand3);
for(int i = 0; i < 5; i++){
for (int j = 0 ; j < 3; j++){
Card card = cards.remove(0);
hands.get(j).add(card);
}
}
完整代码:
Card类:
public class Card {
private String suit;
private int rank;
public Card(String suit, int rank) {
this.suit = suit;
this.rank = 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;
}
@Override
public String toString() {
return suit +" "+ rank;
}
}
测试代码:
public class test {
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(SUITS[i], j);
cards.add(card);
}
}
return cards;
}
public static List<Card> shuffle(List<Card> cards){
Random random = new Random();
for (int i = 0; i < cards.size() - 1; i++){
int j = random.nextInt(i + 1,cards.size());
Card card = cards.get(i);
cards.set(i, cards.get(j));
cards.set(j, card);
}
return cards;
}
public static void main(String[] args) {
List<Card> cards = buyCard();
System.out.println(cards);
cards = shuffle(cards);
System.out.println("洗牌:");
System.out.println(cards);
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
List<List<Card>> hands = new ArrayList<>();
hands.add(hand1);
hands.add(hand2);
hands.add(hand3);
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("第一个人牌:");
System.out.println(hand1);
System.out.println("第二个人牌:");
System.out.println(hand2);
System.out.println("第三个人牌:");
System.out.println(hand3);
System.out.println("剩余的牌:");
System.out.println(cards);
}
}
运行结果:
[♥ 1, ♥ 2, ♥ 3, ♥ 4, ♥ 5, ♥ 6, ♥ 7, ♥ 8, ♥ 9, ♥ 10, ♥ 11, ♥ 12, ♥ 13, ♠ 1, ♠ 2, ♠ 3, ♠ 4, ♠ 5, ♠ 6, ♠ 7, ♠ 8, ♠ 9, ♠ 10, ♠ 11, ♠ 12, ♠ 13, ♣ 1, ♣ 2, ♣ 3, ♣ 4, ♣ 5, ♣ 6, ♣ 7, ♣ 8, ♣ 9, ♣ 10, ♣ 11, ♣ 12, ♣ 13, ♦ 1, ♦ 2, ♦ 3, ♦ 4, ♦ 5, ♦ 6, ♦ 7, ♦ 8, ♦ 9, ♦ 10, ♦ 11, ♦ 12, ♦ 13]
洗牌:
[♠ 10, ♣ 13, ♣ 12, ♣ 10, ♠ 11, ♥ 1, ♠ 5, ♣ 5, ♥ 10, ♠ 9, ♥ 7, ♠ 1, ♦ 11, ♦ 3, ♥ 5, ♣ 1, ♦ 1, ♠ 2, ♥ 13, ♥ 11, ♣ 4, ♥ 8, ♣ 7, ♦ 7, ♣ 8, ♥ 3, ♣ 6, ♠ 4, ♥ 6, ♠ 3, ♦ 5, ♥ 12, ♠ 7, ♦ 6, ♠ 8, ♦ 10, ♦ 12, ♣ 9, ♦ 13, ♥ 4, ♥ 2, ♥ 9, ♣ 2, ♣ 11, ♦ 2, ♠ 12, ♠ 6, ♦ 8, ♠ 13, ♦ 4, ♣ 3, ♦ 9]
第一个人牌:
[♠ 10, ♣ 10, ♠ 5, ♠ 9, ♦ 11]
第二个人牌:
[♣ 13, ♠ 11, ♣ 5, ♥ 7, ♦ 3]
第三个人牌:
[♣ 12, ♥ 1, ♥ 10, ♠ 1, ♥ 5]
剩余的牌:
[♣ 1, ♦ 1, ♠ 2, ♥ 13, ♥ 11, ♣ 4, ♥ 8, ♣ 7, ♦ 7, ♣ 8, ♥ 3, ♣ 6, ♠ 4, ♥ 6, ♠ 3, ♦ 5, ♥ 12, ♠ 7, ♦ 6, ♠ 8, ♦ 10, ♦ 12, ♣ 9, ♦ 13, ♥ 4, ♥ 2, ♥ 9, ♣ 2, ♣ 11, ♦ 2, ♠ 12, ♠ 6, ♦ 8, ♠ 13, ♦ 4, ♣ 3, ♦ 9]
四.ArrayList的优缺点
优点:
1.根据指定下标去查找元素,效率非常的高!时间复杂度:O(1)。
2.更新元素也很快。
缺点:
1.每次插入数据都需要移动数据,如果插入到0下标,那么移动的元素复杂度为O(n)。
2.每次删除数据都需要移动数据,如果删除0下标,那么移动的元素复杂度为O(n)。
3.当满了之后,进行1.5倍扩容,然后脂肪一个元素的话,可能会有浪费空间。
总结:顺序表适用于经常查找元素或者是更新元素的场景。
为了解决这些个缺点,我们可以使用链表。预知后事如何,请听我下回分解!
总结
好啦~本篇文章就到此结束,顺序表也就结束啦!从下一篇开始我为大家讲解链表的知识!希望大家能够多多点赞,多多评论,多多批评,大家下一篇再见!!!