• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Array declaration and initialization

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any difference with these two statements?
int a[][] = new int[][]{{1,2,3} , {1,2,3}};
Or
int a[][] = {{1,2,3} , {1,2,3}};

regards
vivek
[This message has been edited by Vivek Shrivastava (edited June 06, 2000).]
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no they both work fine
here is another alternative
int[]a[]={1,2,3},{1,2,3};
[This message has been edited by Fred Abbot (edited June 06, 2000).]
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred,
I suppose you forgot one pair of "{}". Please, correct me if it is wrong now.
int[]a[]={{1,2,3},{1,2,3}};
Adrian
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred,
i tried to compile with your syntex , it didn't work out. i think u are missing something.
int[]a[]={1,2,3},{1,2,3}; //doesn't work
regard
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
adrian is right you need another set of curly bracaes around the whole statement
i was nt tring to show you that part as you already knew that my point is that these brackets[]can be placed on either side
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh! got your point.
thanks.
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek , I reproduce your question below:
-----------------------------------------------------------------Is there any difference with these two statements?
int a[][] = new int[][]{{1,2,3} , {1,2,3}};
Or
int a[][] = {{1,2,3} , {1,2,3}};
---------------------------------------------------------------
I say YES, there is a difference.FASTEN YOUR SEAT BELT!!!
A.int a[][] = new int[][]{{1,2,3},{1,2,3}}; is called an array creation statement.
B.int a[][] ={{1,2,3},{1,2,3}}; is an array Initialiser.
The difference is that you can use A,(the array creation statement) anywhere in your code where you want to create a pre-declared array.But you can use B(the array initialiser) only at the same point you are declaring the array, otherwise it will be illegal and will not compile.Do you get it ? Oky, This is what I mean.
You can declare an array as int [][]a; and defer its creation to a later time. Then, at that opportune time you can legally write:
a = new int[][]{{1,2,3},{1,2,3}};//LEGAL, and it will work fine. However if you declared your array as above, without initializing, you CAN NOT later on say:
a ={{1,2,3},{1,2,3}};//ILLEGAL.You would have no choice now but to say a = new int[2][3]; or new int[2][] and initialise the elements one by one.
Do I make any Sense ?
Herbert.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Herbert you really makes sense.
Thanks for a nice explanation.
vivek
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey herbert,
i just tried to compile following program.
public class Test
{
int c[][];
int d[][];
int a[][] =new int[][]{{1,2},{3,4}}; //working
int b[][] = {{1,2},{3,4}}; //working
c = new int[][]{{1,2},{3,4}}; //not working
d = {{1,2},{3,4}};
}

as per your point array c should be complied fine but it didn't. can u please explain or am i missing something?
vivek
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
you wrote:
-----------------------------------------------------------
c = new int[][]{{1,2},{3,4}}; //not working
d = {{1,2},{3,4}};
}
------------------------------------------------------------
You are right to say that c should work, and it REALLY should. If your programm is not working, it is probably because of the following line, that is, d = {{1,2},{3,4}}//Remember this CAN NOT WORK. Take out this part here, or comment it and it WILL work.
Regards,
Herbert.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Herbert,
believe me i tried it without second statement( d = {{},{}}); and it is not working.
what i tried
public class Test
{
int c[][];
c = new int[][]{{1,2},{3,4}};
}

feel free to correct me.
vivek
[This message has been edited by Vivek Shrivastava (edited June 06, 2000).]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For the SCJP exam; be very mindful of where you are and
aren't allowed to position the array brackets!!
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
Did you put the above code in main mthd, it should work fine...
thnks
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
I think I see the problem with your code.But it is a different issue altogether, so I will only touch it VERY BRIEFLY, only that the code it should work and that you should appreciate the array creation statement.
When you code:
int [][] c;
c = new int[][]{{1,2},{3,4}};
You will not compile successfully. My reasoning is that in the second line, the compiler can not permit you to do this unless you enclose the second line in a pair of braces, or put it in a method. Thus
int [][] c;
{
c = new int[][]{{1,2},{3,4}};
}
//Should wok.
This has the effect of indicating to the compiler that the second variable c, is refering to the same c you already declared.Otherwise, if you put them in the same name space as you did, the compiler will probably ask you to provide an identifier.
Have Fun.
Herbert
 
kiranv
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
herbert,
I did not get your point..
I dont think the code below works
public class test{
int[][] c;
c = new int[][]{{1,2},{3,4}};
}
I have the below code working:
public class test{
public static void main(String args[]){
int[][] c;
c = new int[][]{{1,2},{3,4}};
}
}
or we could do this:
public class test{
static int[][] c;
public static void main(String args[]){
c = new int[][]{{1,2},{3,4}};
}
}
Did I get it correctly?
thnks
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kiranv
---------------------------------
You wrote
herbert,
I did not get your point..
I dont think the code below works
public class test{
int[][] c;
c = new int[][]{{1,2},{3,4}};
}
Herbert doesn't say that this will work. The initilization
code c = new int [][]{{1,2},{3,4}}; has to be inside a method or
inside a initializer code block.
So
public class test{
int[][] c;
{
c = new int[][]{{1,2},{3,4}};
}
}
Will work


I have the below code working:
public class test{
public static void main(String args[]){
int[][] c;
c = new int[][]{{1,2},{3,4}};
}
}
or we could do this:
public class test{
static int[][] c;
public static void main(String args[]){
c = new int[][]{{1,2},{3,4}};
}
}
Both the above cases had the code inside the main method. So these two should work.
Did I get it correctly?
thnks
--------------------------------
I did some comments to your question. Hope this helps.
Thanks
ARS Kumar.
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic