1

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

I want to have a class:

public class Foo {
   String bar;
}

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.