help

W

wee

i'm confused with the way java initializes object arrays:
if i declare array objects as such;

JTextField[] arr_field;
arr_field[0] = new JTextField(10);
arr_field[1] = new JTextField(10);

the compiler reports it as an error, but if i declare it as such;

JTextField[] arr_field = {new JTextField(10), new JTextField(10);

then the complier compiles it. shouldn't they be the same? and also,
why can't i initialize array objects as;

JTextField arr_field[3];
for (int i = 0; i < 3; i++) {
arr_field = new JTextField(10);
}

thanks.. -wee
 
P

Peter Duniho

i'm confused with the way java initializes object arrays:
if i declare array objects as such;

JTextField[] arr_field;
arr_field[0] = new JTextField(10);
arr_field[1] = new JTextField(10);

the compiler reports it as an error, but if i declare it as such;

JTextField[] arr_field = {new JTextField(10), new JTextField(10);

then the complier compiles it. shouldn't they be the same?

No, they're not the same. In the latter example, you're providing an
initial value for the array and its elements. In the former, the variable
is never initialized, and so referencing any elements in the array is
illegal.
and also,
why can't i initialize array objects as;

JTextField arr_field[3];
for (int i = 0; i < 3; i++) {
arr_field = new JTextField(10);
}


Well, you can't use that syntax. But you could write this:

JTextField[] arr_field = new JTextField[3];

for (int i = 0; i < arr_field.length(); i++)
{
arr_field = new JTextField(10);
}

In other words, you can allocate a new array, assign that instance to the
variable, and then access the array through that variable. (You'll note I
also changed the loop termination...I prefer not hard-coding numbers when
not necessary, as it makes the code more reliable and flexible).

See http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
for more details.

Pete
 
L

Lew

Peter said:
But you could write this:

JTextField[] arr_field = new JTextField[3];

for (int i = 0; i < arr_field.length(); i++)
{
arr_field = new JTextField(10);
}

In other words, you can allocate a new array, assign that instance to
the variable, and then access the array through that variable. (You'll
note I also changed the loop termination...I prefer not hard-coding
numbers when not necessary, as it makes the code more reliable and
flexible).

See
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
for more details.


Peter is right except for one tiny typographical glitch - the 'length'
attribute of an array is not a method, so the termination of the loop will be
with
i < arr_field.length;
without the parentheses.

Array initialization or assignment is a two-step process: first assign the
entire array as a block of "empty" storage locations (there are rules about
the initial value of each element), then assign each element. The first step
does not create individual object instances for each slot, but allocates
pointer storage and determines the overall size of the array - the 'length'
attribute.
 
M

Mark Space

Peter is right but I think maybe could have been simpler. For example,
modify your code like this:
i'm confused with the way java initializes object arrays:
if i declare array objects as such;

JTextField[] arr_field;
arr_field = new JTextField[2]; // Add this line
arr_field[0] = new JTextField(10);
arr_field[1] = new JTextField(10);

A JTextField[] is just one value, the reference. It's just like a 4
byte pointer. It can hold one address, but that's it. You still have
to allocate some space for the array itself.

/* C example */
int *arr_field;
arr_field = malloc( sizeof (int), 2 ); // ? didn't check syntax

Now you can use it arr_field. Before the malloc (= "new") there wasn't
any memory to put your new JTextField(10) into.

I got bit by this when I was first starting out. It seems like in an
object oriented language it should be more automagic, but it's not. You
still have to allocate the array.

JTextField arr_field[3];

I don't know why this doesn't work. The language designers just decided
not to include it. Use the syntax above with the explicit array allocation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top