natarajan raman

Greenhorn

Posts: 26

posted 12 years ago

Hi all,

I have a doubt with assigning values to the array variables.

In K&B self test page 49 question 12.

It asks for the line of code to be inserted and still allow the code to compile

____________________________________________________________________

class{

main here{

//code

byte [] [] big = new byte [7][7];

byte [][] b= new byte[2][1];

byte b3=5;

byte b2[][][][]=new byte [2][3][1][2];

//here we need to insert any of the line of code below

}

}

_______________________________________________________________________

they ask for the code that can be inserted

a) b2[0][2]=b; b) b[0][0]=b3; c) b2[1][1][0]=b[0][0]; d) b2[1][2][0]=b;

d) b2[0][1][0][0] = b[0][0]; f) b2[0][1] = big;

The answers are a,b,e and f. The explanation states that "If an array is declared as a two dimensional array, you can't assign a one dimensional array to a one-dimesional array reference"

What's that? If i declare an array as two dimesional what does the one dimensional array got to do with that?

When choice a assigns a 2-D array to 4-d it works fine...then why not option c which assigns 2-D to 3-D.?

I am really confused with array assignment. I tried to search earlier posts too. please help me out. I am losing a lot because of not understanding this concept in other areas too.

Can any of you help me please....Else please atleast specify links to earlier posts for reference.

Thanks a lot in advance.

[ August 08, 2004: Message edited by: natarajan raman ]

I have a doubt with assigning values to the array variables.

In K&B self test page 49 question 12.

It asks for the line of code to be inserted and still allow the code to compile

____________________________________________________________________

class{

main here{

//code

byte [] [] big = new byte [7][7];

byte [][] b= new byte[2][1];

byte b3=5;

byte b2[][][][]=new byte [2][3][1][2];

//here we need to insert any of the line of code below

}

}

_______________________________________________________________________

they ask for the code that can be inserted

a) b2[0][2]=b; b) b[0][0]=b3; c) b2[1][1][0]=b[0][0]; d) b2[1][2][0]=b;

d) b2[0][1][0][0] = b[0][0]; f) b2[0][1] = big;

The answers are a,b,e and f. The explanation states that "If an array is declared as a two dimensional array, you can't assign a one dimensional array to a one-dimesional array reference"

What's that? If i declare an array as two dimesional what does the one dimensional array got to do with that?

When choice a assigns a 2-D array to 4-d it works fine...then why not option c which assigns 2-D to 3-D.?

I am really confused with array assignment. I tried to search earlier posts too. please help me out. I am losing a lot because of not understanding this concept in other areas too.

Can any of you help me please....Else please atleast specify links to earlier posts for reference.

Thanks a lot in advance.

[ August 08, 2004: Message edited by: natarajan raman ]

nata.r<br />**********************************<br />To win is not always success<br />And to lose is not always failure.<br />**********************************

Tybon Wu

Ranch Hand

Posts: 84

posted 12 years ago

You can only assign arrays of the same dimension to another array. In choice a), b2[0][2] is a 2-dimensional array, and b is also a 2-dimensional array, so b2[0][2]=b is legal. In choice c), b2[1][1][0] is a 1-dimensional array, but b[0][0] is a byte, so b2[1][1][0]=b[0][0] is not legal.

Given byte b2[][][][] = new byte [2][3][1][2];

the following are true:

b2 is a 4-dimensional array

b2[0] is a 3-dimensional array

b2[0][0] is a 2-dimensional array

b2[0][0][0] is a 1-dimensional array

b2[0][0][0][0] is a byte

Hopefully that will make it clear for you...

Given byte b2[][][][] = new byte [2][3][1][2];

the following are true:

b2 is a 4-dimensional array

b2[0] is a 3-dimensional array

b2[0][0] is a 2-dimensional array

b2[0][0][0] is a 1-dimensional array

b2[0][0][0][0] is a byte

Hopefully that will make it clear for you...

SCJP2

natarajan raman

Greenhorn

Posts: 26

posted 12 years ago
nata.r<br />**********************************<br />To win is not always success<br />And to lose is not always failure.<br />**********************************

Dear Tybon,

Quote

_______________________________________________

Given byte b2[][][][] = new byte [2][3][1][2];

the following are true:

b2 is a 4-dimensional array

b2[0] is a 3-dimensional array//isn't that a 1-dimensional array?

b2[0][0] is a 2-dimensional array

b2[0][0][0] is a 1-dimensional array//isn't this a 3-D array?

b2[0][0][0][0] is a byte

_________________________________________________________

Please help.....will be grateful if you do....

Thanks.

Quote

_______________________________________________

Given byte b2[][][][] = new byte [2][3][1][2];

the following are true:

b2 is a 4-dimensional array

b2[0] is a 3-dimensional array//isn't that a 1-dimensional array?

b2[0][0] is a 2-dimensional array

b2[0][0][0] is a 1-dimensional array//isn't this a 3-D array?

b2[0][0][0][0] is a byte

_________________________________________________________

Please help.....will be grateful if you do....

Thanks.

Tybon Wu

Ranch Hand

Posts: 84

posted 12 years ago

Do you agree with the following?

A n dimensional array is composed of series of n-1 dimensional arrays.

A 4 dimensional array is composed of series of 3 dimensional arrays.

A 3 dimensional array is composed of series of 2 dimensional arrays.

...

...

...

If you agree with the above, then you should agree with the following, which is another way of saying the above:

An element of a n dimensional array is a n-1 dimensional array.

An element of a 4 dimensional array is a 3 dimensional array.

An element of a 3 dimensional array is a 2 dimensional array.

...

...

...

OK if you are still in agreement, we can go back to the previous example:

byte b2[][][][] = new byte [2][3][1][2];

b2 is a 4 dimensional array

We just agreed from above that "An element of a 4 dimensional array is a 3 dimensional array"

And since b2[0] is an element of b2

Therefore b2[0] must be 3 dimensional

We just agreed from above that "An element of a 3 dimensional array is a 2 dimensional array"

And since b2[0][0] is an element of b2[0]

Therefore b2[0][0] must be 2 dimensional

...

...

...

A n dimensional array is composed of series of n-1 dimensional arrays.

A 4 dimensional array is composed of series of 3 dimensional arrays.

A 3 dimensional array is composed of series of 2 dimensional arrays.

...

...

...

If you agree with the above, then you should agree with the following, which is another way of saying the above:

An element of a n dimensional array is a n-1 dimensional array.

An element of a 4 dimensional array is a 3 dimensional array.

An element of a 3 dimensional array is a 2 dimensional array.

...

...

...

OK if you are still in agreement, we can go back to the previous example:

byte b2[][][][] = new byte [2][3][1][2];

b2 is a 4 dimensional array

We just agreed from above that "An element of a 4 dimensional array is a 3 dimensional array"

And since b2[0] is an element of b2

Therefore b2[0] must be 3 dimensional

We just agreed from above that "An element of a 3 dimensional array is a 2 dimensional array"

And since b2[0][0] is an element of b2[0]

Therefore b2[0][0] must be 2 dimensional

...

...

...

SCJP2

kapil munjal

Ranch Hand

Posts: 298

posted 12 years ago

Hi Tybon,

you are very right in saying this

but I think we need to know a complete explanation of this concept.

If you know any link which tells teh assignment of values to multi-dimensional arrays then please provide us.

Thanks

Kaps

you are very right in saying this

Given byte b2[][][][] = new byte [2][3][1][2];

the following are true:

b2 is a 4-dimensional array

b2[0] is a 3-dimensional array//isn't that a 1-dimensional array?

b2[0][0] is a 2-dimensional array

b2[0][0][0] is a 1-dimensional array//isn't this a 3-D array?

b2[0][0][0][0] is a byte

but I think we need to know a complete explanation of this concept.

If you know any link which tells teh assignment of values to multi-dimensional arrays then please provide us.

Thanks

Kaps

Kapil Munjal

SCJP 1.4, SCWCD 1.4

Julian Kennedy

Ranch Hand

Posts: 823

posted 12 years ago

Tony Morris provides a good explanation of how arrays are implemented in Java. The thrust of the article is that Java does

Consider the following method of initializing an array of arrays:

This does not produce the same set of data as the following, more conventional, method:

Both arrays have 3 rows but whilst each row in the first example has a different number of columns, each row in the second example has an equal number of columns.

This may help to clear up the source of your confusion.

The answer to your question is, as I guess you suspect, "nothing". There appears to be a typo in the explanation. It means to say that you can't assign a

Let me know if that clears things up for you.

Jules

**NOT**implement multi-dimensional arrays; it only provides support for arrays of arrays. What he doesn't explain clearly is what, for example, a two-dimensional array is as opposed to an array of arrays. The crux of this is that in a 2D array each row has a fixed number of columns (no more, no less). With an array of arrays the number of columns can be different for each row.Consider the following method of initializing an array of arrays:

This does not produce the same set of data as the following, more conventional, method:

Both arrays have 3 rows but whilst each row in the first example has a different number of columns, each row in the second example has an equal number of columns.

This may help to clear up the source of your confusion.

Originally posted by Nata Raman

a) b2[0][2]=b; b) b[0][0]=b3; c) b2[1][1][0]=b[0][0]; d) b2[1][2][0]=b;

d) b2[0][1][0][0] = b[0][0]; f) b2[0][1] = big;

The answers are a,b,e and f. The explanation states that "If an array is declared as a two dimensional array, you can't assign a one dimensional array to a one-dimesional array reference"

What's that? If i declare an array as two dimesional what does the one dimensional array got to do with that?

The answer to your question is, as I guess you suspect, "nothing". There appears to be a typo in the explanation. It means to say that you can't assign a

**two**dimensional array (array of arrays) to a one dimensional array reference. Think about it this way: the only thing that you can assign to a Thing reference is an instance of Thing. So, answer d) (the first one in your posting ) will fail to compile as b2[1][2][0] is a one dimensional array reference and b is a two dimensional array. Similarly, answer c) will fail to compile as b2[1][1][0] is a one dimensional array reference and b[0][0] is a primitive byte.Let me know if that clears things up for you.

Jules

Murtuza Akhtari

Ranch Hand

Posts: 108

posted 12 years ago

I think Tybon is right..

Natarajan...the best way I made myself understand this concept was..

b2 is a 4 dimensional array...so it will need 4 indices..

b and big are 2-dimensional arrays..so they need 2 indices..

b3 of course is just a byte.

now look at the answer choices.

a) b2[0][2]=b; ..2 indices given for b2 already + 2 of b i.e. putting a 2-dimensional array within a 4-dimensional array..definitely possible !!

b) b[0][0]=b3;..2 indices given for b(doesnt need anymore) b3 is an element which can be stored at tht location

c) b2[1][1][0]=b[0][0]; 3 indices given..needs 1 more..b[0][0] is element...therefore INCORRECT

d) b2[1][2][0]=b;..3 indices given..needs 1 more..b has 2..so makes 5..therefore INCORRECT

e) b2[0][1][0][0] = b[0][0]; b2 has 4 indices..so it is representing the element at tht location....same with b[0][0]...two elements can be equated.

f) b2[0][1] = big;..again...b2 has 2 indices..needs 2 more...big has 2

Hope that helps more than it confuses!!!

Sorry if i m making this more difficult for u !!

[ August 08, 2004: Message edited by: INXS ]

Natarajan...the best way I made myself understand this concept was..

b2 is a 4 dimensional array...so it will need 4 indices..

b and big are 2-dimensional arrays..so they need 2 indices..

b3 of course is just a byte.

now look at the answer choices.

a) b2[0][2]=b; ..2 indices given for b2 already + 2 of b i.e. putting a 2-dimensional array within a 4-dimensional array..definitely possible !!

b) b[0][0]=b3;..2 indices given for b(doesnt need anymore) b3 is an element which can be stored at tht location

c) b2[1][1][0]=b[0][0]; 3 indices given..needs 1 more..b[0][0] is element...therefore INCORRECT

d) b2[1][2][0]=b;..3 indices given..needs 1 more..b has 2..so makes 5..therefore INCORRECT

e) b2[0][1][0][0] = b[0][0]; b2 has 4 indices..so it is representing the element at tht location....same with b[0][0]...two elements can be equated.

f) b2[0][1] = big;..again...b2 has 2 indices..needs 2 more...big has 2

Hope that helps more than it confuses!!!

Sorry if i m making this more difficult for u !!

[ August 08, 2004: Message edited by: INXS ]

---<br />SCJP 1.4

natarajan raman

Greenhorn

Posts: 26

posted 12 years ago
nata.r<br />**********************************<br />To win is not always success<br />And to lose is not always failure.<br />**********************************

That's really a great explanation from all you people.

Tybon has given me a clear idea how the array is looked at.

Julian Thanx a lot ......Your explanation was also good.

INXS( I just wonder what your real name is!!!) I just can't believe

anyone can explain this as lucid as you did. Great!. you never confused me.

It's just because of you I could grasp what other two explained easily.

Thank you all.

It's just this great response which you people show makes many like me to

prepare well.

Thank you all again.

Tybon has given me a clear idea how the array is looked at.

Julian Thanx a lot ......Your explanation was also good.

INXS( I just wonder what your real name is!!!) I just can't believe

anyone can explain this as lucid as you did. Great!. you never confused me.

It's just because of you I could grasp what other two explained easily.

Thank you all.

It's just this great response which you people show makes many like me to

prepare well.

Thank you all again.

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |