How can I add (+) an integer to an element in a 2-dimensional ArrayList?

♀尐吖头ヾ 提交于 2020-01-07 07:39:18

问题


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.


回答1:


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().




回答2:


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.




回答3:


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 ().



来源:https://stackoverflow.com/questions/28884816/how-can-i-add-an-integer-to-an-element-in-a-2-dimensional-arraylist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!