如何添加(+)的整数元素在一个二维的ArrayList? [英] How can I add (+) an integer to an element in a 2-dimensional ArrayList?

查看:310
本文介绍了如何添加(+)的整数元素在一个二维的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个2维的ArrayList加1的整数。

我使用了设置()方法与元素+ 1 作为第二个参数,但+ 1是行不通的。当我检索元件,它把它定义为一个对象,而不是一个整数。我该如何解决这个问题?

code:

 的ArrayList< ArrayList的> inventoryList =新的ArrayList(
    Arrays.asList(新的ArrayList<串GT;(),新的ArrayList<整数GT;()));

...

 (inventoryList.get(1))集(I,((inventoryList.get(1))获得(I)+ 1))。

错误:

  Main.java:47:错误:错误的操作数类型的二进制运算符+
                (inventoryList.get(1))组(也就是,((inventoryList.get(1))获得(I)+ 1));
                                                                             ^

我的code是在这个ideone页。这code是由蟒蛇翻译,我目前正在调试它,所以不要担心其他错误。


解决方案

 的ArrayList< ArrayList的> inventoryList = ...

您正在使用ArrayList的为你的内在名单的原始变体,例如一个ArrayList确实包含的对象,而不是整数。你不应该使用那些原始的ArrayList,而使用一般的:

什么是原始类型和原因我们不应该使用它呢?

看你的codeA一点,似乎inventoryList应该包含两个列表,包含你的项目(字符串)和一个包含你有多少(整型),您可以发现你有多少在第一个列表指数 I 商品具有通过相同指数 I

如果这是正确的有多种方式来解决这个问题,的确,铸造对象为整数的作品,但你还在使用原始的类型,你可能不应该。为了解决这个问题,你应该只是没有保持的ArrayList<弦乐> 的ArrayList<整数GT; 在同一列表。你可以只是:

 的ArrayList<串GT; inventoryItems = ...
ArrayList的<整数GT; inventoryItemCounts = ...

分开(你并不需要一个列表中,如果它总是正好包含2项,字符串列表和整数列表)。然而,一个更清洁的解决方案是,如在评论中建议由user2418306,使用地图

 地图<字符串,整数>库存= ...

http://docs.oracle.com/ JavaSE的/ 7 /文档/ API / JAVA / UTIL / Map.html ,在您的存货那样每个字符串(项目)有(你有项目的数量)恰好一个对应的整数,你不必获得通过使用同一指数的把戏。

看着你code一点,虽然,我会说更多的是用库存去错了。您可以使用打印清单:

 的for(int i = 0; I< inventoryList.size();我++){
    的System.out.println((inventoryList.get(1))获得(I)+:+(inventoryList.get(0))获得(I));
}

和你通过它在其他地方那样,以及迭代。但是,如果我没有误会什么, inventoryList.size()总是将是2(inventoryList包含2个列表,其中一个字符串和整数之一)。为了获得在清单中不同项目(串)的数量,你不得不做的 inventoryList.get(0).size()(或 inventoryList.get(1).size()因为将是相同的)。但是,如果你选择了一个更好的数据类型为您的库存事情会变得更加容易。我会去了解一下所提到的地图。利用这一点,您可以轻松地使用得到正确的数量 inventory.size()

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.

解决方案

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

这篇关于如何添加(+)的整数元素在一个二维的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆