O
ohaya
Hi,
I'm trying to create/use an array of classes, and I found the following
code snippet using Google Groups:
...
rectangle[ ] array_name = new rectangle[ 100 ]; // or whatever
size
for ( int n = 0; n < 100; ++n )
{
array_name[ n ] = new rectangle( );
}
...
int i = 99;
int wide = array_name.getwidth( );
...
The above snippet was from:
http://groups.google.com/group/comp.lang.java.programmer/msg/1ea4bbde51104914
What I'm trying to understand in the above snippet is the line where the
array is initialized:
rectangle[ ] array_name = new rectangle[ 100 ]; // or whatever size
Before I found that post, I was trying something like:
rectangle [] arrayName; // declear an array of 'rectangle classes'
..
..
rectangle[0] = new rectangle(); // instantiate rectangle/populate array
rectangle[1] = new rectangle(); // instantiate rectangle/populate array
I did it that way because I don't know the number of classes that I'll
want to have in the array of classes ahead of time.
The code per the snippet from the cited posts works, but my original
code would get a NullPointerException, and I don't understand why.
Can someone explain this?
Also, is it possible to do this in a way that I don't have to allocate a
fixed size array?
Thanks,
Jim
I'm trying to create/use an array of classes, and I found the following
code snippet using Google Groups:
...
rectangle[ ] array_name = new rectangle[ 100 ]; // or whatever
size
for ( int n = 0; n < 100; ++n )
{
array_name[ n ] = new rectangle( );
}
...
int i = 99;
int wide = array_name.getwidth( );
...
The above snippet was from:
http://groups.google.com/group/comp.lang.java.programmer/msg/1ea4bbde51104914
What I'm trying to understand in the above snippet is the line where the
array is initialized:
rectangle[ ] array_name = new rectangle[ 100 ]; // or whatever size
Before I found that post, I was trying something like:
rectangle [] arrayName; // declear an array of 'rectangle classes'
..
..
rectangle[0] = new rectangle(); // instantiate rectangle/populate array
rectangle[1] = new rectangle(); // instantiate rectangle/populate array
I did it that way because I don't know the number of classes that I'll
want to have in the array of classes ahead of time.
The code per the snippet from the cited posts works, but my original
code would get a NullPointerException, and I don't understand why.
Can someone explain this?
Also, is it possible to do this in a way that I don't have to allocate a
fixed size array?
Thanks,
Jim