Arrays.fill()

Discussion in 'Java' started by Ike, Jul 1, 2003.

  1. Ike

    Ike Guest

    Yow! This doesnt do what I thought it would do! If I have a 2D boolean
    array, and say I set a tuple to all false, then go to set the other tuples
    using Arrays.fill as in the third line below:

    boolean brod[][]= new boolean[3][3];
    Arrays.fill(brod[0],true);
    Arrays.fill(brod,brod[0]);

    Then, if I subsequently set, say brod[0][1]=true; then all brod[?][1] become
    true.....but I dont want that at all! How can I use Arrays.fill to fill a 2D
    (or N-D) array? thanks, Ike
    Ike, Jul 1, 2003
    #1
    1. Advertising

  2. Ike

    Adam Maass Guest

    "Ike" <> wrote:
    > Yow! This doesnt do what I thought it would do! If I have a 2D boolean
    > array, and say I set a tuple to all false, then go to set the other tuples
    > using Arrays.fill as in the third line below:
    >
    > boolean brod[][]= new boolean[3][3];
    > Arrays.fill(brod[0],true);
    > Arrays.fill(brod,brod[0]);
    >
    > Then, if I subsequently set, say brod[0][1]=true; then all brod[?][1]

    become
    > true.....


    Because all the brod[?] refer to the same array after your line 3.

    > but I dont want that at all! How can I use Arrays.fill to fill a 2D
    > (or N-D) array? thanks, Ike



    You can't, not in the way you want.


    If your array is always 3 x 3, you might get away with:

    boolean[][] brod = new boolean[][] {
    {true, true, true,},
    {true, true, true,},
    {true, true, true,},
    };


    If not, you'll have to write some loops to set the elements.


    -- Adam Maass
    Adam Maass, Jul 1, 2003
    #2
    1. Advertising

  3. The key to understanding this behaviour is that the statement:

    boolean brod[][] = new boolean[3][3];

    does not declare a two-dimensional array of booleans as it would in C/C++.
    In Java this declares brod as an array of references to arrays of booleans.
    So, your second call to Arrays.fill:

    Arrays.fill(brod,brod[0]);

    sets each element of brod to point to the array referenced by brod[0]; that
    is, now all three elements in brod point to the same array. So, of course
    setting brod[0][1]=true means brod[1][1] will also be true, since brod[0]
    and brod[1] point to the same array.

    Adam

    "Ike" <> wrote in message
    news:eI3Ma.72388$...
    > Yow! This doesnt do what I thought it would do! If I have a 2D boolean
    > array, and say I set a tuple to all false, then go to set the other tuples
    > using Arrays.fill as in the third line below:
    >
    > boolean brod[][]= new boolean[3][3];
    > Arrays.fill(brod[0],true);
    > Arrays.fill(brod,brod[0]);
    >
    > Then, if I subsequently set, say brod[0][1]=true; then all brod[?][1]

    become
    > true.....but I dont want that at all! How can I use Arrays.fill to fill a

    2D
    > (or N-D) array? thanks, Ike
    >
    >
    Adam P. Jenkins, Jul 1, 2003
    #3
    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,532
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,225
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    550
  4. std::fill arrays

    , Jul 20, 2006, in forum: C++
    Replies:
    2
    Views:
    581
    Mike Wahler
    Jul 21, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
Loading...

Share This Page