1

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.

3
  • Couldnt you use an array of List<String>`? Commented Oct 26, 2013 at 6:53
  • @StefanFreitag I could use it if I didn't need to index the element. Since I don't know what if statement will occur, I need to have sort of indexing (hard coded) as a reference for each list. Commented Oct 26, 2013 at 7:07
  • I am confused. What is the that "index"? The row number or the column number? Commented Oct 28, 2013 at 14:05

3 Answers 3

2

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...
Sign up to request clarification or add additional context in comments.

5 Comments

This works only if I add one element but if I do something like the following it throws IndexOutOfBoundsExpection : ` strings.get(0).add(" String1"); strings.get(2).add(" String2"); strings.get(3).add(" String3"); strings.get(3).add(" String4"); `
You said you knew the number of rows, so first add n arrays to the array of arrays. See edit
I think I got it. There is a chance for you to know how many rows will you need at MAX??
So you want to store a certain number of elements(unknown) in a certain number of rows (unknown). IN your first for you are looping over what? Over the array of arrays?
well I'm looping over an Arraylist in the very first loop and with my if statements I check to a number of data (parsing by ",") in side it. Then I end up with the need of a known number of columns (equals if statements/result) plus an unknown number of rows for each.
1

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);
}

8 Comments

The solution should be of the second kind but not sure how to do it? List doesn't work for me. see my edit.
I believe you have to make your question more clear. Your index is not consecutive from 0 to n. That is your problem. Right? In that case, you need a map. Also: What is your key, the index or the string? If you like to get the index for a given string, then the map is the other way around. Maybe you have to formulate the question more precise
I'm trying to explain as much as I can. My index could be a known number starting from 0 but for each index I need to have an unknown number of elements considering that one element can can be inserted at one time. Also The first element maybe stored at, for example, index 5.
This is exactly what a map can do for you. See my answer above. Map<Integer,String> strings supports a call like strings.put(5, "bla"); as its first call.
List listForIndex = map.get(index); if(listForIndex == null) listForIndex = new ArrayList<String>(); listForIndex.add(string);
|
0

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

1 Comment

It doesn't work with me when I use ` add(int index, E element)` method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.