Hey guys I'm having a bit of touble assigning the toString to a string from an index of an array list, any help would be appreciated.

    public String indentedToString(int level)
        String answer = " ";
        answer = super.indentedToString(level);
        answer += "Value " + this.list.toString() + "\n";
        for(int i = 0; i < list.size(); i ++)
            answer += list[i].indentedToString(level + 1);
        return answer;

I keep getting an error in line 9 [array required, but java.util.ArrayList found]
any help ?

Thanks in advance

Recommended Answers

All 2 Replies

You can only use the notation for an array index on an array. When you do list[i], it is only allowed if list is an array, and list is a java.util.ArrayList according to your error message. Even with its suggestive name, java.util.ArrayList is not a kind of array.

Fortunately, ArrayList has the get method which does for lists what [i] does for arrays. Just call list.get(i) in place of list[i]. If this were C++ the [] operator would probably be overridden by ArrayList, but in Java that's not possible so we do the best we can with a get method. You will notice something similar when you try to use java.math.BigInteger.

Thanks bguild, this helped me out a lot, :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.