0

Following are some snippets from a jsp page:

<%! ArrayList songList = new ArrayList<String>(); %>

    <%
        songList = StoreSongLink.linkList;
        // linkList is a static variable in the class StoreSongLink
        // There the linkList is defined as public static ArrayList<String> linkList = new ArrayList<String>();
    %>

<%}  else {
       for (ArrayList<String> list : songList){}

%>

The code inside the else srciplet produces an error required java.util.ArrayList<String> found java.lang.Object. Why is this ? I do not understand the reason for this.

Why does the compiler say songList to be of type Object ?

1
  • Should it not be for (String l: songList) and ArrayList<String> songList = ... ?? Commented Aug 16, 2012 at 13:08

7 Answers 7

3

You should declare it explicitly at the start:

ArrayList<String> songList = new ArrayList<String>();

If you declare it like this:

ArrayList songList = new ArrayList<String>();

Then you are saying songList is an ArrayList of Objects, regardless of what is to the right of the =. Assignment doesn't change this, so this:

ArrayList songList = new ArrayList<String>();
songList = StoreSongLink.linkList;

does not change the type of songList, it's still effectively ArrayList<Object>.

If you fix the declaration, then your for loop should look like:

for (String list : songList){}

Because songList is array list of Strings. As you have it, Java extracts each Object from songList and tries to assign it to what you have declared to the left of :, which you have told it is an ArrayList<String>. It cannot convert the Object to ArrayList<String> (especially since the Object is really just a String underneath), so it throws an error.

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

3 Comments

why shall i declare it above ? This call songList = storeSongLink.linkList; fetches the same
Why does the compiler say songList to be of type Object ?
@SuhailGupta Edited answer for clarity. Does it make sense?
2
for (ArrayList<String> list : songList){} // Error is here

And you have define a reference of ArrayList of Object type and declared a String type ArrayList instance which should be ArrayList<String> songList = new ArrayList<String>(); or ArrayList<String> songList = new ArrayList(); (supports only java 7)

So the Corrected code could be like -

<%! List<String> songList = new ArrayList<String>(); %>
...
for (String list : songList){
}

3 Comments

but this call songList = StoreSongLink.linkList; does the same
why does it say songList to be of type object ?
@SuhailGupta - while creating ArrayList<E> if do not defined type(E) it has been granted as a Object(SuperType). And your defined reference ArrayList<E> can hold that type of or sub type of that type of objects. And it is always good approach to define type of list.
0

Use:

for (String s: songList) {
  // etc.
}

Also ensure that StoreSongLink.linkList is of type ArrayList<String>.

Comments

0
for (ArrayList<String> list : songList){}

must be

for (Object list : songList){}

Because songList declared type is ArrayList(equal ArrayList)

Comments

0

While you assign songList a new ArrayList, you're defining the songList as an arrayList (which is arrayList). That would make it okay to iterate over songList as Objects. But you're actually iterating over songList as ArrayList.

So in short, you need to switch songList to be decared to ArrayList and you need to iterate over songList with Strings, not ArrayList.

Comments

0

ArrayList is, as you probably know, a raw type, when not using parametrized ArrayList<E> declaration. So you effectively turned it to array list of Object instead of String, with your initialization:
ArrayList songList = new ArrayList<String>();

Comments

0

Your iteration is wrong in

for (ArrayList<String> list : songList){}

songList contains the list of String elements not the list of ArrayList<String>

so you need to iterate this way for (Object song: songList){}

or

ArrayList<String> songList = new ArrayList<String>();
for (String song: songList){}

Comments

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.