0

This isn't a question that I am expecting a specific answer to since it is pretty broad. I'm teaching myself Java and am focusing specifically on dynamic memory allocation. Let's make a very oversimplified example: Say that I have a really basic data entry screen. So basic that all that happens is the user enters a series of first names, maybe for an employee directory or something. On my JFrame, I have a single JTextField control where these names are entered. The number of employees is known only at run time. The user enters a name and hits the enter key, which commits the name to memory and creates a new object to store the next name (silly, I know, but I'm trying to focus). Something like this (don't take this too literally - I obviously didn't compile this):

public class Employee {
  String fName;

  public void setName( String n ) {
      fName = n;
  }
};

public class JFrameEmp {

//blah, blah, blah

JTxtName.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
        /* Handle the enter key */
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            /* Store name */
            this.setName(JTxtName.getText());

            /* Create New Object */
            Employee next = new Employee();
            }
        };
    }
}

Where I need help is on the last line. Even if this were to work, let's say that I wanted to print out a list of the names entered. In my approach, I see no way to uniquely identify each object (so that I can iterate through them).

Do I need to create an array to store these? But that has a fixed length and lives on the stack. I'd rather find a way to use the heap to allow for an open-ended list.

What should I be reading to learn how to do this? It must be a very common thing to do, but the books that I have don't seem to cover it.

Thanks. I hope that I have explained this well enough without going into too much detail.

1
  • 1
    have you looked into the Map<K,V> interface? Commented Jun 16, 2012 at 3:16

3 Answers 3

2

It sounds like you want to store a List of Employees. Java provides dynamically sized array with its ArrayList class.

Here is an example:

ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("Alice"));
employees.add(new Employee("Bob"));
System.out.println(employees.get(0)); // prints out Alice
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I think that is the way to proceed.
2

You could use an array. You might find it useful to have a look at the Java Collections framework, in particular the page about the implementations.

But yes you will need to be adding the objects to some sort of data structure otherwise they will be getting cleaned up by the garbage collector after there is no valid reference to them.

1 Comment

No problem. Also if you're looking to iterate through the objects, a lot of the Java implementations of these data structures are iterable, which means you can do this :)
2

It seems like you are looking for a data storage structure.

You could use a built in array, which is actually stored on the heap in java(the only issue is that you would have to keep track of size and reallocate when more space is needed).

A better option would be an ArrayList, which has built in add methods that automatically resize when needed.

You could also use a HashMap, which would allow to quickly search for your employees by name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.