This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList confusion

 
Phil Dixon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone shed some light on the ArrayList below.





I thought this would print

This is ClassA
This is ClassA
This is ClassA


Error
ClassB.java:11: incompatible types
found : ClassA
required: java.lang.String
for (String inList : myList) {
 
Phil Dixon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changed this line to



And now get

ClassA@82ba41
ClassA@923e30
ClassA@130c19b
 
Julien Grenier
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

for (String inList : myList) {
System.out.println(inList);
}



Error
ClassB.java:11: incompatible types
found : ClassA
required: java.lang.String
for (String inList : myList) { ...


Well as you can see in the error message it says found ClassA required String for (String inList : myList).

So the code state that in the List called myList each element will be a String But this is not true because it is declared as a ArrayList of ClassA : .

In conclusion the solution is to do :
as the compiler told you.
 
Tommy Becker
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you're printing "This is ClassA" from the constructor of ClassA. This code as written should indeed print "This is ClassA" 3 times, but it's not working the way I think you think it's working. Think about what is actually happening when you say System.out.println(inList). . .
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic