So I have a Map defined as
Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>();
and I am adding data as I loop through and read a text file. For most of the data, I'll add it and move on. However, there is data in the file that needs to applied back to an existing entry without deleting it or removing it's position. Example.
data.put(counter, new Object[] {ex1, ex2, ex3, ex4});
So, if I were to add an entry expecting to have data later on I would need to add (EDIT: it can be appended), is there way to keep the existing data and append new data? Example.
First,
data.put(counter, new Object[] {"testData1", "testData2", "testData3", "testData4"});
When I loop to data that needs to be added, I need to be able to add "testData5" to the end position while only knowing the counter's value when the data was originally added.
Is there a way to do this without deleting the existing data in that specific entry?
Edit: The data can be appended, changed example for this.
Using your arrays, it's quite messy. I agree with the comments that you should use Lists, which allow you to just use the list reference and not have to set anything back on the map.
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
}