• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing an element  RSS feed

 
Lonnie Wood
Greenhorn
Posts: 15
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys/gals,

I've built an array of objects (accounts), populated it with attributes, name, ID, balance.....

I'm then displaying the array and asking the user which ID they'd like to delete. I now have an input that matches an index (maybe, I'll need to validate it later)

showAccounts(accounts);
input = JOptionPane.showInputDialog("Please enter the Account Number that you'd like to delete.");
del = Integer.parseInt(input);
delete = (del - 1); // to make input match index

Here's where I'm stuck.
I really just want to discuss the best way to tackle this, then maybe we'll talk about code.

I'm going to need to add other entries into the array after I delete one. So I add three accounts, I now have 7 null accounts

name Acct#
George 1
Linda 2
Bill 3

The user types in 2 which is index 1.

Here's what I think I want to do.
  • set index 2 to null, which I beleive will remove all other field values.
  • Sort the accounts array by index, moving all nulls to the rear
  • Figure out how to find the first Null index and then add there.

  • I know to look for null in the array, but not how to stop and capture the index number of the first one.

    Does that sound correct, or is there a standard way to do this that I haven't been taught or found yet?

    As always, thanks for your help,

     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There are probably other collections you could use instead of an array. Perhaps a HashMap would work better, as it would let you find or delete elements by a key value (account number sounds like a good candidate).
     
    Lonnie Wood
    Greenhorn
    Posts: 15
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fred,

    I'm certain there are many other fine ways to do this, but at this time, the only one that I know, and the one that my instructor is looking for is an array.

    I do find it funny that my textbook covers how to add to an array but not remove. It shows how to do it with an ArrayList, but once again, I've been asked to use an array.

    Thanks though.
     
    Campbell Ritchie
    Marshal
    Posts: 55761
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lonnie Wood wrote: . . . It shows how to do it with an ArrayList, . . .
    And I won't tell you that ArrayList uses an array. And I won't tell you about the src.zip file which lives in your Java™ installation folder. And I won't tell you that contains the code for ArrayList. And I won't tell you, that code describes how to remove something from an ArrayList . . .
     
    Henry Wong
    author
    Sheriff
    Posts: 23283
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lonnie Wood wrote:
  • Sort the accounts array by index, moving all nulls to the rear



  • Sorting is an expensive operation. If the goal is to just move the nulls to the rear, you can probably use the System.arraycopy() method to move array elements around.

    Henry
     
    Lonnie Wood
    Greenhorn
    Posts: 15
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm certain that most of you know this, but in case anyone is looking at this, this is how I did it.

    deleteStudentID(accounts, userInput);
    // where accounts is the Array and userInput is the studentID to delete.

    public static void deleteStudentID(StudentAccount accounts[], int stuID)
    {
    for (int x = 0; x <= accounts.length - 1; x++)
    {
    if (accounts[x] != null && accounts[x].getStudentID() == stuID)
    {
    if (stuID != accounts[x].getStudentID())
    {
    System.out.println("Invalid Entry");
    }
    else
    {

    accounts[x] = null;

    }
    }

    }
    }
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!