2

So I want draw a field (of array strings) with obstacles and free spaces. "+" are the obstacles and free spaces are just "". And if we have a free space, I want it replaced by "*". I will choose one specific element of that 2d array that equals "" and that will be replaced by "1".

My question isn't as long as it seems, you could ignore the 2 codes below. Here is summary, if unclear, please read to the end:

Why aren't these things equal to each other?

if(field[i][j]==field[5][1]){
  System.out.print("1");
}

and

if(i==5 && j==1){
   System.out.print("1");
}

First code doesn't work. That specific array element here will be field[5][1] which I want replace by 1:

public class Test{
    public static void main(String[] args){

        String[][] field = {{"+" , "+" , "+" , "+" ,"+" , "+" , "+"},
                            {"+" , "+" , "+" , "+" ,"+" , "" , "+"},
                            {"+" , "+" , "+" , "+" ,""  , ""  , "+"},
                            {"+" , "+" , "+" , ""  ,""  , "+" , "+"},
                            {"+" , ""  , ""  , ""  ,"+" , "+" , "+"},
                            {"+" , ""  , "+" , "+" ,"+" , "+" , "+"},
                            {"+" , "+" , "+" , "+" ,"+" , "+" , "+"}};
        int x = field.length;

        for(int i=0; i<x; i++){
            for(int j=0; j<field[i].length; j++){
                System.out.print(field[i][j]);

                if(field[i][j] != "+"){
                    if(field[i][j]==field[5][1]){
                        System.out.print("1");
                    }
                    else{
                        System.out.print("*");
                    }
                }
            }
            System.out.println("");
        }
    }
}

Output:
+++++++
+++++1+
++++11+
+++11++
+111+++
+1+++++
+++++++

Second code works as desired:

public class Test{
    public static void main(String[] args){

        String[][] field = {{"+" , "+" , "+" , "+" ,"+" , "+" , "+"},
                            {"+" , "+" , "+" , "+" ,"+" , "" , "+"},
                            {"+" , "+" , "+" , "+" ,""  , ""  , "+"},
                            {"+" , "+" , "+" , ""  ,""  , "+" , "+"},
                            {"+" , ""  , ""  , ""  ,"+" , "+" , "+"},
                            {"+" , ""  , "+" , "+" ,"+" , "+" , "+"},
                            {"+" , "+" , "+" , "+" ,"+" , "+" , "+"}};
        int x = field.length;

        for(int i=0; i<x; i++){
            for(int j=0; j<field[i].length; j++){
                System.out.print(field[i][j]);

                if(field[i][j] != "+"){
                    if(i==5 && j==1){
                        System.out.print("1");
                    }
                    else{
                        System.out.print("*");
                    }
                }
            }
            System.out.println("");
        }
    }
}

Output: 
+++++++
+++++*+
++++**+
+++**++
+***+++
+1+++++
+++++++
Andy Turner
  • 137,514
  • 11
  • 162
  • 243

4 Answers4

2
if(field[i][j]==field[5][1]){
  System.out.print("1");
}

This this compares value of field[5][1] which is equals to "" and every field that has value "" prints number "1". This code doesn't check the field position but compares values.

Emil Hotkowski
  • 2,233
  • 1
  • 13
  • 19
2

The two are obviously different:

if(field[i][j]==field[5][1]){

Is true if field[i][j] is "" - and there are lots of array elements equal to that.

if(i==5 && j==1){

is true if i == 5 and j == 1 - which is only true at 1 position.


It should be noted that the first approach is not a reliable way to compare strings: it is only because of the way that you construct field that the code currently behaves as it does. In general, you should use field[i][j].equals(field[5][1]). See How do I compare strings in Java.

Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1
if (field[i][j] == field[5][1]) {
  System.out.print("1");
}

checks whether the current field's value is equal to the value of field[5][1] (that's "" in your case). As you can see, the first output marks every "" from field with a 1.

if (i==5 && j==1) {
   System.out.print("1");
}

checks whether the current position is 5,1. Values of field are not considered.

Socowi
  • 25,550
  • 3
  • 32
  • 54
0

if(i==5 && j==1) is true for only one case, if(field[i][j]==field[5][1]) is true for all the case with the same value than field[5][1]. ("")

If your question is "why == work between two different string?"

The cause is an optimisation of the compilator, who will create a constante for each identical string , so when == compare the reference of two string with different (i,j), it's in fact the same String Object, so it's true.

sab
  • 4,352
  • 7
  • 36
  • 60