package cn.edu.lcudcc.collection_test; public class Card { private String size; private String color; private int index; // 真正的大小 public Card() { } public Card(String size, String color, int index) { this.size = size; this.color = color; this.index = index; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } @Override public String toString() { return size + color; } }
package cn.edu.lcudcc.collection_test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class GameDemo { public static List<Card> allCards = new ArrayList<>(); static { String[] sizes = {"3","4","5","6","7","8","9","10","J","Q","K","1","2"}; String[] colors = {"♠","♥","♣","♦"}; int index = 0; // 记录牌的大小 for (String size : sizes) { index++ ; for (String color : colors) { Card c = new Card(size, color, index); allCards.add(c); } } Card c1 = new Card("","小 标签:index,Java,String,斗地主,color,分牌,public,Card,size From: https://www.cnblogs.com/zhzhang/p/16972967.html