Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Memory Management

Posted on 1998-12-08
7
Medium Priority
?
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 150 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

722 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