简体   繁体   中英

ArrayList - Remove the first added object and no repeats

I'm doing an assignment where I have to add a Present to an ArrayList of presents. There can be a maximum of 10 presents in the ArrayList and the present is defined by its type, cost and name, and if these three fields are the same, then it's the same present and it cannot be added to the ArrayList.

When an eleventh present is added, the first present added to the list has to be removed and this new present take its place.

And I'm really stuck.

This is my code so far:

public class GiftList 

{

       private ArrayList<Present> presents;

       public GiftList() {
              presents = new ArrayList<Present>();
       }

       public void addPresent(Present present) {
       if (presents.size() <= 10) {
              presents.add(present);
       }
       else {
              //**Remove oldest present in list, then
              presents.add(present);
       }
       //**Another if statement for no duplicated presents
}

public class Present
{
    private String name;
    private String type;
    private double cost;

    public Present(String name, String type, double cost) {
        this.name = name;
        this.type = type;
        this.cost = cost;
    }
}

Hints:

If you add an equals method to your Present object:

public class Present {
    private double cost;
    private String name;
    private String type;

    public Present(String name, String type, double cost) {
        this.name = name;
        this.type = type;
        this.cost = cost;
    }

    public boolean equals(Object o) {
        if (o instanceof Present) {
            Present p2 = (Present) o;
            return name.equals(p2.name) && type.equals(p2.type) && Double.valueOf(cost).equals(p2.cost);
        }
        return false;
    }

    public int hashCode() {
        return Objects.hash(name, type, cost);
    }
}

Then the following code could be used:

List<Present> myList = new ArrayList<Present>() {
    @Override
    public boolean add(final Present present) {
        if (contains(present)) {
            return false;
        }
        if (size() >= 10) {
            remove(0);
        }
        return super.add(present);
    }
};

myList.add(new Present("name", "type", 1.0));

For more info, see Object.equals and List.remove .

However, I believe the best way to do this would be to either use a Set which automatically handles the no repeats -problem or some kind of Queue . If some kind of hash-solution is used (eg a HashSet ) don't forget to implement the hashCode function (more info on hashCode here ).

Edit: added the hashCode -implementation after input from @ TJamesBoone.

you can just remove the first element by calling

presents.remove(0);

check this http://www.tutorialspoint.com/java/util/arraylist_remove.htm

For duplication it would be better if you used a Set

But, if you stick with ArrayList, then implement equals/hashcode as already mentioned and now there are two options:

  1. Either always call remove (for the new object) before adding it, so it always removes oldest entries

  2. or check existence before adding.

Although 2nd option might be more efficient it depends on how you want to utilize your queue (how FIFO is defined for duplicated objects?). If a new object already exists in the list, do you want to update its index, as it was a new entry? if yes you update its status and it will be deleted at a later time than keeping the older one.

Looking at your description I think this could do your job

int rotatingIndex =- 1 ; //variable maintaining current value of the index.-1 when no elements are         present
public void addPresent(Present present) {
    rotatingIndex = (rotatingIndex + 1)%10;
    presents.add(rotatingIndex,present);
}

I would suggest to override equals for Present, and use linkedlist as queue. like;

public class Present
{
    private String name;
    private String type;
    private Double cost;


    public Present(String name, String type, Double cost) {
        this.name = name;
        this.type = type;
        this.cost = cost;
    }



    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }



    public String getType() {
        return type;
    }



    public void setType(String type) {
        this.type = type;
    }



    public Double getCost() {
        return cost;
    }



    public void setCost(Double cost) {
        this.cost = cost;
    }



    @Override
    public boolean equals(Object obj){
        if(obj == null || !(obj instanceof Present)){
        return false;
        }

        Present anotherPresent = (Present) obj;
        if(this.getName() == anotherPresent.getName() 
                && this.getCost() == anotherPresent.getCost() 
                && this.getType() == anotherPresent.getType()){
            return true;
        }
        return false;
    }

}

Gift class:

public class GiftList 

{

       private LinkedList<Present> presents = new LinkedList() ;

       public void addPresent(Present present) {
       if (presents.contains(present)){
           // do nothing it already exists!!
           return ;
       }
       if(presents.size() <= 10) {
              presents.add(present);
       }
       else {
            presents.pop();
              //**Remove oldest present in list, then
              presents.add(present);
       }
       //**Another if statement for no duplicated presents
}
}

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