You can not check the equality of String objects with the ==
operator.
You have to use .equals()
instead. This question has been asked many times in StackOverflow and this answer, explains it concisely.
What you want to do, is something like this:
final String ARRAY_NAME1 = "row0";
final String ARRAY_NAME2 = "row1";
String index = "row0";
if(index.equals(ARRAY_NAME1))
System.out.print(row0[1]);
else if((index.equals(ARRAY_NAME2))
System.out.print(row1[1]);
We got that out of the way.
Now, you say that you don't want to hardcode this and I absolutely understand.
So, if I were in your position, I would create a new class for this. It's really simple and easy to do, even for a total beginner.
//class can be named anything. (As long as it is meaningful)
public class IntegerArrayIdentity{
/*This class will have two variables.
A name, for comparison operations and
the integer array that it will represent.*/
public String name;
public int[] array;
/*Simple constructor. Takes the name and array as its parameters.
You can change it to only take a name as a parameter, in case
that you want to initialise the array later. Add new methods as you like*/
public IntegerArrayIdentity(String name, int[] array){
this.name = name;
this.array = array;
}
}
Then in your code, you can do:
int[] row0 = {8, 3, 5, 143, 27, 0};
IntegerArrayIdentity row0id = new IntegerArrayIdentity("row0", row0);
if(index.equals(row0id.name))
System.out.println(row0id.array[1]);
output -> '3'
All the above is just an example. You can implement it however you like.
Hope it helped.