0

I am trying to delete from an array the first occurence, not all elements like the searched element The array is something like:

 String[] names = {"Becky", "Rosa", "Tina", "Jill", "Rosa", "Bill"};

And I want to be able to say remove(Rosa) and only find and remove the first element in the array named Rosa.

6
  • 2
    Might want to qualify what you mean by delete, and consider using a List. Commented Sep 9, 2014 at 22:09
  • your example doesn't even compile. Commented Sep 9, 2014 at 22:11
  • it was just an example, it wasn't from anything @djechlin Commented Sep 9, 2014 at 22:12
  • The same question is asked here: stackoverflow.com/questions/7940337/… Commented Sep 9, 2014 at 22:12
  • 2
    @djechlin: What about that array declaration is invalid syntax? Commented Sep 9, 2014 at 22:16

1 Answer 1

1

So you might want to consider using an Arraylist for dynamic removal and resizing

import java.util.ArrayList;


ArrayList<string> persons = new ArrayList<string>();
persons.add("Becky");
//... adding in people to the list

//then if you want to remove  someone name "becky"
persons.removeall.(collection.singleton("Becky"))
Sign up to request clarification or add additional context in comments.

6 Comments

Use a while loop to remove all Beckys `while(persons.remove("Becky");
don't have to use a while, can just use removeall
But then you have to deal with collections.
Not like thats a big deal removeall.(collection.singleton("Becky"))
Too-shay, I have never done Collections before so I had no idea, you should change your post to removeAll instead of while
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.