I have this problem that I don't know how to approach. I have two array list, one that contains String and the other one containing integers, like this:
* Utah 5
* Nevada 6
* California 12
* Oregon 8
* Utah 9
* California 10
* Nevada 4
* Nevada 4
* Oregon 17
* California 6
and i need to produce a result like this in array lists: Utah [5, 9] Nevada [6, 4, 4] California [12, 10, 6].
I honestly don't know how to approach this and I'm looking an advice on how to do it.
I was suggested to use arraylist Of arraylist and this is what I have so far.
int i=0;
ArrayList<ArrayList<Integer>> values = new ArrayList<ArrayList<Integer>>();
while (i<categories.size())
{
String cat=categories.get(i);
i++;
for (int r = 0; r < column1.size();r++) {
if(column1.get(r)==cat)
{
int value = column2.get(r);
ArrayList<Integer> val=new ArrayList<Integer>();
val.add(value);
values.add(val);
}
}
Column1=states list, column2=the integers list and categories= categories list.
My problem now is that the for loop seems to just be adding the same array over and over until the while loop is done. Any suggestions?
Map<String, List<Integer>>?Multimapimplementation, which lets you easily and clearly associate keys with multiple values. As a homework assignment using an external library may not be the point of the exercise, but the "right" way to address this type of problem is with aMultimap.