Solved

Java Memory Management

Posted on 1998-12-08
7
171 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:NickHeitz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

772 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

14 Experts available now in Live!

Get 1:1 Help Now