Salut ![[[sticker:p/1kkr]]](https://image.jeuxvideo.com/stickers/p/st/1kkr)
Je cherche à ajouter dans mon hashset, un objet en l'occurrence de type "penny" ?
Pour cela j'ai taper dans mon constructeur ces deux lignes :
HashSet hashSetPenny = new HashSet();
hashSetPenny.add(new Penny());
qui devraient initialiser un objet penny à mon hashset
Je rencontre aussi une erreur dans ma méthode pennyCount qui me dit qu'il n'y a pas d'objet initialisé (ce que j'ai initialisé dans mon constructeur)...
Merci ![[[sticker:p/1ljp]]](https://image.jeuxvideo.com/stickers/p/st/1ljp)
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
public class Pocket
{
int sizeOfPocket;
private HashSet<Penny> hashSetPenny;
public Pocket(int sizeOfPocket)
{
for(int counter = 0; counter < sizeOfPocket ; counter++ )
{
this.sizeOfPocket = sizeOfPocket;
HashSet hashSetPenny = new HashSet();
hashSetPenny.add(new Penny());
}
}
public int pennyCount()
{
return hashSetPenny.size();
}
public Penny removePenny(Penny penny)
{
Iterator<Penny> it = new Iterator<Penny>();
Iterator <Penny> it = hashSetPenny.iterator();
if (it.hasNext())
{
hashSetPenny.remove(penny);
}
else
{
return null;
}
return penny;
}
}