KIM
KIM

Reputation: 47

Having more than one ArrayList

Hello everyone today am trying to do this in my ArrayList and i know it is possible but it is giving me an exception at main. Now am wondering how am i doing it wrong or what is the best way to do it. Am trying to have An ArrayList and inside the ArrayList I have another one and i give it a variable.

import java.util.ArrayList;
import java.util.Collection;

public class Example3 {
public static void main(String[] args) {
    ArrayList<ArrayList<Family>> smallFamily = new ArrayList<ArrayList<Family>>();
    smallFamily.addAll((Collection<? extends ArrayList<Family>>) (new Family("John",89 )));

    smallFamily.addAll((Collection<? extends ArrayList<Family>>) new Family ("Smith", 78)));

    for(ArrayList<Family> s: smallFamily){
        System.out.println(s);
    }

}

Now bellow is my Family Class and in my family this are the values

public class Family {

    public String Name;
    public int weight;

    public Family(String Name, int weight){
        this.Name = Name;
        this.weight = weight;
    }
    public String toString(){
        return ("The name is " + this.Name + "The weight is: " + this.weight);
    }
}}

The exception being thrown when i compile and run my programme is

Exception in thread "main" java.lang.ClassCastException: Examples.Family cannot be cast to java.util.Collection
    at Examples.Example3.main(Example3.java:9)

Now am learning Java on my own and don't have anyone i can ask. Any kind of help will be appreciated.

Upvotes: 1

Views: 242

Answers (2)

Madona wambua
Madona wambua

Reputation: 1428

Now Why you get this line here

smallFamily.addAll((Collection<? extends ArrayList<Family>>) (new Family("John",89 )));

simply it because the computer couldnt understand what you were doing so it casted for you. Or you casted :). Now trying to convert an object into a collection does not work.

Rather you need to try this

public class Example3 {
public static void main(String[] args) {
    ArrayList<ArrayList<Family>> smallFamily = new ArrayList<ArrayList<Family>>();
    smallFamily.add(new ArrayList<Family>(2222) {{add(new Family("smith ", 0));}});

    smallFamily.add(new ArrayList<Family>(333) {{add(new Family("john ", 0));}});

    for(ArrayList<Family> s: smallFamily){
        System.out.println(s);
    }

}
}

when you add your Family Class

public class Family {

    public String Name;
    public int weight;

    public Family(String Name, int weight){
        this.Name = Name;
        this.weight = weight;
    }
    public String toString(){
        return ("The name is " + this.Name + "The weight is: " + this.weight);
    }
}

The output should be

[The name is smith The weight is: 0]
[The name is john The weight is: 0]

Hope thats what you anticipated.

Upvotes: 0

hyperneutrino
hyperneutrino

Reputation: 5425

You are trying to convert an object into a collection. This does not work. Rather, you need to do this:

smallFamily.add(new ArrayList<Family>(1) {{add(new Family("", 0));}});

This will create an ArrayList which adds new Family("", 0) when it is created. Then, it will add this arraylist to the smallFamily arraylist.

Upvotes: 2

Related Questions