简体   繁体   English

2D数组但带索引 - java

[英]2D Array but with indexing - java

I'm looping into a number of rows and trying to filter these rows with some if statements. 我循环到许多行,并试图用一些if语句过滤这些行。 within each if statement I need to have an index for a number of elements. 在每个if语句中,我需要有一些元素的索引。 I could have done that using 2d String[][] but the problem is I don't know what is the size of each row at this stage. 我本可以使用2d String [] []来做到这一点,但问题是我不知道这个阶段每行的大小是多少。

I'm looking to store my data like the following : 我想要存储我的数据,如下所示:

    0     1    3    4    5    6    7  etc.. 
 0 str   str  str  str  str  str  str
 1 str   str  str  
 2 
 3 str   str  str  str  str  str  

Any suggestion would be appreciate it 任何建议都会很感激

 Edit:

Sorry if my question wasn't clear. 对不起,如果我的问题不明确。 But I'll explain it more here. 但我会在这里解释一下。

My Loop looks like this: My Loop看起来像这样:

newArrayList
for (i; i < List ;i++)
{
  if(...)
  {
    newArrayList.add.(0, List.get(i));
  } else if(...)
  {
    newArrayList.add.(2, List.get(i));
  } else if(...)
  {
    newArrayList.add.(6, List.get(i));
  }
 }

The above code doesn't work but I'm just trying to explain what I need to do actually! 上面的代码不起作用,但我只是想解释一下我需要做什么! My if statements can occur several times and I would like to consider an index for each if statement expectation plus a set of strings. 我的if语句可以多次出现,我想考虑每个if语句期望的索引加上一组字符串。 Thanks. 谢谢。

You could try an ArrayList of ArrayList 's: 您可以尝试ArrayListArrayList

    ArrayList<ArrayList<String>> strings = new ArrayList<ArrayList<String>>();
    strings.add(new ArrayList<String>()); // Adding a first array to the 'array of arrays'
    strings.get(0).add("String1"); // Add a string to the first array,
                                   // Similar to: arr[0][0] = "String1"

    //To access them element by element use a double for, note that "s" is each element
    for (ArrayList<String> l : strings) {
        for (String s : l) {

        }
    }

PS: An ArrayList<Object> is like an array Object[] but more flexible. PS: ArrayList<Object>就像一个数组Object[]但更灵活。 It has some useful methods like: 它有一些有用的方法,如:

arr_list.get(index); // Getting an object in position 'index'
arr_list.add(object); // Adding an element (Similar to assignment in arrays)

Edit 编辑

If you know the number of "rows" then you have to add them to the array of arrays . 如果您知道“行”的数量,则必须将它们添加到array of arrays With this for you are "creating the empty rows of your array": 有了这个for你就是“创建数组的空行”:

Rows:
   0
   1
  ...
   n


for (int i = 0; i < n; i++) {    // n is the number of "rows"
    strings.add(new ArrayList<String>());
}

Then add an element to a "row": 然后将一个元素添加到“行”:

strings.get(0).add("String1"); // get(0) to obtain the first row, get(1) to obtain the second...

If your index is consecutive form 0 to n and you are inserting them in that order, but n is not known in advance: There are two classical solution: 如果您的索引是从0到n的连续形式,并且您按顺序插入它们,但是事先不知道n:有两种经典的解决方案:

1) If you do it with a pre-allocated fixed array, you obviously need two passes. 1)如果你使用预先分配的固定阵列进行,你显然需要两次传球。 The first pass is scanning the row and counting the elements. 第一步是扫描行并计算元素。 The second pass is then creating the index. 第二遍是创建索引。

2) You can do it with a collection allowing dynamic growth via an .add(item) method, like List 2)你可以通过一个允许通过.add(item)方法(如List)进行动态增长的集合来实现

If you will convert the collection to an fixed size array later, then it is maybe faster to use method 1) since the add method may be slower due to memory management / allocation / re-allocation. 如果稍后将集合转换为固定大小的数组,则使用方法1)可能会更快,因为由于内存管理/分配/重新分配,add方法可能会更慢。

If your index is consecutive form 0 to n and n is known in advance, but you are inserting the elements not in that order: 如果您的索引是从0到n的连续形式,并且n是事先已知的,但您要按顺序插入元素:

You should use solution 1) above. 你应该使用上面的解决方案1)。

If your index is not consecutive and n is known known in advance: 如果您的索引不是连续的,并且事先已知n:

3) You create a Map<Integer,String> strings and add the elements via strings.put(index, string) (in any order). 3)您创建Map<Integer,String> strings并通过strings.put(index, string)添加元素(以任何顺序)。

If your index is not unique (as we have finally found out): 如果您的索引不是唯一的(正如我们最终发现的那样):

4) You crate a Map<Integer,ArrayList<String>> stringMap and add elements via 4)你创建一个Map<Integer,ArrayList<String>> stringMap并通过添加元素

addStringForIndex(String string, Integer index)
{
    listForString = stringMap.get(index);
    if(listForString == null) {
        listForString = new ArrayList<String>;
        map.put(index, listForString);
    }
    listForString.add(string);
}

If you don't know the size of your array, you could use a List implementation, for example: 如果您不知道数组的大小,可以使用List实现,例如:

ArrayList<ArrayList<String>> 2D = new ArrayList<ArrayList<String>>();

And then use a for-each loop 然后使用for-each循环

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

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