• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Array Creation

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from Kathy-Bert book (ch.1)selfTest.


>java CommandArgsThree 1 2 3
The array argCopy is constructed as new String[2][2].
when args is assigned to argCopy[0], should it not give an error?
Because:
The value of args is 3 (from command line) and the array is just initialized as 2X2 array. When you are assiging argCopy[0]=3, dont you have to reassign memory to the new element?
argsNew= new String [args]
argCopy[0]= argsNew // would this work?
Dont you have to use new is you are changing(allocating) space to array in the program?
Please help me understand this.
Thank you
[Dan added the UBB code to format the code block.]
[ February 24, 2003: Message edited by: Dan Chisholm ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saith,
argCopy [2] [2] array is two-dimensional array; it has two cells (elements) argCopy[0] and argCopy[1] and each element contains a one-dimensional array.
So this line is legal :
argCopy[0]=args;//5
Because both sides are one-dimensional.
However

argCopy[0]=args[0] // is illegal because argCopy[0] contains an object (one-dimensional array args) but args[0] contains primative value (1).

This example will help to understand that:
Given:
double daaa[][][] = new double[3][][];
double d = 100.0;
double daa = new double[1][1];
Which of the following is valid?
a) daaa[0] = d;
daaa[0] can contain a two-dimensional array not double, so it is invalid.
b ) daaa[0] = daa;
it is correct because both side is two-dimensional array
c) daaa[0] = daa[0];
daaa[0] can contain a two-dimensional array referance but daa[0] is a one-dimensional array. so it is invalid.
d) daa[1][1] = d;
it is correct because both side is double.
e) daa = daaa[0];
it is also correct because both side is two-dimensional array
I hope that I gave a correct answer.
[ February 23, 2003: Message edited by: Ahmad El-Tawbah ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saith,
Ahmad's answer is good. Another way to say this is that argCopy[0] is a reference variable that must refer to a one-dimensional String array. It can be re-assigned to refer to any one-dimensional String array,... of any length.
As Ahmad indicates, it CAN"T be re-referenced to a String or to any non-one dimensional array (say a two or three dimensional array).
make sense?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saith,
>When you are assiging argCopy[0]=3, dont you have to reassign memory to the new element?
No need to do that. In fact you may declare the array like this:
String [][] argCopy= new String[2][];
then the array can have a different number of columns for each row. The actual number of columns for a row depends on how it is initialized. So, the following:
argCopy[0]=args;
makes row 0 of argCopy[] to have 3 columns, and if you add the following statments to the class:
String[] row1values = {"1", "2", "3", "4"};
argCopy[1] = row1values;
then row 1 will have 4 columns. The columns are automatically allocated for you. Isn't it nice?
vp

 
Saith Govind
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bert, Ahmad and Victor.
yes victor, it is nice that it allocates memory by itself when we increase the size of an array and we need not do any explicit memory allocations.
Thank you
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saith -
It is important to be VERY careful of how you think about these things - if your fundamentals are solid the exam will be much easier...

Thanks Bert, Ahmad and Victor.
yes victor, it is nice that it allocates memory by itself when we increase the size of an array and we need not do any explicit memory allocations.


We don't increase the size of an array
 
Saith Govind
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert,
Thanks for the clarification.
String[][] twoDarray= new String[2][];
String [] oneDarray = new String[2];
String[] duparray= new String[5];
twoDarray[0]= oneDarray; //4
twoDarray[0]=duparray; //5
at line 4 twoDarray[0] holds reference to oneDarray which is of size 2
after line 5
now twoDarray[0] holds reference to duparray object which is of size 5.
twoDarray[0] now holds reference to a object of bigger size.
Is this correct?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic