This discussion is archived
6 Replies Latest reply: May 29, 2007 6:08 PM by 807600 RSS

remove from array list

807600 Newbie
Currently Being Moderated
The error i get is in the line in bold. How do i remove one "large", "paper" from the arraylist brew2. The line I currently have gives the error cannot find method remove. Suggestions?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BrewControl {
    
        private static ArrayList brew1 = new ArrayList();
    private static ArrayList brew2 = new ArrayList();
    
   
    private static BrewControl instance=null;
    
  
    public static BrewControl getInstance(){ 
    if(instance == null)
    instance = new BrewControl();
    
    return instance;
    
    
}
    public BrewControl() {
       ArrayList brew1 = new ArrayList();
       ArrayList brew2 = new ArrayList();
       for(int i=0;i<5;i++){
           brew1.add(new Cup("small","plastic"));
           brew2.add(new Cup("large","paper")); 
       }//end for loop
       
    }//end instance of BrewControl                     
    public void brewSelection(int[] ingredientCodes) {
    
    
        System.out.println("Now calling brew selection");
    if (ScenarioSteps.SCENARIO_1==ScenarioSteps.SEL_LARGE_COFFEE)
          brew2.remove("large","paper");      
    }//end brewSelection
}//end BrewControl class
 
 
  • 1. Re: remove from array list
    807600 Newbie
    Currently Being Moderated
    Have a look at the API:
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

    (Hint: there is no overloaded remove method that takes two strings as parameters.)
    You can either remove(int index) or remove(Object o). Also, if you'd like to remove several items in a range, try removeRange(int start, int end). The JavaDoc is much more detailed.
  • 2. Re: remove from array list
    807600 Newbie
    Currently Being Moderated
    Something tells me that you want to remove the cup object that is defined by having "large" and "paper" strings in the constructor.

    For an arraylist to remove object o (i.e., myArrayList.remove(o), then the list must have an object e such that e.equals(o). You might need to play with your cup class's equals method. You also need to pass an object (or numeric index) to the remove method, not strings.
  • 3. Re: remove from array list
    807600 Newbie
    Currently Being Moderated
    Are you talking about an iterator perhaps?
  • 4. Re: remove from array list
    807600 Newbie
    Currently Being Moderated
    No, probably something more along the lines of:
    MyArrayList.remove(myArrayList.indexOf("MyString"));
    MyArrayList.remove("MyString"); // also suitable
    But, you may find a better method of doing it if you would [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html]RTFM.
  • 5. Re: remove from array list
    807600 Newbie
    Currently Being Moderated
    Here's a small example of what I mean by overriding a class's equals method.

    Consider two classes Foo1 and Foo2, both hold an int, both identical in every way except that Foo2 overrides equals such that a Foo2 object is equal to any other Foo2 object that holds the same int value:
    /**
     * Foo1, A simple class that holds a single int field.
     * @author Pete
     */
    class Foo1 
    {
        private int number;
        
        public Foo1(int n)
        {
            this.number = n;
        }
        
        public int getNumber()
        {
            return number;
        }
    }
    Our second class that overrides equals:
    /**
     * Foo2, An identical class in every way except that this one
     * overrides the equals method
     * @author Pete
     */
    class Foo2 
    {
        private int number;
        
        public Foo2(int n)
        {
            this.number = n;
        }
        
        public int getNumber()
        {
            return number;
        }
        
        @Override
        public boolean equals(Object fu)
        {
            // if the exact same object, they are equal
            if (this == fu)
            {
                return true;
            }
            
            // if not the same class, they are not equal
            if (fu == null || fu.getClass() != this.getClass())
            {
                return false;
            }
            
            // here's the important stuff:  if int field values == 
            // then objects are equal
            return ((Foo2)fu).number == this.number;
        }
    }
    Now if we test these classes out in simple arraylists, here's what we get:
    import java.util.ArrayList;
    import java.util.List;
    
    class TestFoo
    {
        private static List foo1List = new ArrayList();
        private static List foo2List = new ArrayList();
        
        public static void main(String[] args)
        {
            // add 4 Foo1 and Foo2 objects into respective 
            // ArrayLists, each containing 0, 1, 2, 3
            for (int i = 0; i < 4; i++)
            {
                foo1List.add(new Foo1(i));
                foo2List.add(new Foo2(i));
            }
            
            // show the list
            System.out.println("foo1List originally contains: ");
            for (int i = 0; i < foo1List.size(); i++)
            {
                System.out.println(((Foo1)foo1List.get(i)).getNumber());
            }
            System.out.println();
    
            // show the list
            System.out.println("foo2List originally contains: ");
            for (int i = 0; i < foo2List.size(); i++)
            {
                System.out.println(((Foo2)foo2List.get(i)).getNumber());
            }
            System.out.println();
            
            // now try to remove from its respective arraylist an object 
            // that does not have the equal method overridden
            boolean canRemove = foo1List.remove(new Foo1(2));
            
            System.out.println("Can you remove new Foo1(2) from foo1List? " + canRemove);
    
            // show the list
            System.out.println("foo1List now contains: ");
            for (int i = 0; i < foo1List.size(); i++)
            {
                System.out.println(((Foo1)foo1List.get(i)).getNumber());
            }
            System.out.println();
    
            // now try to remove from its respective arraylist an object 
            // that does have the equal method overridden
            canRemove = foo2List.remove(new Foo2(2));
            
            System.out.println("Can you remove new Foo2(2) from foo2List? " + canRemove);
            
            // show the list
            System.out.println("foo2List now contains: ");
            for (int i = 0; i < foo2List.size(); i++)
            {
                System.out.println(((Foo2)foo2List.get(i)).getNumber());
            }
            System.out.println();
        }
    }
  • 6. Re: remove from array list
    807600 Newbie
    Currently Being Moderated
    You will need to loop over you ArrayList and when you find the object you can delete it. Use a normal for loop to remove using an index or a for each loop to remove using an object.

    However, why are you bothering with ArrayLists at all? You have two ArrayLists each with one item in them Seems a waste of effort.