19

I have an arraylist<interface> Objects get added to this list in a for loop. I would like this arraylist to be empty each time the method is called.

Here is the code:

The array I want to empty here is suggestedPhrases.

public List<Interface> returnSuggestedList(String prefix) {

    String tempPrefix = prefix;


   // suggestedPhrases = null;
    //suggestedPhrases = new ArrayList<Interface>();
    //Vector<String> list = new Vector<String>();

    //List<Interface> interfaceList = new ArrayList<Interface>();
    Collections.sort(wordsList);
    System.out.println("Sorted Vector contains : " + wordsList);
    int i = 0;

    //List<String> selected = new ArrayList<String>();
    for(String w:wordsList){
        System.out.println(w);
        if(w.startsWith(prefix.toLowerCase())) { // or .contains(), depending on 
            //selected.add(w);     // what you want exactly 
        Item itemInt = new Item(w);
        suggestedPhrases.add(itemInt);
    }
}
EI756
  • 557
  • 1
  • 8
  • 19
  • 3
    How about reading the api doc for ArrayList? http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html – JB Nizet Oct 06 '11 at 13:04
  • 3
    -1 Dude, what's so hard about looking at the API? – mre Oct 06 '11 at 13:05
  • 7
    Oh, come on. There are just 20 methods (30 if you count the ones inherited from superclasses). Just scanning their name is sufficient to find the good one. You're lazy. At least admit it. You won't ever be a good developer if you refuse to read the documentation, and are not willing to learn. – JB Nizet Oct 06 '11 at 13:17
  • Thanks for your question, saved me 5 seconds – tjb Sep 18 '12 at 11:55
  • 5
    Cut the sarcasm and down voting guys. The way you find out how to do something is by Googling it and the first hit is this. When I come here in my first 5 seconds of researching this, it would be nice to get the answer instead of this crap. – Allen Edwards Mar 07 '14 at 03:53
  • 4
    Agree with Allen, the first google hit for 'how to empty out an arraylist' is this. SO is a resource for finding stuff out, what does it matter if it's found from the API or a question here on SO. – Siddhartha Jan 13 '15 at 20:00

2 Answers2

37

If the array persists across calls to your method (e.g. if it's a member of the class), you can call suggestedPhrases.clear() to clear it.

From your example there's doesn't appear to be any need to persist suggestedPhrases across calls, so you can simply create (and return) a new array list every time your method is called:

public List<Interface> returnSuggestedList(String prefix) {
   ArrayList<Interface> suggestedPhrases = new ArrayList<Interface>();
   // populate suggestedPhrases here
   return suggestedPhrases;
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
8

You can call the clear method, but normally I prefer creating a new instance instead of re-using an existing one. The overhead is negligible (except for really performance-critical sections) as today's JVMs can handle object re-allocation quite well.

michael667
  • 3,241
  • 24
  • 32