Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:37:33+00:00 2026-06-05T17:37:33+00:00

This isn’t a question that I am expecting a specific answer to since it

  • 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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:37:35+00:00Added an answer on June 5, 2026 at 5:37 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This isn't a question that has a black/white yes/no answer, this is more a
This isn't actually a homework question per se, just a question that keeps nagging
This isn't a development question, I'm Looking for a plugin for Joomla that allows
This isn't quite the same question as many others that it might resemble. Consider
This isn’t a specific question with a specific solution; but it’s rather a response
This isn't that complicated of a question, but I can't wrap my head around
This isn't the best question ever, but since search engines feel the need to
This isn't meant as a troll or flamebait or anything like that. I've been
This isn't strictly a programming question, but I'm asking it here because it's certainly
This isn't a code question for once, but it definitely has me confused. Basically,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.