How to create my own collection?
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 But it doesn't work because "baseballCardList cannot be resolved to a variable"
Answer by Luiggi Mendoza for How to create my own collection?
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; } } Answer by Micha Szydowski for How to create my own collection?
You could try creating your own class implementing the Collection interface and define your own methods + implement Collection methods:
public class myContainer implements Collection { } Answer by Matt Winstanley for How to create my own collection?
You could try using ArrayLists - http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html:
ArrayList baseballCardList = new ArrayList(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 getBaseballCardList(){ return baseballCardList; } Answer by Stefan Haustein for How to create my own collection?
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 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.
Answer by Mike Barlotta for How to create my own collection?
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-szydowski above
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:
Post a Comment