remove from array list
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;
publicclass BrewControl{
privatestatic ArrayList brew1 =new ArrayList();
privatestatic ArrayList brew2 =new ArrayList();
privatestatic BrewControl instance=null;
publicstatic 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
publicvoid brewSelection(int[] ingredientCodes){
System.out.println("Now calling brew selection");
if (ScenarioSteps.SCENARIO_1==ScenarioSteps.SEL_LARGE_COFFEE)
[b] brew2.remove("large","paper");[/b]
}//end brewSelection
}//end BrewControl class
[2778 byte] By [
pberardi1a] at [2007-11-27 5:50:41]

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();
}
}