ChocoPython
ChocoPython

Reputation: 65

Why won't the list be empty?

I am trying to empty out the list, meaning removing all the items. According to my professor, I should manually trace the for loop. I know that as each item is removed, the size changes, but I do not understand how that does relate with the problem. By the way, as you see the code below, they're all in a separate activity from the MainActivity. Here is the code that I am working on:

public void removeAll(View view)
{
    //Declare the reference
    StringList the_list;
    int i;



    //Access the singleton list class
    the_list = StringList.getInstance();

    //Try to remove all items in the list

    try
    {
        //Look through the list to remove each item
        for(i = 0; i <= the_list.size(); i++)
        {
            the_list.remove(i);
        }

        Toast.makeText(RemoveAllActivity.this, "All items are removed successfully!", 
                        Toast.LENGTH_SHORT).show();
    }
    catch(IndexOutOfBoundsException e)
    {
        Toast.makeText(RemoveAllActivity.this, "Error: Removing all items have failed!", 
                Toast.LENGTH_SHORT).show();
    }
}

If you want to know what the MainActivity looks like, I will show the function that I am working with:

public class MainActivity extends Activity {

public static TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StringList the_list;

    // set the reference to the "main" textview object so
    // we do not have to retrieve it in every method below

    tv = (TextView) findViewById(R.id.text_main);

    // create/access the list of strings

    the_list = StringList.getInstance();

    // put some strings on the list (if the list is empty).  Note that the
    // "new" list might not be empty due to a restart of the app

  if(the_list.isEmpty())
  { 

    the_list.add(the_list.size(), "pizza");
    the_list.add(the_list.size(), "crackers");
    the_list.add(the_list.size(), "peanut butter");
    the_list.add(the_list.size(), "jelly");
    the_list.add(the_list.size(), "bread");

  }

}  // end onCreate

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/*Some code*/

public void onOption6(MenuItem i)
{

    // YYY: Remove all items from the list
    startActivity(new Intent(this, RemoveAllActivity.class));

    tv.setText("Removing all items from the list.");

} // end onOption6

If you want to know what the singleton list class looks like:

public final class StringList extends LinkedList<String>
{

private static StringList instance = null; 

private StringList()
{
    // Exists only to defeat additional instantiations.
}

public static StringList getInstance()
{
      if(instance == null)
             instance = new StringList();

      return instance;
}

} // end StringList

Upvotes: 0

Views: 92

Answers (4)

Guillaume agis
Guillaume agis

Reputation: 3774

Dont bother yourself to do it programmatically !

Just use the "clear" method of your list.

All types of list in Java has this method.

ex : the_list.clear();

Upvotes: 0

T.Gounelle
T.Gounelle

Reputation: 6033

You remove by index in the list and re-evaluate the size at each step, which end up by not removing the second half of the list...

Since your list is a Linkedlist you can go with removing the head of the list until there is no more element:

while (the_list.isEmpty()) {
    the_list.remove();
}

Or even simpler, use the clear() method:

the_list.clear();

But if you really want to loop through all elements and use the size, you can start from the last element, but this may not be very efficient since you will traverse the list at each loop - accessing the last element by index:

for(int i = the_list.size() -1; i >= 0; i--) {
    the_list.remove(i);
}

Upvotes: 0

toadzky
toadzky

Reputation: 3846

Manually looping through the list is probably the wrong way to go. There is a reason that this method exists: LinkedList.clear. Stop trying to reinvent the wheel and just call the_list.clear().

Side Note: Java style conventions strongly discourage using underscores in variable names. It should be theList not the_list.

Upvotes: 2

Patrick Dorn
Patrick Dorn

Reputation: 768

You should use a Iterator for that (if you do not want to use clear()):

Something like (very rough):

while (iterator.hasNext()) {
         iterator.next();
         iterator.remove();
      }

For examples see: http://examples.javacodegeeks.com/core-java/util/arraylist/arraylist-listiterator-example/

Upvotes: 0

Related Questions