Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array List does not allow .add or .get

 
Tim Kinnaird
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Noob here. Really, really raw noob.

The above is code from the book Programming Video Games/Evil Genius published in 2008.

The problems are with the .add (line 25) and .get (line 36) for the array called 'games'. I've poked around and it appears that this may have been changed since version 5 which is probably after publication of this book. As you can see, I added "<String>" (line 13 now commented out) when I created the array per examples at Sun website but this had no effect.

Program is supposed to allow user to input elements (strings) to the array and then call them out by their element position. Will not compile or run.

Can anyone point me in the right direction?

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you'd post the compiler error you get - verbatim. Those errors actually mean something. without you posting it, we either have to guess, or go through the hassle of copying your code and compiling it ourselves. Your job, as the asker of the question, is to make it as easy as possible for me to answer it.

edit: It is probably not a good idea to make your own class called ArrayList since there already is such a class as part of the API. It may work, but you are setting yourself up for confusion.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Kinnaird wrote:

Your own class is also called ArrayList, and despite the import it shadows java.util.ArrayList. Rename it to something else.
 
Joydeep Ghatak
Ranch Hand
Posts: 42
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

First of all as correctly pointed out by 'Rob', your classname has to be changed into something as per the standard Java cosing practice. [Should not be 'ArrayList']

I have changed the classname and able to compile and execute the program successfully,

Thanks,
Joydeep
 
Tim Kinnaird
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Fred, Rob and Joydeep.

I've been trying to name these tutorial files something that will tell me what's in them for future use. It never occurred to me that I'd be creating a confilct.

Fred, Sorry for not including my error message. I'll be sure to do that in the future.

Renamed my class and got it to work. Something interesting is that I had to change my import from 'java.util.*' to 'java.util.ArrayList' to get it to compile. I thought that 'java.util.*' meant to load in all utilities?

Either way, you guys have sorted me out.

Thanks again.

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to apologize...I just want you to get the best experience from here that you can. Nothing is more frustrating than asking for help, and being ignored.

Make sure you have deleted your old ArrayList.class file. If that is still around, you may be confusing the compiler as well. I would have thought 'java.util.*' would work, but I am not the expert.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic