How to destroy an array.

P

Perfect Reign

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 - (e-mail address removed) - 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".
 
R

RS

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.
 
W

Wendy Smoak

Perfect Reign said:
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];
 
P

Perfect Reign

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 - (e-mail address removed) - 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".
 
P

Perfect Reign

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 - (e-mail address removed) - 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".
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top