树状图是如何工作的?
How does Treemap works?
所以我试图从 excel 文件中读取数据,并且我使用 Treemap 来读取文件。但问题是 treemap 将我的数据随机放入其列表中。虽然,我用的时候设置了索引
//This writes the data from an old file
//writer is my treemap
//sReadData is a List with the old data
for (int i = 0; i < (sReadData.size()+1)/5; i++) {
writer.put(String.valueOf(i),new Object[] { sReadData.get(i*5),sReadData.get(i*5+1),sReadData.get(i*5+2),sReadData.get(i*5+3),sReadData.get(i*5+4)});
}
writer.put(String.valueOf(iCounter +1), new Object[] { "Name", "Date","Time", "Type","Owner"});
//This writes the new data
for (int i = 0; i < sName.length; i++) {
//iCounter is just the number of rows from the old file|These are just some Array with some data
writer.put(String.valueOf(iCounter+2+i), new Object[] { sName[i], sDate[i],sTime[i], sTypes[i],sOwners[i]});
}
我的错误是我错误地初始化了树状图。它应该是这样的: Map tMap = new TreeMap();
顺序不会是随机的。但它可能不是你所期望的。字符串按词法排序,因此 "1" < "10" < "100" < ... < "11" ... < "2".
如果您希望 TreeMap
条目按数字顺序返回,请使用 Integer
作为键类型。
When I try to use an int
instead of a String
, I get this error:
The method put(String, Object[]) in the type Map<String,Object[]>
is not applicable for the arguments (int, Object[])
您还需要将地图的声明更改为TreeMap<Integer, Object[]>
。
所以我试图从 excel 文件中读取数据,并且我使用 Treemap 来读取文件。但问题是 treemap 将我的数据随机放入其列表中。虽然,我用的时候设置了索引
//This writes the data from an old file
//writer is my treemap
//sReadData is a List with the old data
for (int i = 0; i < (sReadData.size()+1)/5; i++) {
writer.put(String.valueOf(i),new Object[] { sReadData.get(i*5),sReadData.get(i*5+1),sReadData.get(i*5+2),sReadData.get(i*5+3),sReadData.get(i*5+4)});
}
writer.put(String.valueOf(iCounter +1), new Object[] { "Name", "Date","Time", "Type","Owner"});
//This writes the new data
for (int i = 0; i < sName.length; i++) {
//iCounter is just the number of rows from the old file|These are just some Array with some data
writer.put(String.valueOf(iCounter+2+i), new Object[] { sName[i], sDate[i],sTime[i], sTypes[i],sOwners[i]});
}
我的错误是我错误地初始化了树状图。它应该是这样的: Map
顺序不会是随机的。但它可能不是你所期望的。字符串按词法排序,因此 "1" < "10" < "100" < ... < "11" ... < "2".
如果您希望 TreeMap
条目按数字顺序返回,请使用 Integer
作为键类型。
When I try to use an
int
instead of aString
, I get this error:The method put(String, Object[]) in the type Map<String,Object[]> is not applicable for the arguments (int, Object[])
您还需要将地图的声明更改为TreeMap<Integer, Object[]>
。