Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > arrays

Reply
Thread Tools

arrays

 
 
Myriam Abramson
Guest
Posts: n/a
 
      05-11-2004

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

 
Reply With Quote
 
 
 
 
Rob Shepherd
Guest
Posts: n/a
 
      05-11-2004
Myriam Abramson wrote:
> 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
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      05-11-2004
Myriam Abramson wrote:
> 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.
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      05-11-2004
Myriam Abramson wrote:

> 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[i];
}

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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Andy Fish
Guest
Posts: n/a
 
      05-11-2004
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]

"Rob Shepherd" <(E-Mail Removed)> wrote in message
news:c7qm64$j85$(E-Mail Removed)...
> Myriam Abramson wrote:
> > 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



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-11-2004
On 11 May 2004 09:22:30 -0400, Myriam Abramson <(E-Mail Removed)>
wrote or quoted :

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


see http://mindprod.com/jgloss/matrix.html

you can have two dimensional arrays, but the pieces for them are
scattered all over RAM.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM
initializing arrays of arrays Mantorok Redgormor C Programming 4 09-11-2003 02:08 AM



Advertisments