0

I have a Dog class; Created 2 dogs with properties; now I want to create an arrayList that will contain the info of both dogs (and their parameters) and print out one of the values, how should I print a certain value of the dog parameters? for example: furLength of d1

public Dog(String nickName, int furLength, String favToy) {
        this.nickName = nickName;
        this.furLength = furLength;
        this.favToy = favToy;
    }
   



   public class Main {


    public static void main(String[] args) {
        Dog d1 = new Dog("Archy", 5, "kong");
        Dog d2 = new Dog("Arnavushik", 3, "socks");
        System.out.println(d1.getFavToy());
        ArrayList DogList = new ArrayList();
        DogList.add(d1);
        DogList.add(d2);
        System.out.println(DogList.get(1));

        }
    }
user3581800
  • 317
  • 2
  • 5
  • 16
  • You need to override `toString` method in the class, `Dog`. – Arvind Kumar Avinash Aug 01 '20 at 12:10
  • "*... the output is gibberish*" - [The default `toString`-behaviour prints the object-type, followed by an `@`, followed by the object's `hashCode` as hexstring](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Object.html#toString()) – Turing85 Aug 01 '20 at 12:10

1 Answers1

0

@override default toString() method in the Dog class

public class Main {
    public static void main(String[] args) {
        Dog d1 = new Dog("Archy", 5, "kong");
        Dog d2 = new Dog("Arnavushik", 3, "socks");
        System.out.println(d1.getFavToy());
        ArrayList Dogs = new ArrayList();
        Dogs.add(d1);
        Dogs.add(d2);
        dogs.stream().forEach(System.out::println);
    }
}
Thirumal
  • 8,280
  • 11
  • 53
  • 103