Convert int[][] to double[][]

Hi,is there a way to convert a int[][] to double[][] without running through all the elements?I've tried double[][] matrix = (double[][])wt where wt is a int[][]but doesn't work.thanks in advance,mleiria
[261 byte] By [manuel.leiriaa] at [2007-10-3 5:57:39]
# 1
You will have to loop.
mlka at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 2
> You will have to loop.And another one.
CeciNEstPasUnProgrammeura at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 3
> > You will have to loop.> > And another one.No you don't; you can create a single loop with a bit of index fiddling thataccomplishes the same ;-)kind regards,Jos
JosAHa at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 4
> No you don't; you can create a single loop with a bit> of index fiddling that> accomplishes the same ;-)> > kind regards,> > JosAnd what if the array holds arrays of different sizes?; )
prometheuzza at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 5

> > No you don't; you can create a single loop with a bit

> > of index fiddling that accomplishes the same ;-)

>

> And what if the array holds arrays of different sizes? ; )

No problem grasshopper, never try to look beyond from what you can see

and all will be fine ;-)double[][] doublify(int[][] a) {

double[][] d= new double[a.length][];

for (int r= 0, c= 0;; ) {

if (r == a.length) return d;

if (c == 0) {

if (a[r] != null) d[r]= new double[a[r].length);

else continue;

}

if (c == a[r].length) {

r++;

c= 0;

continue;

}

d[r][c]= a[r][c++];

}

}

kind regards,

Jos ;-)

JosAHa at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 6

> No problem grasshopper, never try to look beyond from

> what you can see and all will be fine ;-)

>

> ... [snip] ...

>

> kind regards,

>

> Jos ;-)

Thank You Master, my mind is much clearer.

Now I will go and meditate some more.

; )

prometheuzza at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 7

You can do this operations only If You have link based arrays.

But in your case, when arrays based on primitive types, you cannot do this.

Because, when you create int[][] array jvm creates array where each cell has

fixed (4 bytes) size. And if you want to convert you array to double[][] jvm will need to create new double[][] array and copy there all values from int[][] array.

Do you understand?

When you create link based array each cell has pointer size and contain null, that is why you may use convert operation here.

Also you may read more about it in Java Specification.

dbes@isd.dp.uaa at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 8

>> double[][] doublify(int[][] a) {

>double[][] d= new double[a.length][];

> for (int r= 0, c= 0;; ) {

>if (r == a.length) return d;

> if (c == 0) {

> if (a[r] != null) d[r]= new

> double[a[r].length);

> else continue;

>

>if (c == a[r].length) {

>r++;

>c= 0;

>continue;

>}

>d[r][c]= a[r][c++];

> }

> }

> kind regards,

>

> Jos ;-)

Smooth!!

But is it faster than using 2 classical for loops?

manuel.leiriaa at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 9

> Smooth!!

Thank you.

> But is it faster than using 2 classical for loops?

No of course not, it was just a little academic exercise to prove my point.

Look at all that testing and adjusting at every single loop entrance. A

nested loop would be easier, more readable and faster because there

would be only one test per element in a row and one additional test for

every row; no to mention all that jumping around caused by those 'continue'

statements ;-)

kind regards,

Jos

JosAHa at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...
# 10
> You can do this operations only If You have link based arrays. But in > your case, when arrays based on primitive types, you cannot do this.Read my totally zen reply #5 ;-)kind regards,Jos
JosAHa at 2007-7-15 0:39:03 > top of Java-index,Java Essentials,Java Programming...