how can we free the memory of array

how can we refresh the memory of an array every time......if we want new values or fresh values each time.........becuase array index out of exception error is coming up and i want to deallocate the memory at the end of iteration or in the starting
[255 byte] By [ping.sumita] at [2007-11-26 15:25:54]
# 1

> how can we refresh the memory of an array every

> time......if we want new values or fresh values each

> time.........becuase array index out of exception

> error is coming up and i want to deallocate the

> memory at the end of iteration or in the starting

This post consist of 95% gibberish. The only part that is not gibberish is "array index out of bounds exception" and that has nothing to do with memory at all.

Please attempt to learn some basic Java before injuring yourself permanently.

[url=http://java.sun.com/docs/books/tutorial/ ]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html ]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com ]http://javaalmanac.com [/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance ]The Java Developers Almanac[/url].

[url=http://www.jguru.com ]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com ]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites ]Thinking in Java[/url] (Available online.)

Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh ]Effective Java[/url]

Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance ]Head First Java[/url].

James Gosling's [url=http://www.bookpool.com/sm/0321349806 ]The Java Programming Language[/url]. Gosling is

the creator of Java. It doesn't get much more authoritative than this.

cotton.ma at 2007-7-8 21:41:34 > top of Java-index,Java Essentials,New To Java...
# 2

At the end of iteration if u r complete with the elements of present array and if u don't need it anymore, then just do one of the two things.

1>if u just need to deallocate, then

presentArray=null;

2>if u need to reload it with new values

presentArray=new ArrayType[];

in Java every thing concerned to memory is taken care by garbage collector.

Er.Vela at 2007-7-8 21:41:34 > top of Java-index,Java Essentials,New To Java...
# 3

is that array of primitive types or reference types.

if it is reference type, all objects are clean-uped by Garbage collector only. if u want to make your object eligible for garbage collection then set its reference to null.

if you want to clean all the elements of an array (of references) before assigning new references then create a method which iterate over the array and sets all the references to null.

AnjanReddya at 2007-7-8 21:41:34 > top of Java-index,Java Essentials,New To Java...
# 4

IndexOutOfBoundsException has nothing to do with deallocating memory or not. It's just some error in your code that tries to access a memory location beyond your array allocated memory. Verify your code that it doesn't exceed the bounds of your array and you should be fine!

If you are reusing the same array variable in various iterations and it doesn't allways have the same length, then you should create a new array like

>2>if u need to reload it with new values

>presentArray=new ArrayType[];

with the appropriate size.

Peetzorea at 2007-7-8 21:41:34 > top of Java-index,Java Essentials,New To Java...
# 5

> how can we refresh the memory of an array every

> time......if we want new values or fresh values each

> time.........becuase array index out of exception

> error is coming up and i want to deallocate the

> memory at the end of iteration or in the starting

1==> array index out of bound exception comes when we try to access excess of the memory which is allocated

lets suppose you can declared an array like below

int student[] = new student[2];

hence if you try to access student[2] or above then you get

"ArrayIndexOutOfBoundsException "

2==>The answer to your question is you can use ArrayList instead of Array because case where you need to insert and deleted at any position ArrayList is the best .

For removing all the references in the ArrayList there is a simple command clear(); JVM will do the rest ;)

import java.util.ArrayList;

ArrayList student = new ArrayList(();

student.add("Merry");

student.add( any object of a class);

student.add(any variable);

student.clear();

hav u solved the problem?

Message was edited by:

online.programmer

online.programmera at 2007-7-8 21:41:34 > top of Java-index,Java Essentials,New To Java...