Hey

I want to "reset" a array....What would be the correct way to do it?

Code A:

for (int i=0;i<=somearray.length;i++)
{
 somearray[i]=0;
}

or Code B:

somearray=null;

Thank you

A would only work for an array of ints. And what do you mean by resetting (u need to provide some context)

A would only work for an array of ints. And what do you mean by resetting (u need to provide some context)

I mean by resetting by leaving it as it originally was when I first declared it.

(Or destroying the object/instance of the object itself would also be valid)

If you put it like that then setting the value to null would be your answer but you are not responsible for destroying objects. When you deference an object by setting the reference to null if there are other references to it then that object becomes available for garbage collection.

The garbage collector (GC) is responsible for freeing allocated memory and you are not in control of when the GC runs.

So you need not worry to much about resetting object state (Java takes care of that for you).

only C++ programmers need worry :D

If you put it like that then setting the value to null would be your answer but you are not responsible for destroying objects. When you deference an object by setting the reference to null if there are other references to it then that object becomes available for garbage collection.

The garbage collector (GC) is responsible for freeing allocated memory and you are not in control of when the GC runs.

So you need not worry to much about resetting object state (Java takes care of that for you).

only C++ programmers need worry

It is a web service so I believe I do; I reutilizes the objects (declaring instances of them) at the beginning of the code and I destroy them at the end (with this null fix)

Don't confuse variables and objects. A variable is just a reference (pointer),, not an object. You can't "declare an instance". You can only declare variables or create new instances. int[] arr; declares a reference variable, initially containing a null pointer new int[99] creates an array of 99 ints int[] arr = new int[99]; creates an array and sets the variable arr to refer to it arr = null means that arr no longer refers to the array. If that was the last remaining reference to that array then the array will be garbage collected at some time later. That's a function of GC that you do not control. There is no concept of "destroying" an object in Java. for (int i=0;i<=somearray.length;i++) somearray[i]=0; resets all the elements of the array to zero.

Whether you clear all the elements back to zero, or dereference the array then create a new one is your choice. In practice it won't make the slightest difference unless the array is so large (millions of elements) that you are running short of memory.

Whether you clear all the elements back to zero, or dereference the array then create a new one is your choice. In practice it won't make the slightest difference unless the array is so large (millions of elements) that you are running short of memory.

There is the reason why I think dereferencing them makes a difference: The array is (I believe) 50000 going thru all the positions and setting them to 0 (some of the positions are never used, justed declared for extra space) is stupid. Thats why I think the null method is better.

You may be right. But a loop like that will be incredibly fast, and creating a new object and garbage collecting the old one will have some overhead, so it's not obvious which would be faster. You would have to benchmark both to see what happens in reality. Either way it's going to be some tiny fraction of a second, certainly too fast for a human to see.

You may be right. But a loop like that will be incredibly fast, and creating a new object and garbage collecting the old one will have some overhead, so it's not obvious which would be faster. You would have to benchmark both to see what happens in reality. Either way it's going to be some tiny fraction of a second, certainly too fast for a human to see.

Noted. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.