Im having a problem in a program I’m making and I can’t figure out what the problem is. I have made a couple of smaller test classes to try and figure out what the problem is but I dont get it. I’m sure it’s some basic thing about how Arrays works or something but I can’t seem to remember what. So I post the classes here and hope you guys know whats wrong. Thanks!
public class Main {
public static void main(String[] args) {
TestArray t = new TestArray(8);
t.set(1, 15);
t.print();
}
}
public class TestArray {
private Word[] a;
public TestArray(int i){
a = new Word[i];
}
public void set(int pos, long value){
a[pos].set(value);
}
public void print(){
for(Word w : a){
System.out.println(w);
}
}
}
public class Word {
private long value;
public Word(long value){
this.value = value;
}
public void set(long value){
this.value = value;
}
public String toString(){
return String.valueOf(value);
}
}
It’s when I try to do t.set(1,15) the error occours and Eclipse says something is wrong with the line: a[pos].set(value);
You aren’t initializing the actual elements of the
Word[] avariable. Initialize them if they’re null in yourTestArray.setmethod.The values of an object array in Java are initialized to
null, which is contrary to how a primitive array gets initialized (to all zeroes or equivalent thereof). So when you create the array vianew Word[i], you’re actually creating an array ofnullelements, and you have to set them all accordingly.Doing it in the set method ensures that you aren’t creating any unused
Wordobjects. This is called lazy initialization. The other way to do it would be to initialize them all to some default value in the constructor:Per your comment:
Your code:
Doesn’t work because
wisn’t the actual reference likea[index]is. In a for-each loop on an array, your code actually does this when compiled:As you can see, you’re assigning a value to the local variable,
w, not to the actual element ina, so the array isn’t being changed. One remark: don’t get thrown off by the$variables because 1)$is legal in Java variable names (though you should never use them explicitly) and 2) Java generates these variables when it compiles your code (it can be seen in the proper debuggers).