0

I'm trying to add 1 to an integer in a 2-dimensional ArrayList.

I'm using the set() method with the element + 1 as the second argument, but the "+ 1" isn't working. When I retrieve the element, it defines it as an object, not an integer. How do I get around this?

Code:

ArrayList<ArrayList> inventoryList = new ArrayList(
    Arrays.asList(new ArrayList<String>(), new ArrayList<Integer>()));

...

(inventoryList.get(1)).set(i, ((inventoryList.get(1)).get(i) + 1));

Error:

Main.java:47: error: bad operand types for binary operator '+'
                (inventoryList.get(1)).set(i, ((inventoryList.get(1)).get(i) + 1));
                                                                             ^

My code is at this ideone page. This code is translated from python and I'm currently debugging it so don't worry about the other errors.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
smograth
  • 63
  • 10

3 Answers3

2
ArrayList<ArrayList> inventoryList = ...

You are using the raw variant of ArrayList for your inner lists, such an ArrayList indeed contains Objects instead of Integers. You shouldn't use those raw ArrayLists and instead use generic ones:

What is a raw type and why shouldn't we use it?

Looking at your code a bit more, it seems that inventoryList is supposed to contain two lists, one that contains the items you have (as strings) and one that contains how many you have (as integers) where you can find how many you have of the item at index i in the first list by looking in the second list at that same index i.

If that is correct there are multiple ways to fix this, indeed, casting the Objects to Integers works, but then you are still using raw types, which you probably shouldn't. To fix this you should just not keep the ArrayList<String> and the ArrayList<Integer> in the same list. You could just have:

ArrayList<String> inventoryItems = ...
ArrayList<Integer> inventoryItemCounts = ...

separately (you don't need a list if it always contains exactly 2 items, a list of strings and a list of integers). However a cleaner solution would be, as was suggested in the comments by user2418306, to use a map

Map<String, Integer> inventory = ...

http://docs.oracle.com/javase/7/docs/api/java/util/Map.html, that way each string (item) in your inventory has exactly one corresponding integer (number you have of that item) and you don't have to get that by using the "at the same index" trick.

Looking at you code a bit though, I would say that more is going wrong with the inventory. You print your inventory using:

for (int i = 0; i < inventoryList.size(); i++){
    System.out.println((inventoryList.get(1)).get(i) + " : " + (inventoryList.get(0)).get(i));
}

and you iterate through it in that way in other places as well. However, if i'm not misunderstanding anything, inventoryList.size() is always going to be 2 (the inventoryList contains 2 lists, one of strings and one of integer). To get the number of distinct items (strings) in your inventory you'd have to do inventoryList.get(0).size() (or inventoryList.get(1).size() because that is going to be the same). However, things will get easier if you chose a better datatype for your inventory. I would look into the mentioned Map. Using that, you easily get the correct number using inventory.size().

Community
  • 1
  • 1
Hirle
  • 188
  • 6
  • But this isn't two arraylists of integers, this is one of integers and one of strings. – smograth Mar 05 '15 at 23:22
  • Good point, only glanced over the code, should have looked better before answering. So, looking a bit more I tried fixing my answer a bit.. – Hirle Mar 06 '15 at 00:31
0

You can solve your problem by casting to Integer

inventoryList.get(1).set(i, (Integer) inventoryList.get(1).get(i)+1);

Of course first take a look on comments below your question to see how to properly init your list, so you wont need explicit cast.

vtor
  • 8,989
  • 7
  • 51
  • 67
-1

The way it is declared, your lists are list of Object. You need to cast the result from second get() with (Integer), like:

inventoryList.get(1).set(i, (Integer)inventoryList.get(1).get(i)+1);

Note: There are unneeded ().

T.Gounelle
  • 5,953
  • 1
  • 22
  • 32