赞
踩
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。
public class MyArraylist {
public int[] elem;
public int usedSize;
public final static int DEFAULT_SIZE = 10; //为数组设置初始容量
public MyArrayList(){
//用数组实现空顺序表
this.elem = new int[DEFAULT_SIZE];
}
}
//增加元素
public void add(int val){
if(isFull()){ //数组满了需要扩容,这里设置二倍扩容
this.elem = Arrays.copyOf(array, 2*array.length);
}
//数组未满,添加元素,usedSize加 1
elem[usedSize] = val;
usedSize++;
}
public boolean isFull(){ //判断数组满不满
return elem.usedSize == elem.length){
}
//删除指定元素 public void delete(int value){ //判断顺序表是否为空,为空抛出错误,或设置提示方法 if(isEmpty()){ System.out.println("顺序表为空,无法删除"); return; } int index = -1; for(int i = 0; i < elem.length; i++){ if(elem[i] == value){ index = i; } } if(index == -1){ System.out.println("要删除的元素不在顺序表中"); }else{ //将index后面的元素向前挪一位 for(int i = index; i < elem.length; i++){ elem[i] = elem[i+1]; } usedSize--;//注意最后顺序表的usedSize要减1 } }
//在pos位置新增元素 public void addData(int pos, int value){ //先判断pos的合法性 if(pos < 0 || pos > usedSize){ System.out.println("要添加的元素位置不合法!"); return; } //判断数组满不满 if(isFull()){ this.elem = Arrays.copyOf(array, 2*array.length); } //添加元素,需要先把pos之后的元素向后挪一位,再添加value for(int i = elem.usedSize; i > pos; i++){ elem[i] = elem[i-1]; } elem[pos] = value; usedSize++; }
public void clear(){
this.usedSize = 0;
}
首先,这个游戏的逻辑功能是,买牌(建立顺序表),发牌(随机将元素分为三组),揭牌
public class Poker { //买一副扑克牌,52张牌,四个花色 private String color; private String num; public Poker(String color, String num) { this.color = color; this.num = num; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } @Override public String toString() { return color + " " + num; } }
public class Pokers { //买牌,洗牌,揭牌 public static final String[] COLOR = {"♥","♦","♣","♠"}; public static final String[] NUM = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; //买一副扑克牌 public static List<Poker> buyPokers(){ List<Poker> pokerlist = new ArrayList<>(); for(int i = 0; i < 4; i++){ for(int j = 0; j < 13; j++){ //买一张牌 Poker poker = new Poker(COLOR[i],NUM[j]); pokerlist.add(poker); } } return pokerlist; } }
//洗牌,交换 public static void shuffle(List<Poker> pokerList){ Random random = new Random(); for(int x = pokerList.size()-1; x > 0; x--){ //random.nextInt(x),随机获取0~x-1的数字 int index = random.nextInt(x); swap(pokerList,x,index); } } //swap函数 public static void swap(List<Poker> pokerList, int x, int index){ //换牌用set(设置),get(获取) Poker tmp = pokerList.get(x); pokerList.set(x,pokerList.get(index)); pokerList.set(index,tmp); }
public static void main(String[] args){ List<Poker> pokerList = buyPokers(); System.out.println("买牌"); System.out.println(pokerList); System.out.println("========================="); System.out.println("洗牌"); shuffle(pokerList); System.out.println(pokerList); System.out.println("========================="); //揭牌,四个人,轮流揭牌五张 //定义二维数组,hand的每个成员是List<Poker>类型 List<List<Poker>> hand = new ArrayList<>(); List<Poker> hand1 = new ArrayList<>(); List<Poker> hand2 = new ArrayList<>(); List<Poker> hand3 = new ArrayList<>(); hand.add(hand1); hand.add(hand2); hand.add(hand3); //二维数组初始化 for(int i = 0; i < 17; i++){ for(int j = 0; j < 3; j++){ List<Poker> handpoker = hand.get(j); //remove(0)返回值就是下标为0的元素 handpoker.add(pokerList.remove(0)); } } for(int i = 0; i < hand.size(); i++){ System.out.println("第"+i+"个人的牌是"+hand.get(i)); } System.out.println("剩余的牌"); System.out.println(pokerList); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。