• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Removing an element

 
Greenhorn
Posts: 15
Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,

     
    lowercase baba
    Posts: 13081
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Marshal
    Posts: 76860
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 . . .
     
    author
    Posts: 23928
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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;

    }
    }

    }
    }
     
    A tiny monkey bit me and I got tiny ads:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic