关于HashMap的问题 我觉得还是用HashMap,因为其是Collection框架之中的类,Hashtable是继承自Dictionary,不需要同步时用Hashtable没必要吧,况且利用Collections类也可以得到同步的Map,楼主把源代码帖出来看看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ArrayIndexOutOfBoundsException数组越界! ArrayIndexOutOfBoundsException主要是发生在数组的index越界时,看看你不是访问了不存在的元素;HashMap比Hashtable效率高,但是它是unsynchronized 可以去看看AbstractCollection.java的源代码125行,里面具体在做什么 AbstractCollection.java中125行:从集合中取值,然后放到数组中从错误信息看,肯定是集合中的元素大于声明的数组长度 将错误的stack打出来, 分析以下jdk的源码就知道了 key值越界?关注! 里面的数据结构一点都不会-_- 如果一直没有问题,那可能是多线程中的问题。改用Hashtable. 线程并发造成的。toArray的时候,HashMap中可能有n个元素,当使用System.arraycopy的时候,可能已经被其他线程插入了m个新元素,此时为n+m,所以发生数组越界。这个异常应该是发生在toArray()时。因此必须在toArray的时候对HashMap进行Synchronized. 不必一定使用Hashtable. 哦,刚才看了一下AbstractCollection的源码,实现不是使用的System.arraycopy,是用iterator遍历然后赋值,不过都一样了。:) 一共有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; } 代码很另类,完全是C的风格。我看就别用HashMap了,你用list吧。到处都是数组,看着眼睛都疼。 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; }} 在 HasmMap中保存的values对象可以维持其原来的属性吗?假设class1 object=new class1();hashMap.put(new Integer(1),object);那么以后从HashMap里面再把这个对象拿出来的时候,object是否还能作为class1的对象调用class1//de//属性?若能,请给个例子!谢谢 java中的ArrayList中的addAll方法 java如何压缩一个空文件夹 wsad 和ibm application developer是一个东西吗? 初学java应该先看些什么书籍呢?java里面的东西是不是很多,什么j2ee,j2se到底是些什么东西呢? 鸟鸟问题:为什么我想打开数据库时,说找不到jdbc driver?? 为何我的程序不能导入corejava包? 俩个线程输出1-30 谁看过韩顺平老师QQ 92讲多对多聊天 新手eclipse快捷键求解 JS语言提问 java有没有类似listview的组件 java 怎样调用传真的功能
HashMap比Hashtable效率高,但是它是unsynchronized
关注! 里面的数据结构一点都不会-_-
toArray的时候,HashMap中可能有n个元素,当使用System.arraycopy的时候,可能已经被其他线程插入了m个新元素,此时为n+m,所以发生数组越界。这个异常应该是发生在toArray()时。因此必须在toArray的时候对HashMap进行Synchronized. 不必一定使用Hashtable.
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;
}
我看就别用HashMap了,你用list吧。到处都是数组,看着眼睛都疼。
// 此处对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;
}
}
假设class1 object=new class1();
hashMap.put(new Integer(1),object);
那么以后从HashMap里面再把这个对象拿出来的时候,object是否还能作为class1的对象调用class1//de//属性?若能,请给个例子!谢谢