• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayLists  RSS feed

 
John Paterson
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I have this Arraylist:



I now want to print out, say the value in index 2 of the arrayList.
I tried the following in my method:


Unfortunately I am getting the following error:
incompatible types
required: ArrayList<Fruits>
found: Fruits


This method is in a Struts2 action class, I am basically sending 'fruitList' to a jsp page.
The following works without a problem, it returns the entire arraylist:


However what I want is just the value from a particular index. Hope someone can help. Thank you.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at what the get method returns in the ArrayList API to understand what is wrong on your code.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Paterson wrote:I now want to print out, say the value in index 2 of the arrayList.
I tried the following in my method:

You say you want to get the value of the item at index 2 (which is going to be a Fruit instance), but you have declared your method to return an ArrayList of Fruits.
Then on the right hand side of the expression on line 2 you retrieve the Fruit instance you want (that's good) but you try to assign it to a variable that has type ArrayList of Fruit.

So you need to change your method's return type, declare a variable to hold the Fruit you retrieve from your ArrayList and then return the value of that variable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!