• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList.get incompatible type  RSS feed

 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am trying to access a Car object in an arraylist with the .get function however what ever i try to do it seems that it keeps tell me it is an incompatible type
Here is the code


please help me I have been looking all over for a solution and its really starting to get on my nerves (i can't even start testing anything yet)

thank you
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code isn't complete.
How is the CarList declared?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More over the ideal data structure would be a Map, so that you can map the car number to the car instance.

The get method in the List supports index based look up and not the one you are expecting unless your Car Number follows the index of the car in the List.

Update: Just thought of giving a Pseudo code so that it can help you with the actual code:


Also do you really think the Car Number would be an int?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably just declared it as a List, without qualifying the element type. If so, you need to either cast, or use generics.



For details, google for java collections tutorial and java generics tutorial
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry for not giving the full code
Here is the (abridged) full version of the code

@Mohamed I'm doing this for coursework so they have requested that i "give the RentalCompany class an attribute that is an array list of objects of type Car."
also I think CarNumber must be an int but maybe i don't fuly understand arraylist although i does seem to work for the other fuctions such as .remove
@Jeff I think i have declared the list properly as everthing else seems to compile ok.




Thanks for your quick replies
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't declared CarList with the generic type on line 6. Yes, you've used a generic type when you create it on line 14, but it's the declared type that matters when you start trying to manipulate it.

Because you're just using a plain ArrayList, CarList.get(CarNumber) returns an Object, and you try and assign it to a Car. You can't do that without a cast. Fix the declaration and it will be OK.

(I'd also strongly suggest following Java conventions in your code, to help readability. That means starting with lower case for methods and variables names.
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Mathew and everyone else you have all been a great help finally i can get this stuff done
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!