How to destroy an array.

Discussion in 'Java' started by Perfect Reign, Apr 7, 2005.

  1. I am working with a class in which I am creating a 2d array with nine
    elements. During the course of the class I set values to each of the nine
    elements:

    int intSquareOwner[][] = {{0, 0, 0}, {0, 0, 0,}, {0, 0, 0}};

    At one point, I want to redim the array so that each element is set back to
    a value of null or zero (doesn't really matter.)

    Right now, I've written this:

    for( int x=0; x<3; x++)
    {
    for( int y=0; y<3; y++)
    {
    intSquareOwner[x][y] = 0;
    }


    The above snippet works to reset all values to zero. Isn't there a way to
    destroy or close an array then just reinitialize it?
    --
    kai - - www.perfectreign.com

    kai:/> format a:
    Error: The DOS concept of formatting disk media is screwed.
    To format a floppy, use "fdformat /dev/fd0"
    and then "mkfs.minix /dev/fd0".
     
    Perfect Reign, Apr 7, 2005
    #1
    1. Advertising

  2. Perfect Reign

    RS Guest

    You can just reinitialize it in the same way you declared it.

    int intSquareOwner[][] = {{0, 0, 0}, {0, 0, 0,}, {0, 0, 0}};

    /*
    for( int x=0; x<3; x++)
    {
    for( int y=0; y<3; y++)
    {
    intSquareOwner[x][y] = 0;
    }
    */

    Instead of above:

    intSquareOwner[][] = {{0, 0, 0}, {0, 0, 0,}, {0, 0, 0}};
    that's reset all to 0.

    Or You can reset to null

    intSquareOwner[][] = null

    and restart.
     
    RS, Apr 7, 2005
    #2
    1. Advertising

  3. Perfect Reign

    Wendy Smoak Guest

    "Perfect Reign" <> wrote:
    > I am working with a class in which I am creating a 2d array with nine
    > elements. During the course of the class I set values to each of the nine
    > elements:
    >
    > int intSquareOwner[][] = {{0, 0, 0}, {0, 0, 0,}, {0, 0, 0}};
    >
    > At one point, I want to redim the array so that each element is set back

    to
    > a value of null or zero (doesn't really matter.)


    You'll probably get better and more precise answers to questions of this
    type on comp.lang.java.help.

    To reset each position to zero, the code you wrote is fine. (Assuming it
    works, I didn't check.) You could also use one of the Arrays.fill(...)
    methods:
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html
    (Since you have an array of arrays, you'll probably have to put it in a
    loop, not a huge improvement on what you already have.)

    If it wasn't going to happen often, I would probably just be lazy and write:
    intSquareOwner = new int[3][3];
    It's inefficient because it discards the original array and creates a new
    one. But it's also easy to understand and takes only one line of code.

    Now I'm going to be picky again:

    Your subject line says you want to destroy an array, but that's not what you
    go on to talk about. If you really want it to go away, get rid of all the
    references to it and let the garbage collector do its job.

    To me, 'redim the array' means changing its dimensions, which you don't
    appear to be doing.

    And you can't set a primitive type like an int to null. Try to compile
    this:
    int x = null;

    Though it works either way, most people would put the brackets with the
    type:
    int[][] intSquareOwner = {{0, 0, 0}, {0, 0, 0,}, {0, 0, 0}};

    Which is the same as writing:
    int[][] intSquareOwner = new int[3][3];

    --
    Wendy
     
    Wendy Smoak, Apr 7, 2005
    #3
  4. On 7 Apr 2005 09:16:01 -0700, someone posing as RS donned fireproof
    bloomers and chiseled in the wall:

    > You can just reinitialize it in the same way you declared it.
    >
    > int intSquareOwner[][] = {{0, 0, 0}, {0, 0, 0,}, {0, 0, 0}};
    >
    > /*
    > for( int x=0; x<3; x++)
    > {
    > for( int y=0; y<3; y++)
    > {
    > intSquareOwner[x][y] = 0;
    > }
    > */
    >
    > Instead of above:
    >
    > intSquareOwner[][] = {{0, 0, 0}, {0, 0, 0,}, {0, 0, 0}};
    > that's reset all to 0.
    >


    Actually I had tried that and it retained the original values.
    > Or You can reset to null
    >
    > intSquareOwner[][] = null


    Hadn't tried that yet though.

    Thanks!

    --
    kai - - www.perfectreign.com

    kai:/> format a:
    Error: The DOS concept of formatting disk media is screwed.
    To format a floppy, use "fdformat /dev/fd0"
    and then "mkfs.minix /dev/fd0".
     
    Perfect Reign, Apr 7, 2005
    #4
  5. On Thu, 7 Apr 2005 09:19:04 -0700, someone posing as Wendy Smoak donned
    fireproof bloomers and chiseled in the wall:


    > int[][] intSquareOwner = {{0, 0, 0}, {0, 0, 0,}, {0, 0, 0}};
    >
    > Which is the same as writing:
    > int[][] intSquareOwner = new int[3][3];


    Thanks for the nice answer! I'll do some more reading. I have the j2se
    docs bookmarked, but they are about as helpful as reading greek to me. I
    did look at the arrays page, but hadn't noticed anything useful. I'll look
    further.
    --
    kai - - www.perfectreign.com

    kai:/> format a:
    Error: The DOS concept of formatting disk media is screwed.
    To format a floppy, use "fdformat /dev/fd0"
    and then "mkfs.minix /dev/fd0".
     
    Perfect Reign, Apr 7, 2005
    #5
    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. Cyril Vi?ville

    Block in Perl/TK MainWindow destroy

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    0
    Views:
    622
    Cyril Vi?ville
    Jun 29, 2004
  2. Jenny

    Destroy COM object

    Jenny, Jul 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    516
    Alvin Bruney
    Jul 21, 2003
  3. Jenny

    Destroy COM object

    Jenny, Jul 31, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    504
  4. Ook
    Replies:
    2
    Views:
    338
  5. Replies:
    4
    Views:
    335
    Victor Bazarov
    Feb 14, 2007
Loading...

Share This Page