My code now prints the values of the objects, but I'm getting another error. If I un-comment line 15, it verifies that the constructor is being run 10 times. My output is:
0
5
10
15
20
25
30
35
40
45
So I have 10 objects in the arrayList. I don't understand how I am not meeting condition 3.
package com.codegym.task.task06.task0614;
import java.util.ArrayList;
/*
Static cats
*/
public class Cat {
//write your code here
int x;
public static ArrayList<Cat> cats = new ArrayList<Cat>();
public Cat(int x) {
//System.out.print("Constructor: x=" + x + " "); // verify that the constructor is being run
this.x = x;
}
@Override
public String toString() { // print the value of the object
return Integer.toString(x);
}
public static void main(String[] args) {
//write your code here
for(int i = 0; i<10; i++) {
Cat cat = new Cat(i*5);
//System.out.println("cat(" + i + ")=" + cat.x);
//Cat.cats.add(new Cat(i));
Cat.cats.add(cat);
}
//System.out.println(Cat.cats.size());
printCats();
}
public static void printCats() {
//write your code here
for(int i = 0; i<cats.size(); i++) {
//for(Cat cat: cats) {
System.out.println(cats.get(i));
}
}
}