1

I have an array list of array lists. And I am trying to iterate through them. However I keep getting compilation error. Where am I going wrong. Is there a better way to iterate.

Code:

import java.util.ArrayList;

public class ListofLists {
    public static ArrayList family() {
        // ArrayList of ArrayLists
        ArrayList<ArrayList<String>> couples = new ArrayList<ArrayList<String>>();
        ArrayList<String> husbands = new ArrayList<String>();
        husbands.add("brad");
        husbands.add("jessie");
        couples.add(husbands);
        ArrayList<String> wives = new ArrayList<String>();
        wives.add("jolie");
        wives.add("jena");
        couples.add(wives);
        return couples;
    }


    public static void main(String[] args) {
        ArrayList couples = family();

        for (Object couple : couples) {
            for (String person: couple) {
                System.out.println(person);
            }
        }
    }
}

Compilation Error:

required: array or java.lang.Iterable
  found:    Object

Expected Output:

brad
jessie
jolie
jena
2
  • Although the error is obvious from inspection, it is generally required that you tell us the actual compiler error you encountered. It might not be so obvious in all cases. Commented Jan 26, 2014 at 2:06
  • Avoid using raw types. Commented Jan 26, 2014 at 2:13

2 Answers 2

4

WHat you need is the following:

public static void main(String[] args) {
    ArrayList<ArrayList<String>> couples = family();

    for (ArrayList<String> couple : couples) {
        for (String person : couple) {
            System.out.println(person);
        }
    }
}

Basically, you were storing the results from your call to familiy() in an ArrayList of unknown type. It was automatically getting boxed to Object, and for-each doesn't work for Objects.

Sign up to request clarification or add additional context in comments.

Comments

2

iterator is a feature of collection,class Object itself does not provide such feature. Look at my code.It's work well.

package com.test;

import java.util.ArrayList;
import java.util.List;

public class ListOfLists {
public static List<List<String>> family() {
    // ArrayList of ArrayLists
    List<List<String>> couples = new ArrayList<List<String>>();
    List<String> husbands = new ArrayList<String>();
    husbands.add("brad");
    husbands.add("jessie");
    couples.add(husbands);
    List<String> wives = new ArrayList<String>();
    wives.add("jolie");
    wives.add("jena");
    couples.add(wives);
    return couples;
}

public static void main(String[] args) {
    List<List<String>> couples = family();

    for (List<String> couple : couples) {
        for (String person : couple) {
            System.out.println(person);
        }
    }
}

}

4 Comments

This is not a useful answer. Anyone can just post code. The point is to explain it.
Ok,I just check java doc,iterator is a feature of collection,class Object itself does not provide such feature.
what is the advantage of using List<List<String>> over ArrayList<ArrayList<String>> ??
In current code,there is no difference between two ways.Generally speaking,I prefer to List<String>.You can get more details from this link type-list-vs-type-arraylist-in-java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.