I'm coding a simple fun project in Java, creating a loop which creates an object in an arraylist with the name Random(1-60) (ex. Dog2, Dog6) 59 times.
And this loop, will check if an object it just made has a same name as the other object in the same arraylist, and delete the object it just made.
Basically, it will delete an object with a clone name.
But I don't know how.
I've tried this but... It doesn't work.
public class main {
public static void main(String[] args) {
ArrayList<Animal> Pets = new ArrayList<Animal>();
Random rand = new Random();
int ha = rand.nextInt(59) 1;
for (int i = 0; i <= 59; i ) {
Pets.add(new Dog("Dog" ha, 4));
//Clone name object detector,
if (Pets.size() > 0 1) {
if (Pets.get(i).name == Pets.get(i-1).name) {
Pets.remove(i);
} // and deleter
}
Pets.get(i).makeSound();
ha = rand.nextInt(59) 1;
}
}
}
An object with a clone name still exists. I know that wouldn't work, because i-1
is pretty small.
Dog4 barks.
Dog16 barks.
Dog11 barks.
Dog37 barks.
Dog25 barks.
Dog35 barks.
Dog40 barks.
Dog30 barks.
Dog14 barks.
Dog44 barks.
Dog4 barks.
Dog21 barks.
Dog37 barks.
Dog51 barks.
Dog20 barks.
Dog30 barks.
Dog32 barks.
Dog25 barks.
Dog26 barks.
Dog41 barks.
Dog20 barks.
Dog37 barks.
Dog23 barks.
Dog48 barks.
Dog49 barks.
Dog8 barks.
Dog7 barks.
Dog40 barks. --
Dog34 barks.
Dog29 barks.
Dog8 barks.
Dog52 barks.
Dog9 barks.
Dog27 barks.
Dog31 barks.
Dog46 barks.
Dog19 barks.
Dog46 barks.
Dog25 barks.
Dog33 barks.
Dog38 barks.
Dog9 barks.
Dog40 barks.--
Dog56 barks.
Dog18 barks.
Dog51 barks.
Dog17 barks.
Dog22 barks.
Dog6 barks.
Dog40 barks. --
Dog47 barks.
Dog23 barks.
Dog10 barks.
Dog23 barks.
Dog20 barks.
Dog13 barks.
Dog33 barks.
Dog35 barks.
Dog18 barks.
Dog17 barks.
So i need help, how can i do this?
CodePudding user response:
You can create temp object for newly created Dog before adding it into your List. Then you check if animal with that name already exists if it doesn't you add it to your collection and make him make sound. If it already exist you can create another OR (as i did in given example) just pass on adding that animal to your list.
Also it is risky to use your iterator i if you remove/skip adding elements as it will get bigger that max index in your list and you can get IndexOutOfBoundsException
public class Main {
public static void main(String[] args) {
List<Animal> pets = new ArrayList<>();
Random rand = new Random();
int ha = rand.nextInt(59) 1;
for (int i = 0; i <= 59; i ) {
//we create new temp object with new name
Animal tempAnimal = new Dog("Dog " ha);
//we can if object with that name already exists in our list
if (!pets.contains(tempAnimal)) {
pets.add(tempAnimal);
pets.get(pets.size()-1).makeSound();
}
ha = rand.nextInt(59) 1;
}
}
}
CodePudding user response:
There are a few issues:
- Compare strings with
.equals()
rand.nextInt(59) 1;
returns a number in the range[1-59]
- When you don't add,
i
is still incremented so less than 60 items are added
You could use a while
loop:
ArrayList<Animal> Pets = new ArrayList<Animal>();
Random rand = new Random();
while (Pets.size() < 60) {
int ha = rand.nextInt(60) 1;
Dog d = new Dog("Dog" ha, 4);
if (!Pets.contains(d)) {
Pets.add(d);
d.makeSound();
}
}
Another option is to make a sequential list, then shuffle that:
for (int i = 1; i <= 60; i ) {
Pets.add(new Dog("Dog" i, 4));
}
Collections.shuffle(Pets);
for (Animal a : Pets) {
a.makeSound();
}
If you use contains()
(like in the first example), don't forget to override equals()
in the Animal
class:
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
return name.equals(((Animal)obj).name);
}