1

I have one matrix which has two rows and one column. Each row has specific name such as row0 and row1. I added some numbers in each row and column of this matrix. For example I have String index which can get one of the name of rows (row0 or row1). How can I check if index == "row0" then print row0[1] and if index == "row1" then print row1[1]??

int[][] s = new int[2][3];
s[0][0] = 5;
s[0][1] = 10;
s[0][2] = 15;
s[0][1] = 25;
s[1][1] = 30;
s[2][1] = 45;
int[] row0 = new int[]{s[0][0], s[0][1], s[0][2]};
int[] row1 = new int[]{s[0][1], s[1][1], s[2][1]};
String index = "row0";
// if index= row0
System.out.println(row0[1]);
// if index=row1
System.out.println(row1[1]);
deHaar
  • 17,687
  • 10
  • 38
  • 51
Ashkan
  • 49
  • 6
  • What are you trying to do? Shouldn't you try to compare to the value in row0[1]? Like this: int index = 10; if (row0[1] == index) { //do somthing }; – fkajzer Jan 11 '19 at 12:25
  • I mentioned clearly . – Ashkan Jan 11 '19 at 12:26
  • If you compare two values you need to use '==' instead of '=', maybe this is your error? – fkajzer Jan 11 '19 at 12:26
  • I have String " index" and different array "row0 and row1" so I want to check if string was equal to row0 then print one of its value or if index was equal to row1 then print on of the value of row1 – Ashkan Jan 11 '19 at 12:28
  • I think you did not get my question – Ashkan Jan 11 '19 at 12:29
  • 1
    You could do something like this: `if (index.equals("row0") { System.out.println(row0[1]) }`. You are comparing `String`s, which are objects and not primitives, so use the `equals` method. – deHaar Jan 11 '19 at 12:31
  • It is good idea but I want to make a code which can find it automatically – Ashkan Jan 11 '19 at 12:34
  • Is it possible ? – Ashkan Jan 11 '19 at 12:34
  • If you explain clearly what you are trying to do, then someone might be able to help you out – Soutzikevich Jan 11 '19 at 12:37
  • 1
    Possible duplicate of [Java Reflection: How to get the name of a variable?](https://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable) – fkajzer Jan 11 '19 at 12:39
  • now I have two int[] (row0 and row1) , so imagine that I made a code which can assign one of these name randomly as string item in index, so I want o check automatically if the name of String index is equal to name of first in[] row0 then print on of the its number which I already added and vise versa – Ashkan Jan 11 '19 at 12:39
  • @Ashkan Please consider up-voting and selecting my answer as the accepted solution, if it solves your problem. – Soutzikevich Jan 11 '19 at 13:01

2 Answers2

3

In your example there is probably something wrong:

int[][] s = new int[2][3]; // two rows, three cols
s[0][0] = 5;
s[0][1] = 10;
s[0][2] = 15;
s[0][1] = 25;
s[1][1] = 30;
s[2][1] = 45; // <---- 2 is out of bounds
int[] row0 = new int[]{s[0][0], s[0][1], s[0][2]};
int[] row1 = new int[]{s[0][1], s[1][1], s[2][1]};

Anyway, you could use a Map for your matrix and then address the rows with a String:

// ...code for s here..

Map<String, Integer[]> matrix = new HashMap<String, Integer[]>();
matrix.put("row0", new Integer[] {s[0][0], s[0][1], s[0][2]});
matrix.put("row1", new Integer[] {s[0][1], s[1][1], s[2][1]});
String index = "row0";
// if index= row0
System.out.println(matrix.get(index)[1]); // row0[1]
// if index=row1
index = "row1";
System.out.println(matrix.get(index)[1]); // row0[1]
Mario Daglio
  • 108
  • 9
0

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.

Soutzikevich
  • 991
  • 3
  • 13
  • 29