wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array List does not allow .add or .get Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array List does not allow .add or .get " Watch "Array List does not allow .add or .get " New topic

Array List does not allow .add or .get

Tim Kinnaird

Joined: Jul 15, 2011
Posts: 5

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

Joined: Oct 02, 2003
Posts: 11155

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor

Joined: Oct 27, 2005
Posts: 19651

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

Joined: Sep 30, 2009
Posts: 41

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,


“Men are only as good as their technical development allows them to be.”
Tim Kinnaird

Joined: Jul 15, 2011
Posts: 5
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

Joined: Oct 02, 2003
Posts: 11155

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.
It is sorta covered in the JavaRanch Style Guide.
subject: Array List does not allow .add or .get
Similar Threads
Finding and changing array elements
Using using an enhanced for loop
Scanner debugging help
GUI and Strings
BufferedReader help