arrays

M

Myriam Abramson

Is there a way of "redefining" the storage allocated to arrays so that you can use them as one-dimensional or two-dimensional?
 
R

Rob Shepherd

Myriam said:
Is there a way of "redefining" the storage allocated to arrays so that you can use them as one-dimensional or two-dimensional?

You could just make another 2d array to "Hold" your 1d arrays

such as...

String[] my_first_array = new String[] {"this","is","my","first","single D","array"};

String[] my_second_array = new String[]{"and","here","is","another","single D","array"};

String[][] my_2d_array = new String[][]{ my_first_array, my_second_array};

System.out.println(my_2d_array[0][3] + " or " + my_2d_array[1][3]);

my_first_array[3] = "worst";

System.out.println(my_2d_array[0][3] + " or " + my_2d_array[1][3]);

.....

$> java [above]
first or another
worst or another
$>

The last two lines show, my_2d_array only references the original arrays so the two may be
used together [in 1d or 2d fashion] without duplicating the actual data...

Rob
 
M

Michael Borgwardt

Myriam said:
Is there a way of "redefining" the storage allocated to arrays so that you can use them as one-dimensional or two-dimensional?

No.

You can do something similar with the buffers of java.nio, but not with the simple
array syntax.
 
J

John C. Bollinger

Myriam said:
Is there a way of "redefining" the storage allocated to arrays so that you can use them as one-dimensional or two-dimensional?

Write your code in Fortran?

You can't do it with regular Java array syntax, but you can simulate it
by wrapping up a 1-D array in an object that manages the
multi-dimensional indexing. Something along these lines:

public class VariableDimensionArray {

private Object[] items;

public VariableDimensionArray(int length) {
items = new Object[length];
}

public Object getElement(int i) {
return items;
}

public Object getElement(int i, int iStride, int j) {
return items[i * iStride + j];
}

/* higher dimensional accessors as necessary */

/* element setters handle indexing the same way as getters */
}


That's just a rough skeleton, but hopefully it's enough to show what I mean.


John Bollinger
(e-mail address removed)
 
A

Andy Fish

you can't do nasty things the way you probably would in C.

However, if you have a one dimensional array of length numrows*numcols that
you want to access as if it was two-dimensional, instead of acessing element
[row,col] just use [row*numcols+col]

you can use them as one-dimensional or two-dimensional?
You could just make another 2d array to "Hold" your 1d arrays

such as...

String[] my_first_array = new String[] {"this","is","my","first","single D","array"};

String[] my_second_array = new
String[]{"and","here","is","another","single D","array"};
String[][] my_2d_array = new String[][]{ my_first_array, my_second_array};

System.out.println(my_2d_array[0][3] + " or " + my_2d_array[1][3]);

my_first_array[3] = "worst";

System.out.println(my_2d_array[0][3] + " or " + my_2d_array[1][3]);

....

$> java [above]
first or another
worst or another
$>

The last two lines show, my_2d_array only references the original arrays so the two may be
used together [in 1d or 2d fashion] without duplicating the actual data...

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top