简体   繁体   中英

How to create my own collection?

So lets say I have a class BaseballCard that creates a baseball card. Now I need to make another class which would be my collection class.

For example I would call it BaseballCardCollection

and then I want to create methods like

size (which returns the numbers of cards in the collection)

addCard(adds a baseball object to the collection object)

removeCard (removes a baseball card)

and so on

What would be the best way to do this. I tried doing this

public CardCollectionList() {
    BaseballCard[] baseballCardList = new BaseballCard[101];
 }

So each object is insinuated with an array of type BaseballCard of size 100.

And then for example the size method I tried something like this

  public int size(){
    int size = 0;
    for(int i = 1; i<this.baseballCardList.length; i++)
        if (baseballCardList!= null)
            size+=1;

}

But it doesn't work because "baseballCardList cannot be resolved to a variable"

You could try using ArrayLists - http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html :

ArrayList<baseballCard> baseballCardList = new ArrayList<baseballCard>(0);

public boolean addCard(baseballCard card){
    return baseballCardList.add(card);
}

public boolean removeCard(int card){
    return baseballCardList.remove(card);
}

public baseballCard getCard(int card){
    return baseballCardList.get(card);
}

public int sizeBaseballCardList(){
    return baseballCardList.size();
}

public ArrayList<baseballCard> getBaseballCardList(){
    return baseballCardList;
}

Move the variable BaseballCard[] baseballCardList outside the constructor, make it a field in your class. Do similar with size .

This is how the class should look like:

public class CardCollectionList {
    //fields
    private BaseballCard[] baseballCardList;
    private int size;

    //constructor
    public CardCollectionList() {
         baseballCardList = new BaseballCard[101];
    }
    //method
    public int getSize() {
        return this.size;
    }
}

You could try creating your own class implementing the Collection interface and define your own methods + implement Collection methods:

public class myContainer implements Collection <BaseballCard> {

}

You need to move the variable declaration from the constructor to the class, so you can access it in other methods, too.

class CardCollectionList {
  BaseballCard[] baseballCardList;

  public CardCollectionList() {
    baseballCardList = new BaseballCard[101];
  }

  public int size(){
    int size = 0;
    for(int i = 1; i<this.baseballCardList.length; i++) {
      if (baseballCardList[i] != null) {
        size+=1;
      }
    }
    return size;
  }
}

The code is as close to your fragment as possible. There are several ways to improve this (keep track of the size when adding, automatic array reallocation etc.). But it is a start if you want to try this yourself.

Normally, you'd probably just use ArrayList<BaseballCard> .

Now I need to make another class which would be my collection class. ... What would be the best way to do this.

I don't have enough reputation to comment on your question, so I am going to assume that you just want to store BaseballCard objects in a Java Collection . The Java SDK offers a lot of options. Since you are asking about the "best" way to go then I would use one of those unless you need to add additional functionality .

if you don't find what you need from the Java SDK or just want to create your own Collection then follow the advice given by @michał-szydłowski above

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