顺序表模拟实现
import java.util.Arrays;
public class MyArraylist {
public int[] arr;
public int count = 0;
public static final int DEFAULT_LENGTH = 3;
public MyArraylist() { // 默认 5 长度
this.arr = new int[DEFAULT_LENGTH];
}
public MyArraylist(int length) {
this.arr = new int[length];
}
public int indexDetection(int pos) { // 下标检测器
if (pos < 0) { // 有问题
return -1;
}
return 1; // 没问题
}
public boolean lengthDetection(int count) { // 长度检测器
return count == arr.length;
}
public void arrayExtends(int[] arr) { // 数组扩容器
this.arr = Arrays.copyOf(arr, arr.length * 2);
}
public int emptyArray(int count) {
if (count == 0) {
return -1;
}
return 1;
}
public void add(int data) { // 新增元素,默认在数组最后位置
if (lengthDetection(count)) {
arrayExtends(this.arr);
}
this.arr[this.count++] = data;
}
public void add(int pos, int data) { // 在 pos 位置新增元素
if (indexDetection(pos) == -1) {
throw new IindexException("输入下标非法");
}
if (pos >= this.count) {
throw new IpositionException("输入下标的数组元素前没有元素");
}
if (lengthDetection(count)) {
arrayExtends(this.arr);
}
for (int i = this.count - 1; i >= pos; i--) {
this.arr[i + 1] = this.arr[i];
}
this.arr[pos] = data;
}
public boolean contains(int data) { // 判断是否包含某个元素
for (int i = 0; i < this.arr.length; i++) {
if (this.arr[i] == data) {
return true;
}
}
return false;
}
public int indexOf(int data) { // 查找某个元素对应的位置
for (int i = 0; i < this.arr.length; i++) {
if (this.arr[i] == data) {
return i;
}
}
return -1;
}
public int get(int pos) { // 查找某个下标对应的元素
if (indexDetection(pos) == -1) {
throw new IpositionException("输入下标非法");
}
if (pos >= this.count) {
throw new IindexException("输入下标非法");
}
if (emptyArray(this.count) == -1) {
throw new IemptyArray("空数组,无法查询");
}
return this.arr[pos];
}
public void remove(int data) { // 删除第一次出现的元素
int tmp= indexOf(data);
if (indexOf(data) == -1) {
System.out.println("未出现该数据");
return;
}
for (int i = tmp; i >= this.count - 1; i++) {
this.arr[i] = this.arr[i + 1];
count--;
}
}
public int size() { // 统计数组长度
return this.count;
}
public void clear() { // 清除数组
this.count = 0;
}
@Override
public String toString() { // 打印数组
return "arr=" + Arrays.toString(arr);
}
}
顺序表的使用
ArrayList的构造
import java.util.ArrayList;
import java.util.List;
public class Array {
public static void main(String[] args) { // 顺序表构造的推荐写法
List<Integer> arrayList1 = new ArrayList<>(); // 无参构造,默认长度为10
List<Integer> arrayList2 = new ArrayList<>(10); // 指定顺序表长度
arrayList1.add(1);
arrayList1.add(2);
arrayList1.add(3);
List<Integer> arrayList3 = new ArrayList<>(arrayList1); // 利用其它集合类构建顺序表
}
}
ArrayList的常用方法
import java.util.ArrayList;
import java.util.List;
public class Array {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1); // 尾插元素
list.add(1, 2); // 将元素插到指定位置
list.remove(1); // 删除指定位置的元素
list.remove((Integer) 2); // 删除第一个指定元素
list.set(0, 1); // 将指定位置设置指定元素
list.get(0); // 获取指定位置元素
System.out.println(list.contains(0)); // 判断指定元素是否存在
System.out.println(list.indexOf(1)); // 返回第一个元素所在下标
System.out.println(list.lastIndexOf(1)); // 返回最后一个元素所在下标
List<Integer> list2 = list.subList(0,1); // 从 [a, b) 位置截取
list.clear(); // 清空
}
}
ArrayList的遍历
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Array {
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);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
ListIterator<Integer> lit1 = list.listIterator();
while (lit1.hasNext()) {
System.out.print(lit1.next() + " ");
}
System.out.println();
ListIterator<Integer> lit2 = list.listIterator(list.size());
while (lit2.hasPrevious()) {
System.out.print(lit2.previous() + " ");
}
System.out.println();
}
}
顺序表实践:扑克牌游戏
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 rank + suit;
}
}
CardGame
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class CardGame {
private static final String[] suits = {"♥", "♦", "♠", "♣"};
private void swap(List<Card> list, int i, int j) {
Card tmp = list.get(i);
list.set(i, list.get(j));
list.set(j, tmp);
}
public List<Card> buyCard() {
List<Card> list = new ArrayList<>(54);
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
Card card = new Card(j, suits[i]);
list.add(card);
}
}
Card card53 = new Card(0, "littleJoker");
list.add(card53);
Card card54 = new Card(0, "bigJoker");
list.add(card54);
return list;
}
public void shuffle(List<Card> list) {
Random random = new Random();
for (int i = list.size() - 1; i > 0; i--) {
int index = random.nextInt(i);
swap(list, 1, index);
}
}
public List<Card> buildCard() {
List<Card> list = buyCard();
shuffle(list);
return list;
}
}
Test
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
// 生成已经洗好的牌
CardGame cardGame = new CardGame();
List<Card> cardList = cardGame.buildCard();
//System.out.println(cardList);
// 3个人轮流抓54张牌
List<List<Card>> hands = new ArrayList<>(3); // 用二维顺序表保存数据
List<Card> hands1 = new ArrayList<>(5);
List<Card> hands2 = new ArrayList<>(5);
List<Card> hands3 = new ArrayList<>(5);
hands.add(hands1);
hands.add(hands2);
hands.add(hands3);
for (int i = 0; i < 18; i++) { // 抓牌实现
for (int j = 0; j < 3; j++) {
Card card = cardList.remove(0);
hands.get(j).add(card); //
}
}
// 打印结果
System.out.println("第一个人的牌:" + hands.get(0));
System.out.println("第二个人的牌:" + hands.get(1));
System.out.println("第三个人的牌:" + hands.get(2));
}
}
第一个人的牌:[6♥, 13♥, 2♠, 9♣, 3♠, 2♦, 12♠, 4♦, 12♦, 9♦, 5♠, 8♠, 11♠, 1♣, 4♣, 7♣, 10♣, 13♣]
第二个人的牌:[3♦, 3♥, 8♥, 11♥, 1♦, 11♦, 8♦, 10♦, 6♠, 13♠, 2♥, 9♠, 4♥, 2♣, 5♣, 8♣, 11♣, 0littleJoker]
第三个人的牌:[7♦, 5♥, 1♥, 12♥, 10♥, 5♦, 9♥, 13♦, 1♠, 4♠, 7♠, 10♠, 7♥, 3♣, 6♣, 6♦, 12♣, 0bigJoker]
参与讨论
(Participate in the discussion)
参与讨论