arrays

Discussion in 'Java' started by Myriam Abramson, May 11, 2004.

  1. Is there a way of "redefining" the storage allocated to arrays so that you can use them as one-dimensional or two-dimensional?
    --
    myriam
     
    Myriam Abramson, May 11, 2004
    #1
    1. Advertising

  2. Myriam Abramson

    Rob Shepherd Guest

    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
     
    Rob Shepherd, May 11, 2004
    #2
    1. Advertising

  3. 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.
     
    Michael Borgwardt, May 11, 2004
    #3
  4. 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;
    }

    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
     
    John C. Bollinger, May 11, 2004
    #4
  5. Myriam Abramson

    Andy Fish Guest

    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" <> wrote in message
    news:c7qm64$j85$...
    > 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
     
    Andy Fish, May 11, 2004
    #5
  6. Myriam Abramson

    Roedy Green Guest

    On 11 May 2004 09:22:30 -0400, Myriam Abramson <>
    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.
     
    Roedy Green, May 11, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,893
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,339
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    589
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    404
  5. Philipp
    Replies:
    21
    Views:
    1,183
    Philipp
    Jan 20, 2009
Loading...

Share This Page