Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaring an unitialized 2D array.

 
Hung Tang
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the proper syntax in declaring an unitialized 2D array. for example.
The following is fine:
Object [][] array = { {"test1", "test2", "test3"} };
}
but when I type in the following code:
public class Test {
private Object [][] array;

public Test() {
array = { {"test1", "test2", "test3"} }; //error
}
}
I get an error... why and how do I fix it?
 
Hung Tang
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I know what was the problem. The problem was that I didn't allocate memory for objects using new. So the fix:
array = new Object[][] { {"test1", "test2", "test3"}
};
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic