Speed question: 2D array vs. Table

Excuse my question.

I need to fill an 2D array with Strings.

Then I want to look up / search certain strings in a precise column.

Would the access be faster using a table with 30
Columns instead of a 2D array?

Using a Table container we would invoke its findRow() method for a unique String search:

Which in turn invokes method findRowIndex():

A 2d array custom column lookup would be faster of course.
I just dunno how much faster compared to the Table’s algorithm though.
It’s a classical convenience vs. speed!

1 Like

Thank you.

I didn’t know that 2D array would be faster. I would habe to write a for loop to search the String?

Anyway, to build the table from scratch from the data would also be much work.

I’ve just done 1 for you now. However I haven’t tested it yet: :warning:

static final String[]
  findRow(final String[][] table, final String word, final int col)
{
  final int row = findRowIndex(table, word, col);
  return row >= 0? table[row] : null;
}

static final int
  findRowIndex(final String[][] table, final String word, int col)
{
  if (table != null && word != null && (col = abs(col)) < table[0].length)
    for (int row = 0; row < table.length; ++row)
      if (word.equals(table[row][col]))  return row;

  return -1;
}
1 Like

I guess when you say 2D array is faster I believe you and I write a for loop to search the 2D array

Or was it an misunderstanding?

Compare the lookup algorithm I did for a 2D array of strings and the Table’s implementation. :eyeglasses:

I didn’t benchmark them either; but it seems like mine would win the race. :horse_racing:

1 Like

Now I understand. Thanks! The second code is for a 2D array.

Thank you!