Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conversion from int[] to long[]

 
anil bisht
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i convert a int[] to long[] .. ???
automatic conversion dosent happen..
public class A {
public static void main (String[] args) {
int sn[] = {1};
get(sn);
}
public static void get(long []in){
//do somting}
}
it gives error cant convert int[] to long[]
can it be done by casting ??? how ???
TIA
anil
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is correct you cannot get automatic conversion of arrays even if their type will automatically convert. you have to iterate through the array and convert each element. actually since you are going from int to long you can just assign elements from one array to the other.
int[] array1;
long[] array2;
//the rest
for(i=0,i<array1.length,i++) {>
array2[i]=array1[i];
}
I think that will work

[This message has been edited by Randall Twede (edited January 23, 2001).]
 
anil bisht
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cant we use casting ???
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cant cast from one array type to another
It is not an arbitrary rule. I think it is because array size is fixed when it is created and cant be changed
I should go to bed

[This message has been edited by Randall Twede (edited January 23, 2001).]
 
anil bisht
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Randall
it works fine for the array of objects
this code compiles fine ...
Object[] ov;
String[] sa = { "Green", "Blue", "Red" };
ov = sa; // this is ok ..this works fine
String []sb;
sb = (String [])ov;
 
RoshanDawrani
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I know what I am going to say is slightly unrelated to the original question but I would still like to add this point.
In case of object arrays, if the individual elements can be converted(down the hierarchy), their object arrays can also be assigned to each other. In case of primitive data-types arrays, even if individual elements can be converted to each other(as in a conversion from int[] to long[]), the arrays' reference can not be converted to each other.
In case of object arrays, the following is allowed:
class SUPERCLASS {}
class SUBCLASS extends SUPERCLASS {}
SUPERCLASS[] supArr = new SUPERCLASS[10];
SUBCLASS[] subArr = new SUBCLASS[10];
supArr = subArr; // allowed because individual elements can be converted.
subArr = supArr;// not allowed because inidividual elements can also not be casted like this. they need explicit casting
subArr = (SUBCLASS[])supArr; // allowed because individual elements can also be casted with explicit casting.
Point is that in case of object arrays, rules are the same as for their individual elements but in case of primitive data-type arrays, rules are not the same as they are for individual items.
That's it.
Thanks for reading...smile...
rgds,
Roshan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic