简体   繁体   中英

Removing objects from an arraylist in java

I'm having issues removing class objects from an arraylist in a class.

package more_java;

import java.util.ArrayList;

public class PiggyBank {
    private static ArrayList<Coin> coins = new ArrayList<Coin>();
    
    public PiggyBank(ArrayList<Coin> coins) {
        PiggyBank.coins = coins;
    }
    
    public static void addNickel(){coins.add(new Nickel());}
    public static void addDime(){coins.add(new Dime());}
    public static void addQuarter(){coins.add(new Quarter());}
    public static void addLoonie(){coins.add(new Loonie());}
    public static void addToonie(){coins.add(new Toonie());}
    
    public static void removeNickel() {coins.remove(Nickel());}
    public static void removeDime(){coins.remove(Dime());}
}

Nickel, Dime, Quarter, Loonie, and Toonie are all class objects made as extensions of an abstract class Coin. Each of these coins contain nothing but an accessor method GetValue() which simply returns the value of each coin. The problem comes from the remove methods where I get the error: The method Nickel() is undefined for the type PiggyBank . Any help would be much appreciated.

Normally when you want to remove an object from a List you need to override equals and hashCode in the object's class declaration. Otherwise, the List won't know how to compare existing instances of the contents.

But in this case, nickels, dimes, etc don't change their value (no pun intended). So there is no requirement to override those methods if you always use the same singleton instance to add or remove values. Here is how it might be done. Only nickels and dimes are shown.

public class PiggyBank {
    
    private static ArrayList<Coin> coins = new ArrayList<Coin>();
    final static Nickel NICKEL = new Nickel();
    final static Dime DIME = new Dime();
    
    public static void main(String[] args) {
        addNickel();
        addNickel();
        addDime();
        addDime();
        System.out.println(coins);
        removeDime();
        System.out.println(coins);
        removeNickel();
        System.out.println(coins);
        coins.add(new Quarter());
        coins.remove(new Quarter());  // won't remove quarter as it is
                                      // a different instance.
        System.out.println(coins);
    }
    
    public static void addNickel() {
        coins.add(NICKEL);
    }
    
    public static void addDime() {
        coins.add(DIME);
    }
    
    public static void removeDime() {
        coins.remove(DIME);
    }
    
    public static void removeNickel() {
        coins.remove(NICKEL);
    }
    
}

abstract class Coin {
    public String toString() {
        return getClass().getSimpleName();
    }
}

class Nickel extends Coin {
}

class Dime extends Coin {
}
class Quarter extends Coin {}

So there is no need to keep creating instances of the various monetary values. If you try and remove new instances of them, they won't be found. Perhaps a better way would be to use an enum .

enum Coins { NICKEL, DIME, QUARTER}

You can check them out in more detail at The Java Tutorials .

try this instead,you can use instanceof key in remove method:

for(Coin coin: coins){ if(coin instanceof Nickel){ coins.remove(coin); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM