Solved

Java Memory Management

Posted on 1998-12-08
7
176 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
Technology Partners: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 85
Convert from a json string array to a Java object 3 81
java imports not found 4 71
netbeans can not run self signed app 8 38
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 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…
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.

751 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