In a for loop I'm trying to change the contents of an array variable for one of my "Tile" classes. I keep getting an illegal start of expression error and I cant for the life of me figure out what's going wrong.
First off, I'll show you my class and the for loop where this is all going wrong.
class Tile {
int value;
int pos;
Tile[] adj;
public Tile(int value, int pos) {
this.value = value;
this.pos = pos;
}
}
and the for loop in question:
public static void update(Tile[] arr) {
for(int i = 0; i < arr.length; i ++) {
Tile t = arr[i];
t.pos = i + 1;
if(i == 0)
t.adj = {arr[i]};
}
}
So for each Tile I have an array called adj, but the compiler is not allowing me to define t.adj (where t is a Tile in the arr[] of Tiles). It's telling me that the first '{' curly bracket is an illegal start to the expression.
What's especially weird is that I can declare a test array right before it that won't run into any errors with the compiler:
if(i == 0) {
Tile[] testTile = {arr[i]};
t.adj = {arr[i]}; //compiler should know that this is an
array of tiles... but it doesn't?
}
The testTile doesn't run into any errors, so I know that it's not a matter of a loose semicolon. I'm really confused as to what the difference between these two statements is. I've already declared that adj is a an array of Tiles(as per the Class Tile). I declare the variable t as each Tile being processed in the method's argument array.
I've tried making the function not static, defining the default size for the adj[] array, but nothing is making the compiler cooperate. I've even tried setting t.adj = 5; but I get this error:
Main.java:105: error: incompatible types: int cannot be converted to
Tile[]
t.adj = 5;
Which proves to me that the compiler recognizes t.adj as a Tile[] type.
What mystical Java rule am I breaking that isn't allowing the compiler to accept what I've written???
Main.java:105: error: illegal start of expression
t.adj = {arr[i]};
^
Main.java:105: error: not a statement
t.adj = {arr[i]};
^
Main.java:105: error: ';' expected
t.adj = {arr[i]};
^
Main.java:112: error: class, interface, or enum expected