This is not exactly an "error" -- it's just a warning. Your code will still compile and run.
As Brandt explained above, the warning is because you have not specified a generic type for your List. The danger is that there are no checks on what the list contains, so you could add any type of object to your list. For example, you could mix Strings with ArrayLists!
This is a problem, because if you are accessing items from this List and expecting it to contain Strings, then you will have a serious problem if you try to treat an ArrayList as a String!
With generics, if you specify a type for your list, like String...
...then attempting to add anything other than a String will result in a compilation error.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Apr 04, 2007
with reference to my above program if i add this line will it show the contents of arraylist.
1> i know we have to iterate through the collection. but in one book it is give n like the below line
When using println() in this case you are invoking the toString() method of an ArrayList. toString() tells an ArrayList to display its contents in the String output you have seen. If we were to check out the source code for the method, I expect there is an Iterator in there somewhere or maybe an enhanced for loop.
Like Marc said, this is only allowing us to view the contents, you are unable to manipulate them at this point. If you wanted to change the output when invoking toString() you could create, for example, MyArrayList, extending ArrayList, and override the toString() method. But that's only if you want to be extra fancy
Also note that the String representing the List is assembled by called toString() on each of the List's elements. Since you have Strings as elements, these display nicely as "[C,a,g,h]" (because calling toString() on a String returns the represented String). Note what happens using simple Objects (i.e., with Object's implementation of toString())...