【8.19】包装类 & ArrayList常用操作 & 迭代器 & LinkedList链表
利用顺序表实现扑克牌的简单操作
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Random;
public class Main {
private static void swap(ArrayList<PokerCard> list,int i,int j){
PokerCard tmpI = list.get(i);
PokerCard tmpJ = list.get(j);
list.set(i,tmpJ);
list.set(j,tmpI);
}
public static void main(String[] args) {
ArrayList<PokerCard> cards = new ArrayList<>(52);
String[] colors = {"♥","♦","♣","♠"};
for(int i = 0; i < 4; i++){
for(int j = 1; j <= 13; j++){
PokerCard card = new PokerCard(j, colors[i]);
cards.add(card);
}
}
//System.out.println(cards);
//洗牌
/*
Random random = new Random(20190820);
for(int i = 0; i < 10; i++){
System.out.println(random.nextInt(20));
}
*/
Random random = new Random(20190820);
for(int i = 51; i > 0; i--){
int j = random.nextInt(i);
swap(cards, i, j);
}
System.out.println(cards);
//有三个人,每个人抓 5 张牌
ArrayList<PokerCard> A = new ArrayList<>();
ArrayList<PokerCard> B = new ArrayList<>();
ArrayList<PokerCard> C = new ArrayList<>();
System.out.println(cards.subList(52 - 15, 52));
for (int i = 0; i < 15; i++) {
PokerCard card = cards.remove(cards.size() - 1);
switch(i % 3){
case 0:
A.add(card);
break;
case 1:
B.add(card);
break;
case 2:
C.add(card);
break;
}
}
System.out.println(cards.size());
System.out.println(A);
System.out.println(B);
System.out.println(C);
//A抓到的牌中,有没有[红心 A]
PokerCard heartA = new PokerCard(3,"♣");
for(PokerCard card: A){
//如何判断card是不是红心 A
//自定义类如何判断两个对象相等?
if(card.equals(heartA)){
System.out.println("包含");
}
}
if(A.contains(heartA)){
System.out.println("包含");
}else{
System.out.println("不包含");
}
/*
System.out.println(A);
for(int i = 0; i < A.size(); i++){
PokerCard o = A.get(i);
if(o.equals(heartA)){
A.remove(o);
}
}
System.out.println(A);
*/
//迭代器
System.out.println(A);
Iterator<PokerCard> it = A.iterator();
while(it.hasNext()){
PokerCard o = it.next();
if(o.equals(heartA)){
it.remove();
}
}
System.out.println(A);
/*
System.out.println(A);
for(PokerCard o : A){
if(o.equals(heartA)){
A.remove(o);
}
}
System.out.println(A);
*/
Iterator<PokerCard> it1 = A.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
ListIterator<PokerCard> it2 = A.listIterator();
System.out.println(it2.next());
System.out.println(it2.next());
System.out.println(it2.previous());
}
}
public class PokerCard {
int value;
String color;
public PokerCard(int value, String color){
this.value = value;
this.color = color;
}
@Override
public String toString() {
return String.format("[%d, %s]",value,color);
}
@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
//返回obj对象能否被PokerCard类型的引用指向
//obj对象的类型是不是PokerCard类型的小类型
if(!(obj instanceof PokerCard)){
return false;
}
PokerCard other = (PokerCard)obj;
//equals方法,判断两个对象是否相等
return this.value == other.value && this.color.equals(other.color);
}
}
运行结果:
[[2, ♣], [8, ♣], [4, ♣], [13, ♦], [9, ♦], [6, ♠], [8, ♥], [6, ♥], [11, ♥], [2, ♠], [13, ♥], [12, ♦], [13, ♠], [9, ♣], [7, ♦], [12, ♠], [9, ♥], [3, ♦], [1, ♣], [10, ♣], [4, ♠], [7, ♥], [13, ♣], [4, ♥], [3, ♠], [11, ♠], [7, ♣], [12, ♣], [5, ♣], [10, ♥], [2, ♥], [11, ♦], [1, ♦], [8, ♦], [7, ♠], [1, ♥], [5, ♦], [1, ♠], [4, ♦], [10, ♠], [11, ♣], [6, ♦], [12, ♥], [9, ♠], [2, ♦], [6, ♣], [5, ♥], [3, ♥], [10, ♦], [5, ♠], [8, ♠], [3, ♣]]
[[1, ♠], [4, ♦], [10, ♠], [11, ♣], [6, ♦], [12, ♥], [9, ♠], [2, ♦], [6, ♣], [5, ♥], [3, ♥], [10, ♦], [5, ♠], [8, ♠], [3, ♣]]
37
[[3, ♣], [10, ♦], [6, ♣], [12, ♥], [10, ♠]]
[[8, ♠], [3, ♥], [2, ♦], [6, ♦], [4, ♦]]
[[5, ♠], [5, ♥], [9, ♠], [11, ♣], [1, ♠]]
包含
包含
[[3, ♣], [10, ♦], [6, ♣], [12, ♥], [10, ♠]]
[[10, ♦], [6, ♣], [12, ♥], [10, ♠]]
[10, ♦]
[6, ♣]
[12, ♥]
[10, ♠]
[10, ♦]
[6, ♣]
[6, ♣]
LinkedList链表示例
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.push("C++");
list.push("JavaSE");
list.push("数据");
list.push("系统");
System.out.println(list.pop());
System.out.println(list.size());
list.addLast("C");
list.addLast("Java");
list.addLast("数据结构");
list.addLast("操作系统");
System.out.println(list.peek());
System.out.println(list.peekFirst());
System.out.println(list.peekLast());
System.out.println(list.size());
System.out.println(list.poll());
System.out.println(list.pollFirst());
System.out.println(list.pollLast());
System.out.println(list.size());
}
}
运行结果:
系统
3
数据
数据
操作系统
7
数据
JavaSE
操作系统
4