This week's book giveaway is in the Agile forum.
We're giving away four copies of Fixing your Scrum and have Ryan Ripley & Todd Miller on-line!
See this thread for details.
Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

arrays

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
somebody please tell me why //line 2 is giving compiler error?
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot have a []before the variable if you are declaring an array using comma as in all the cases above.

Compiler errors for a2, a4, a6 and a8.
So your program can be corrected as below

Also you might be surpirsed to know that
a1 is double dimention array of primitive ints
a2 is double dimention array of primitive ints
a3 is single dimention array of primitive ints
a4 is double dimention array of primitive ints
a5 is single dimention array of primitive ints
a6 is double dimention array of primitive ints
a7 is single dimention array of primitive ints
a8 is single dimention array of primitive ints
Thanks
Deepak
 
ushma agarwal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks deepak
now i got the point.
 
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:

[/CODE]
Also you might be surpirsed to know that
a1 is double dimention array of primitive ints
a2 is double dimention array of primitive ints
a3 is single dimention array of primitive ints
a4 is double dimention array of primitive ints
a5 is single dimention array of primitive ints
a6 is double dimention array of primitive ints
a7 is single dimention array of primitive ints
a8 is single dimention array of primitive ints
Thanks
Deepak



How so? How is a2,a4,a6 double dimension array of ints?
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] a1[],a2[]; // 1
int []a3,a4[]; // 2
int []a5,a6[]; // 3
int[] a7,a8; // 4
Here a2, a4 and a6 all have thier own dimention becuase they are declared as a2[],a4[] and a6[]. Now since they are declared with comma operator and because [] is placed after the datatype , Hence [] gets applied to a2, a4 and a6 and therefore makes them a 2-dimentional array.
If you have
int a1[], a2[]; then here a2 is a single dimentional array.
Hope this clears.
But if you have
int[] a1, a2[] then a1 is 1-D and a2 is 2-D.
Thanks
Deepak
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To avoid confusion, you should never declare arrays with the square brackets after the variable name. This syntax was basically just a concession to C/C++ programmers; in Java, it's best to think of the square brackets as part of the type name, and always write them as such (i.e. immediately following the element type name, without any spaces).
 
Arvind Mahendra
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:
int[] a1[],a2[]; // 1
int []a3,a4[]; // 2
int []a5,a6[]; // 3
int[] a7,a8; // 4
Here a2, a4 and a6 all have thier own dimention becuase they are declared as a2[],a4[] and a6[]. Now since they are declared with comma operator and because [] is placed after the datatype , Hence [] gets applied to a2, a4 and a6 and therefore makes them a 2-dimentional array.
If you have
int a1[], a2[]; then here a2 is a single dimentional array.
Hope this clears.
But if you have
int[] a1, a2[] then a1 is 1-D and a2 is 2-D.
Thanks
Deepak



Hmm. This is where the confusion is. I can understand a2[] being a 2-D array. but not a4 and a6.
In the case of a2 we have:
int[] a1[],a2[]; // so with this i can see that a2 is a 2-d array

but with
int []a3,a4[]; // shouldn't this mean that a3 is a 1-d array and so is a4? as each array has its own []. a3 has it before and a4 has its after.

If I had declared the array as []a3,[]a4 //then a3,a4 would both be 1-d array yes?
but if I declare as []a3,a4[] then a4 becomes a 2-d aray???
[ November 30, 2007: Message edited by: Chunnard Singh II ]
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chunnard,

Don't be fooled by the spacing. These two statements are exactly the same:

and
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic