> > 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 ;-)
> 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.
; )
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.
>> 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?
> 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