aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes conversion from int[] to long[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "conversion from int[] to long[]" Watch "conversion from int[] to long[]" New topic
Author

conversion from int[] to long[]

anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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).]


SCJP
Visit my download page
anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
cant we use casting ???
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Nov 04, 2000
Posts: 81
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

Joined: Jan 21, 2001
Posts: 16
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conversion from int[] to long[]