简体   繁体   English

Java-如何在多维数组列表上添加元素?

[英]Java - How to add elements on a multidimensional arraylist?

I have this array: ArrayList<ArrayList<Integer>> items = new ArrayList<ArrayList<Integer>>(); 我有这个数组: ArrayList<ArrayList<Integer>> items = new ArrayList<ArrayList<Integer>>();

I want to create this array on a for cicle and the result must be something like: 我想创建一个数组for cicle,其结果一定是这样的:

[0]: {0 , 0, 0, 3};
[1]: {1 , 2, 3, 3};
[2]: {6 , 2, 5, 4};

During the cicle, new values must be added to the ArrayList in a certain index . 在循环期间,必须将新值添加到特定index的ArrayList中。 The index can contains values or not, but they must not be overwriten. index可以包含或不包含值,但不得覆盖它们。

Can i add only arrays to a certain index like items.add(index, array); 我可以只将数组添加到某些index例如items.add(index, array); ?

So, how to add more values without overwrite anything? 那么,如何在不覆盖任何内容的情况下添加更多值?

Best solution is to use HashMap, like this: 最好的解决方案是使用HashMap,如下所示:

HashMap<Integer, int[]> items = new HashMap<Integer, int[]>();

items.put(0, new int[] {0 , 0, 0, 3});
items.put(1, new int[] {1 , 2, 3, 3});
items.put(2, new int[] {6 , 2, 5, 4});

Then you can set and get any value at any given key, even if the key does not exist yet. 然后,即使该键尚不存在,也可以在任何给定的键上设置并获取任何值。

You can sipmly use: 您可以简单地使用:

int[] row = items.get(0);

to get the desired row. 获得所需的行。

If I understand correctly, you are trying to add whole integer array(s) to an ArrayList>. 如果我理解正确,则您正在尝试将整个整数数组添加到ArrayList>中。 In this case you might try something like: 在这种情况下,您可以尝试以下操作:

    ArrayList<ArrayList<Integer>> items = new ArrayList<ArrayList<Integer>>();
    Integer[] intList1 = {0,0,0,3};
    Integer[] intList2 = {1,2,3,3};
    Integer[] intList3 = {6,2,5,4};
    items.add(new ArrayList<Integer>(Arrays.asList(intList1)));
    items.add(new ArrayList<Integer>(Arrays.asList(intList2)));
    items.add(new ArrayList<Integer>(Arrays.asList(intList3)));

Or these int arrays can be added to the ArrayList in a loop, instead of individually. 或者这些int数组可以循环添加到ArrayList中,而不是单独添加。

items.get(i).add(j);

其中ij是整数。

From what I understood, in each iteration of your loop, you will have an index and an integer. 据我了解,在循环的每次迭代中,您都会有一个索引和一个整数。 You want to insert the integer into the ArrayList at the specified index, if one exists. 您希望将整数插入指定索引处的ArrayList中(如果存在的话)。 Or are you looping over a 2-dimensional array? 还是要遍历二维数组?

Consider using a HashMap where the key is the index (Integer), and the value is an ArrayList: 考虑使用HashMap,其中键是索引(Integer),值是ArrayList:

HashMap<Integer, ArrayList<Integer>> items = new HashMap<>();

// Given an index, i, and an integer, n 
// (e.g. obtained while looping over your data):

if (items.containsKey(i)){
    // Your HasMap contains an ArrayList at this index / key
    // Add the number to the end of the list

    items.get(i).add(n);

} else {
    // There is no ArrayList stored yet at this index / key
    // Create a new list containing only the number

    ArrayList<Integer> list = new ArrayList<>();
    list.add(n);

    items.put(i, list);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM