• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

cannot convert from element type object to ArrayList

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have the following code:

private void resultsOut(ArrayList results) {  //results have the elements [ [1, 1, 1], [2, 4, 8], [3, 9, 27], [4, 16, 64], [5, 25, 125], [6, 36, 216], [7, 49, 343] ]
for(ArrayList arr : results) //dose not compile on results: Type mismatch: cannot convert from element type object to ArrayList
                                             //dose not compile on ArrayList: ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized
System.out.println(a);
}

I don't undersatand why. Can someone help me? Thank you in advance.

 
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the contents of the variable: results that is passed to the method?
 
Saloon Keeper
Posts: 10428
223
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

When posting code, please put code inside code tags, like this: [code]myCode[/code].

The reason for this is that you're using a raw type: A raw type is a generic type that hasn't been parameterized. ArrayList is a generic type, so you really should parameterize it with the type of elements that you expect to be inside the list. It looks like your list consists of other lists, which in turn consist of integers. That means that a solution to your problem would be to declare your method parameter to be an ArrayList<ArrayList<Integer>>, and your loop variable as an ArrayList<Integer>.

That's not the end of the story though. Never declare variables as a more specific type than what you need for the method to do its job. Do you really care that the input is an ArrayList specifically, or would other collections work as well? If I had a variable of type Queue<Set<String>>, it seems your method implementation would work for that as well, if only the method parameter had the correct type.

You'll notice that I've changed the type to Iterable, because the only thing you need to do inside the method is iterate over it's elements, and I've parameterized it with the unbounded wildcard <?>. That's because we don't care what the element type of the iterable is, because EVERY object can be printed using the println() method.
 
Wei Huang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan van Hulst,

i have got your idea and it works. Thank you very much.

Best regards.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!