Hi! When I print the objects they are printed in an "ugly" way, with the hashcode (Cat@38c6f217). How can I solve this without the need of using a toString() method?
And I've discovered in this task that when creating many objects, they don't necessarily need to have a different name (ie. a different reference variable). Correct, right?
Thank you for your answers!
package com.codegym.task.task06.task0614;
import java.util.ArrayList;
/*
Static cats
*/
public class Cat {
public static ArrayList<Cat> cats = new ArrayList<Cat>();
public Cat() {
}
public static void main(String[] args) {
Cat cat1 = new Cat();
Cat cat2 = new Cat();
Cat cat3 = new Cat();
Cat cat4 = new Cat();
Cat cat5 = new Cat();
Cat cat6 = new Cat();
Cat cat7 = new Cat();
Cat cat8 = new Cat();
Cat cat9 = new Cat();
Cat cat10 = new Cat();
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
cats.add(cat4);
cats.add(cat5);
cats.add(cat6);
cats.add(cat7);
cats.add(cat8);
cats.add(cat9);
cats.add(cat10);
printCats();
}
public static void printCats() {
//for(Cat whatever : cats) {
// System.out.print(whatever);
for(int i = 0; i<cats.size(); i++){
System.out.print(cats.get(i));
}
}
}