我觉得还是用HashMap,因为其是Collection框架之中的类,Hashtable是继承自Dictionary,
不需要同步时用Hashtable没必要吧,况且利用Collections类也可以得到同步的Map,
楼主把源代码帖出来看看

解决方案 »

  1.   

    ArrayIndexOutOfBoundsException数组越界!
      

  2.   

    ArrayIndexOutOfBoundsException主要是发生在数组的index越界时,看看你不是访问了不存在的元素;
    HashMap比Hashtable效率高,但是它是unsynchronized
      

  3.   

    可以去看看AbstractCollection.java的源代码125行,里面具体在做什么
      

  4.   

    AbstractCollection.java中125行:从集合中取值,然后放到数组中从错误信息看,肯定是集合中的元素大于声明的数组长度
      

  5.   

    将错误的stack打出来, 分析以下jdk的源码就知道了
      

  6.   

    key值越界?
    关注! 里面的数据结构一点都不会-_-
      

  7.   

    如果一直没有问题,那可能是多线程中的问题。改用Hashtable.
      

  8.   

    线程并发造成的。
    toArray的时候,HashMap中可能有n个元素,当使用System.arraycopy的时候,可能已经被其他线程插入了m个新元素,此时为n+m,所以发生数组越界。这个异常应该是发生在toArray()时。因此必须在toArray的时候对HashMap进行Synchronized. 不必一定使用Hashtable.
      

  9.   

    哦,刚才看了一下AbstractCollection的源码,实现不是使用的System.arraycopy,是用iterator遍历然后赋值,不过都一样了。:)
      

  10.   

    一共有20个hashmap对象,连续运行一个多礼拜后其中一个对象发生这样得错误,我在进行插入和删除节点的时候使用Synchronized,其它的还对对象进行遍历操作,下面是部分代码
    public class KTV_room{
      public String K_RoomId;       //-- 房间的Id
      public String K_RoomName;     //-- 房间的名字
      public int K_RoomNum=0;       //-- 房间播放歌曲的最后Id(每点一首歌曲,自增一)
      public HashMap K_Room;        //-- 保存一个房间目前点的歌曲
      public int K_SongNum=0;       //-- 目前有多少首歌曲
      public int K_MaxNum=0;        //-- 一个房间最多可以容纳多少人
      public String strErr="";  public int insertDot(String AUserId,String ASongId,String ASongName,String ASinger
    ,String ASongFileName){
        if (AUserId==null || ASongFileName==null){
      strErr+="no user or no file!";
          return -1;
    }    synchronized(K_Room){
          if (K_SongNum>=K_MaxNum) return 1;      KTV_room_dot tmpDot=new KTV_room_dot();
      tmpDot.D_UserId=AUserId;
      tmpDot.D_Id=getNextId();
      tmpDot.D_SongId=ASongId;
      tmpDot.D_SongName=ASongName;
      tmpDot.D_Singer=ASinger;
      tmpDot.D_SongFileName=ASongFileName;   K_Room.put(new Integer(tmpDot.D_Id),tmpDot); 
      K_SongNum++;
    } return 0;
      }  public int deleteDot(int ADeleteId){
        synchronized(K_Room){
          Object tmpObj=searchDot(ADeleteId);
      if (tmpObj==null) return -1;
      K_Room.remove(tmpObj); 
      K_SongNum--;
    }
    return 0;
      }  public String[][] getSongList(){
    Object[] tmpObjs=K_Room.values().toArray();
        KTV_room_dot tmpDot=null;
    String[][] tmpOutArray=null;
        sort(tmpObjs);
        if (tmpObjs!=null){
      tmpOutArray=new String[tmpObjs.length][6];
          for (int i=0;i<tmpObjs.length;i++){
            tmpDot=(KTV_room_dot)tmpObjs[i];
    tmpOutArray[i][0]=tmpDot.D_UserId;
    tmpOutArray[i][1]=tmpDot.D_Id+"";
    tmpOutArray[i][2]=tmpDot.D_SongId;
    tmpOutArray[i][3]=tmpDot.D_SongName;
    tmpOutArray[i][4]=tmpDot.D_Singer;
    tmpOutArray[i][5]=tmpDot.D_SongFileName;
      }
    }
    return tmpOutArray;
      }
      public Object searchDot(int ANo){
    Object[] tmpObjs=K_Room.keySet().toArray();
        Integer tmpDot=null;
    if (tmpObjs==null && tmpObjs.length==0) return null;
    for (int i=0;i<tmpObjs.length;i++){
          if (((Integer)tmpObjs[i]).intValue()==ANo)
            return tmpObjs[i];
    }
        return null;
      }
      

  11.   

    代码很另类,完全是C的风格。
    我看就别用HashMap了,你用list吧。到处都是数组,看着眼睛都疼。
      

  12.   

    public String[][] getSongList(){
    // 此处对K_Room加上synchronized试试....
    Object[] tmpObjs=K_Room.values().toArray();
        KTV_room_dot tmpDot=null;
    String[][] tmpOutArray=null;
        sort(tmpObjs);
        if (tmpObjs!=null){
      tmpOutArray=new String[tmpObjs.length][6];
          for (int i=0;i<tmpObjs.length;i++){
            tmpDot=(KTV_room_dot)tmpObjs[i];
    tmpOutArray[i][0]=tmpDot.D_UserId;
    tmpOutArray[i][1]=tmpDot.D_Id+"";
    tmpOutArray[i][2]=tmpDot.D_SongId;
    tmpOutArray[i][3]=tmpDot.D_SongName;
    tmpOutArray[i][4]=tmpDot.D_Singer;
    tmpOutArray[i][5]=tmpDot.D_SongFileName;
      }
    }
      

  13.   

    在 HasmMap中保存的values对象可以维持其原来的属性吗?
    假设class1 object=new class1();
    hashMap.put(new Integer(1),object);
    那么以后从HashMap里面再把这个对象拿出来的时候,object是否还能作为class1的对象调用class1//de//属性?若能,请给个例子!谢谢