开发者

Android: Initialize a two dimensional array of type <class>

I'm sure that this is some basic misunderstanding on my part, but ...

I want to have a class:

public class Foo {
   String bar;
}
开发者_JAVA技巧

And the declare a two dimensional array of them:

Foo[][] foobar = new Foo[3][3];

Which works fine, but when I get to that point and look at foobar, each element is null. Now I can initialize each element at this point, but this just seems wrong. I don't know if I need to have some special constructor in my class or what.

I'm new to Android and Java, so any help would be appreciated.

Dan


you do have to initialize each element. what you're doing is creating an empty 2 dimensional array of 9 elements. what you can do, however is to have a for loop nested within another for loop that initializes each element to a new Object() (or whatever class you need). in that case nothing will be null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜