黑马程序员技术交流社区
标题:
一个练习题的求解
[打印本页]
作者:
张建银
时间:
2012-2-6 21:57
标题:
一个练习题的求解
1.编写程序利用Random类的对象的链表中一随机的顺序存储一副144张的麻将。用含有连个字符的字符串代表麻将,
例如“11”表示一万,“12”表示2万等。从栈中输出4手牌,每手牌有13张牌,手牌按万条筒风牌排序,输出时要把11显示为一万
12显示成二万这样
下面是我写出的136张麻将(除掉了花),为什么把Thread放在for循环里会报ArrasyIndexOutOf Exception,而单独newThread,不报异常,
package cn.itcast.exam;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class Majiang
{
String[] majiangzi={"一万","二万","三万","四万","五万","六万","七万","八万","九万","一条","二条","三条","四条","五条","六条","七条","八条","九条","一筒","二筒","三筒","四筒","五筒","六筒","七筒","八筒","九筒","东风","南风","西风","北风","红中","白板","发财"};
int[] majiangshu={11,12,13,14,15,16,17,18,19,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,100,101,102,103,104,105,106};
TreeMap<Integer,String> map=new TreeMap<Integer, String>();
ArrayList<Integer> al=new ArrayList<>();
public static void main(String[] args)
{
final Majiang ma=new Majiang();
ma.fetch(new ArrayList());
new Thread(new Runnable()
{
@Override
public void run()
{
ArrayList ar2=new ArrayList();
ma.fetch(ar2);
ma.al.removeAll(ar2);
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
ArrayList ar2=new ArrayList();
ma.fetch(ar2);
ma.al.removeAll(ar2);
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
ArrayList ar2=new ArrayList();
ma.fetch(ar2);
ma.al.removeAll(ar2);
}
}).start();
}
public synchronized void fetch(ArrayList al2){
for(int i=0;i<majiangshu.length ;i++){
al.add(majiangshu[i]);
al.add(majiangshu[i]);
al.add(majiangshu[i]);
al.add(majiangshu[i]);
map.put(majiangshu[i], majiangzi[i]);
}
//System.out.println(al.size());
for(int j=0;j<13;j++){
int a=new Random().nextInt(136);
int b= al.get(a);
al2.add(b);
//al.remove((Integer)b);
//System.out.println(b);
}
al.removeAll(al2);
Object[] obj=al2.toArray();
Arrays.sort(obj);
//System.out.println(obj.length);
for(Object ob:obj){System.out.print(map.get((Integer) ob)+" ");}
System.out.println();
}
}
作者:
张建银
时间:
2012-2-6 22:09
如果for(int i=0;i<3;i++){ new Thread(new Runnable()
{
@Override
public void run()
{
ArrayList ar2=new ArrayList();
ma.fetch(ar2);
ma.al.removeAll(ar2); //这里会报 ArrasyIndexOutOf Exception,而且打印出来的文字有点混乱
}
}).start();
}
作者:
黄秋
时间:
2012-2-7 02:42
你意思是,用一for 循环代替上面三个“ new Thread(new Runnable() ”,报 ArrasyIndexOutOf Exception?但我编译运行,并无错。只是你的代码有些明显错误,如:new ArrayList<>(); --- 漏了Integer .
作者:
刘基军
时间:
2012-2-7 09:46
测试结果为:
ma.al.removeAll(ar2); //报java.lang.ConcurrentModificationException异常
作者:
何洪森
时间:
2012-2-8 17:55
ArrayList<Integer> al=new ArrayList<>();
for(Object ob:obj){System.out.print(map.get((Integer) ob)+" ");}
这两行代码修改后,起初我运行是能成功的,可是当我在多长运行的时候
就出现了异常,主要都是ma.al.removeAll(ar2);处产生了异常,我所想的
是可能是这三个线程不能够同步导致的原因,因为很有可能在第一个线程
调用removeAll()方法的时候,正在移除时,第二个线程已经取走了,它认为
还存在的牌,因为java是多线程的,代码我也修改了但是还没有成功,具体
怎么回事,我也不是太明白。希望得到解决!
作者:
何洪森
时间:
2012-2-8 17:57
":o"怎么变成了{:soso_e114:} 输入法?我也没有用原码呀 :o
作者:
黄秋
时间:
2012-2-8 19:07
本帖最后由 黄秋 于 2012-2-8 19:13 编辑
下面的代码,我也是复制你的,只改了两个明显错误,运行多次正常,无异常:
import java.util.*;
public class Majiang
{
String[] majiangzi={"一万","二万","三万","四万","五万","六万","七万","八万","九万","一条","二条","三条","四条","五条","六条","七条","八条","九条","一筒","二筒","三筒","四筒","五筒","六筒","七筒","八筒","九筒","东风","南风","西风","北风","红中","白板","发财"};
int[] majiangshu={11,12,13,14,15,16,17,18,19,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,100,101,102,103,104,105,106};
TreeMap<Integer,String> map=new TreeMap<Integer, String>();
ArrayList<Integer> al=new ArrayList<Integer>();
public static void main(String[] args)
{
final Majiang ma=new Majiang();
ma.fetch(new ArrayList());
for(int i=0;i<3;i++){ new Thread(new Runnable()
{
@Override
public void run()
{
ArrayList ar2=new ArrayList();
ma.fetch(ar2);
ma.al.removeAll(ar2);
}
}).start();
}
}
public synchronized void fetch(ArrayList al2){
for(int i=0;i<majiangshu.length ;i++){
al.add(majiangshu[i]);
al.add(majiangshu[i]);
al.add(majiangshu[i]);
al.add(majiangshu[i]);
map.put(majiangshu[i], majiangzi[i]);
}
//System.out.println(al.size());
for(int j=0;j<13;j++){
int a=new Random().nextInt(136);
int b= al.get(a);
al2.add(b);
//al.remove((Integer)b);
//System.out.println(b);
}
al.removeAll(al2);
Object[] obj=al2.toArray();
Arrays.sort(obj);
//System.out.println(obj.length);
for(Object ob:obj){System.out.print(map.get((Integer) ob)+" ");}
System.out.println();
}
}
复制代码
如显示的代码与表情符号冲突,用代码标签,或自行输入[code] 标记。冒号+o 刚好是::o 。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2