日期:2014-05-20  浏览次数:20835 次

用ArrayList实现给3个人发牌功能,但老是报越界错误,实在看不出来有什么可以改的地方啊?请高手多多指教
import java.util.ArrayList;

public class E1Divison {

public static void main(String[] args) {
play();
}

public static void play(){
String [] dcolor={"红桃","黑桃","方块","梅花"};
String [] dvalue={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

ArrayList<String> list = new ArrayList<String>();
for(int i=0;i<dcolor.length;i++){
for(int j =0 ;j<dvalue.length;j++){
list.add(dcolor[i]+dvalue[j]);
}
}
//删除含这四张牌的对象
  list.remove("红桃2");
list.remove("方块2");
list.remove("梅花2");
list.remove("方块A");

for(int dn=0;dn<=2;dn++){
System.out.println((dn+1)+"的牌是:");
for(int n=0;n<16;n++){
String resu = (String) list.get((int) (Math.random()*48));
System.out.print(resu+" ");
list.remove(resu);
}
System.out.println();
}
}

}

------解决方案--------------------
String [] dcolor={"红桃","黑桃","方块","梅花"};
String [] dvalue={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

ArrayList<String> list = new ArrayList<String>();
for(int i=0;i<dcolor.length;i++){
for(int j =0 ;j<dvalue.length;j++){
list.add(dcolor[i]+dvalue[j]);
}
}
//删除含这四张牌的对象 
list.remove("红桃2");
list.remove("方块2");
list.remove("梅花2");
list.remove("方块A");

for(int dn=0;dn<=2;dn++){
System.out.println((dn+1)+"的牌是:");
for(int n=0;n<16;n++){ 
int a=(3-dn)*16-n;
String resu = (String) list.get((int) (Math.random()*a));
System.out.print(resu+" ");
list.remove(resu);
}
System.out.println(); 
}
}

看下帮你改了一下