Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java映射/树映射:修改现有条目而不删除条目_Java_Treemap - Fatal编程技术网

Java映射/树映射:修改现有条目而不删除条目

Java映射/树映射:修改现有条目而不删除条目,java,treemap,Java,Treemap,所以我有一张地图定义为 Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>(); 所以,如果我要添加一个预期稍后会有数据的条目,我需要添加(编辑:它可以被追加),有没有办法保留现有数据并追加新数据?例如 首先, data.put(counter, new Object[] {"testData1", "testData2", "testData3", "testData4"}); 当我循环到需要添加

所以我有一张地图定义为

Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>();
所以,如果我要添加一个预期稍后会有数据的条目,我需要添加(编辑:它可以被追加),有没有办法保留现有数据并追加新数据?例如

首先,

data.put(counter, new Object[] {"testData1", "testData2", "testData3", "testData4"});
当我循环到需要添加的数据时,我需要能够将“testData5”添加到结束位置,同时只知道最初添加数据时计数器的值

有没有一种方法可以在不删除该特定条目中的现有数据的情况下执行此操作


编辑:数据可以被追加,改变了这个例子。

使用数组,它非常混乱。我同意你应该使用列表的意见,它允许你只使用列表引用,而不必在地图上设置任何内容

使用数组(讨厌!)
while(/*花式参数*/){
int counter;//你答应过我的
字符串参数;//这个
Object[]foo=data.get(counter);//查找地图上存储的内容
Object[]bar;//我们的预期结果
如果(foo==null){//创建一个新数组并附加它
条=新对象[1];
bar[0]=参数;
}
否则{//填充新数组
条形=新对象[foo.length+1];
for(int i=0;i
使用列表(干净!)
while(/*花式参数*/){
int counter;//你答应过我的
字符串参数;//这个
List foo=data.get(counter);//查找地图上存储的内容
//我们不需要酒吧,因为我们可以和福一起玩
如果(foo==null){//我们需要添加一个新列表
foo=newarraylist();
data.set(counter,foo);//把它放到地图上
}
add(参数);//添加到列表,不需要重新放置,因为它已经存在
}

在本例中,您无法知道数据必须放在数组的第三个元素中?我不确定我是否理解您的问题。让我修改一下,需要添加的元素可以追加,但我需要保留所有其他信息。使用列表而不是数组如何?我不清楚你在问什么。数组不能更改大小。您是想要一个
列表
作为值类型,还是想要一个
多映射
,其中一个键可以发生多个映射?
data.put(counter, new Object[] {"testData1", "testData2", "testData3", "testData4"});
while(/* fancy argument */) {
    int counter; //you promised me this
    String argument; //and this

    Object[] foo = data.get(counter); //find what is stored on the map  
    Object[] bar; //our expected result

    if(foo == null) { //create a new array and append it
        bar = new Object[1];
        bar[0] = argument;
    }
    else { //fill the new array
        bar = new Object[foo.length+1];
        for(int i = 0; i < foo.length; i++) {
            bar[i] = foo[i];
        }
        bar[foo.length] = argument;
    }

    data.set(counter, bar); //put the updated array into the map
}
while(/* fancy argument */) {
    int counter; //you promised me this
    String argument; //and this

    List<Object> foo = data.get(counter); //find what is stored on the map  
    //and we don't need bar since we can play with foo

    if(foo == null) { //we have a new list we need to add
        foo = new ArrayList<>();
        data.set(counter, foo); //put it in the map
    }

    foo.add(argument); //add to list, don't need to re-put since it's already there
}