• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Array Declaration doubts

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here some Array declaration doubts :

1. int x[][] = new int[][];
//err array dimension missing
2. int x[][] = new int[][4];
//invalid array dimension
3. int x[][] = new long[4][];
//invalid incompitable type
4. int x[][] = new int[4][];
//valid
Why 3 is invalid and 4 is valid
Can anybody explain
Thanks
Avi
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vegad,
No. 3 has to be invalid as you are trying to put long value into integer array.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vegad,
3 is invalid because, u r trying to cast a long array into
int array. Arrays of primitive type can't be casted into
other types.
4 is valid because u r declaring an array of 4 rows. and
each row can have same no. of columns or variable columns
later.
here is an exam. for ques. 4==>
int [][]x = new int[4][];
for(int i=0; i<x.length; i++)>
{
x[i] = new int[i+1];
}
now x[0] has 1 elament
x[1] has 2 element
x[2] has 3 elament
x[3] has 4 element
I hope this will help u.
Ashish
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
long has 64 bit and int has 32 bit. Always right side variable's type should be matched to the left side or it should be less than the left side variable's type. Here you are trying to assign a long two dimentional array into integer two dimentional array. It is not, because of incompatible type. And one more thing, Arrays cannot be resized. If you want to change the arrays size first you have to create a new (desired length of) array and pass your existing arrays into it and you can delete the old arrays. I don't know what Ashish trying to say. Please clarify me if I am wrong.
Thanks

[This message has been edited by V Srinivasan (edited February 19, 2001).]
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic