Solved

Java Memory Management

Posted on 1998-12-08
7
173 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

786 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