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.

    @Override
    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

Edited 3 Years Ago by fonzi

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.

Edited 3 Years Ago by bguild

This question has already been answered. Start a new discussion instead.