简体   繁体   中英

2D Array but with indexing - java

I'm looping into a number of rows and trying to filter these rows with some if statements. within each if statement I need to have an index for a number of elements. 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.

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:

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. Thanks.

You could try an ArrayList of ArrayList 's:

    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. 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 . With this for you are "creating the empty rows of your array":

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:

1) If you do it with a pre-allocated fixed array, you obviously need two passes. 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

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.

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:

You should use solution 1) above.

If your index is not consecutive and n is known known in advance:

3) You create a Map<Integer,String> strings and add the elements via strings.put(index, string) (in any order).

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

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:

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

And then use a for-each loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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