0

I have a problem with ArrayList variables printing out to output. What am I doing wrong?

ArrayList al = new ArrayList();
        for (int i = 0; i < 10; i++) {
            al.add(new Calc(i, tan.ro, tan.stro));
        }
        for (int i = 1; i < 10; i++) {
            Object ol = al.get(i);
            Calc temp = (Calc) ol;
            System.out.println("Calculations = "+ temp.getMass());
            System.out.println(al.get(i));

My output:

Calculation = 5.0
App.Calc@15db9742
Calculation = 40.0
App.Calc@6d06d69c

I want to change that App.Calc@6d06d69c to correct value of i

mhasan
  • 3,703
  • 1
  • 18
  • 37
Satnam
  • 23
  • 1
  • 7
  • 4
    You need to override the `toString` method in class `Calc`. – Jesper Oct 11 '16 at 13:13
  • 4
    as a sidenote, make use of the generics for you `ArrayList al`. – SomeJavaGuy Oct 11 '16 at 13:13
  • @Jasper But if my `i` is int variable? – Satnam Oct 11 '16 at 13:28
  • It hasn’t got anything to do with `i`. @Jesper is correct. Please read the explanation in the link: [How do I print my Java object without getting “SomeType@2f92e0f4”?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4). Thx. – Ole V.V. Oct 11 '16 at 13:38
  • Guys, Thank you very much :) Its working! – Satnam Oct 11 '16 at 13:50

0 Answers0