0

Can someone show me how to remove an object from an array. But here's the catch (well for me), the array is something like this

member[0] = new Member("John Cena" , "p000001");

I want to be able to search a name, then when that is detected, to be able to remove. is it possible?

public static void remove(){
    System.out.println("Name: ");
    String removeName = input.next();
    System.out.println("ID: ");
    String removeID = input.next();
    for(int i = 0; i < member.length; i++){
         //not sure if this is the right direction
    }
}

EDIT: Cannot use ArrayList or list because of requirements in an Assignment. Would have used it since it is easier but can't.

  • To avoid array swap complexity. Convert the array to a list and then find the name in the list and delete the object. – FallAndLearn May 27 '16 at 14:32
  • Yes, it is possible. In Java, if you're going to remove something from an array, you either need to iterate through it backwards or using a Collections iterator like seen [here](http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re), if you haven't implemented the `equals()` method in the `Memeber` class – Draken May 27 '16 at 14:32
  • Do you have to use an array? Can you use a collection like `List`? – Makoto May 27 '16 at 14:36
  • @Makoto yea I have to an array – HelpwAssignments May 27 '16 at 15:17

4 Answers4

0

You can either use an ArrayList (or better yet, ArrayList<Member>), which you can populate with members, and then use the indexOf method to search through.

Alternatively, if you have to or would rather use arrays, create a loop like you have that iterates through each index of your members array. The only tricky part is removal requires that you remove it from the array, and then shift each index in front of it down so that the blank space is remove.

In other words, you need to delete index i and use a loop so that you move the member at i + 1 down to i, member i + 2 moves down to i + 1, and so on until you reach the end of the array.

With all that being said, I'd encourage use of the ArrayList. It does all of the operations I just described for you and makes matters a lot easier.

Jeremy Kato
  • 467
  • 5
  • 12
  • @ Jeremy Kato sadly I have to use an array for this assignment, no arraylist or list. Would have used it already but cant. That seems alot of work, I guess it makes sense to cover up the removed object. I don't know what to write – HelpwAssignments May 27 '16 at 14:41
0

Arrays are fixed-size. This means that the best you can do for an object array is set the value to null. If you want to remove it completely, you'll want to use ArrayList instead. In that case,

ArrayList<Member> members = new ArrayList<Member>();
for (int i = members.length-1; i>=0; i--) {
    if (members.get(i).getName().equals(toRemove)) {
        members.remove(i);
    }
}
icez
  • 1
  • 2
0

Sorry for my previous wrong answer. This should be the correct way of removing your Members

 List<Member> l = new ArrayList<>(Arrays.asList(member)); 
 for (Iterator<Member> iter = l.listIterator(); iter.hasNext(); ) {
     Member a = iter.next();
     if ( (a.getId() == removeID) || (removeName.equals(a.getName)) )  {
          iter.remove();
     }
 }

 Member[] newMembers = l.toArray(new Member[l.size()]);

PS: Please get removeID like this;

int removeID = input.nextInt();
erolkaya84
  • 1,769
  • 21
  • 28
  • Then you can use ArrayUtils.removeElement(members, i); but I guess the assignment wants you to write the algorithm. – erolkaya84 May 27 '16 at 15:16
0

It's a bit wonky that you have to use an array, because:

  • You can't truly guarantee that there isn't going to be more than one person with the same name, since the array doesn't guarantee unique entries.
  • Arrays don't dynamically resize, leaving you to have to do that yourself.

It can still be done though. Here's an example using Java 8's Stream API.

Let's assume you're looking for a Member with the same name and ID. Then, you're going to want to filter out any elements that are not that entry, collect them to a List, and then turn that List into an array.

So the result would be:

member = Arrays.stream(member)
               .filter(m -> removeName.equals(m.getName())
                            && removeID.equals(m.getID()))
               .collect(Collectors.toList())
               .toArray(new Member[0]);
Makoto
  • 104,088
  • 27
  • 192
  • 230