Solved

Java Memory Management

Posted on 1998-12-08
7
172 Views
Last Modified: 2008-10-12
I realize this is probably an easy one, but I have just started learning Java in earnest.  According to my book on Java 1.1, it seems there is no such thing as dynamic memory allocation, and therefore no dynamic arrays.  I want to create an image viewer that loads a series of images into an array, but I don't want the array size to be hardcoded.  Rather I want to pass the array size by HTML parameter.  Can I do this?  How do I do this?
0
Comment
Question by:NickHeitz
7 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1229005
You can use the
java.util.Vector class.

It implements a dynamic collection of objects which can be indexed, enumerated and etc. It extends its internal size each time it is necessary, transparently to the developer.

If you want ot use a key assigned to the object (image) you can use a Hashtable as well.

Cheers,
  Nikolay
0
 
LVL 27

Expert Comment

by:BigRat
ID: 1229006

public ImageClass[] loadImages(int nrImages) {
   
    ImageClass icarray[] = new ImageClass[nrImages];

    for(int i=0; i<nrImages; i++) {
        /* or something completely similar */
        icarray[i] = loadImage(i);
    }
    /* and retunr the array so created */
    return icarray;
}
0
 
LVL 16

Expert Comment

by:imladris
ID: 1229007
Sure there is. In fact, it's all Java does. If you are passing in a number via a parameter to indicate how many images are going to be in an image array you would do the following:

get the parameter (assume the value winds up in imgnum) then:

Image imagearray[]=new Image[imgnum];

and voila you have an array of images with imgnum elements.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:NickHeitz
ID: 1229008
I understand how to pass parameters and create arrays, but I need to dynamicall create an array, whose size is passed as a text string into the applet...I won't know the size of the array before the init method is fired, and I may not know until after!  thanks.
0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 1229009
I'm not clear on the problem then. There certainly isn't any constraint on when you execute a line like:

Image imagearray[]=new Image[imgnum];

You can do it at the beginning of init, at the end, or in any other method. The imagearray could be a class variable (static) a member variable, or an automatic. It makes no difference. So you should be able to do this whenever you find out what size the array needs to be.
That's about as dynamic as it gets. The opposite is static, which is what a C compiler does for external array declarations like:

int intarray[50];

It reserves 50*sizeof(int) bytes of memory in the static data space before the program ever even gets control.

Are you in fact wondering how to convert a string to a number? Also no problem, use one of the parseInt methods in the Integer class.

If that still doesn't help, please clarify the problem.

0
 

Author Comment

by:NickHeitz
ID: 1229010
My only question now concerns Java's ability to dynamically expand arrays, say, like VB's Redim statement.  I have read something about a java Vector class, and I think this might also provide a solution.  Thanks.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1229011
Yes, correct. The Vector class provides dynamic expansion for you. It exacts a price though. Each access to an element has to be done through a method call.

An intermediate solution is to do a "Vector" yourself by allocating a bigger array, when needed, and copying the existing elements across using System.arraycopy.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 77
Eclipse Neon and jdk 1.8.0 11 122
sites similar to codingbat to improve coding hanson skills 3 40
MySQL  on Tomcat 8 23
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now