This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ArrayList Problem(s) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList Problem(s)" Watch "ArrayList Problem(s)" New topic
Author

ArrayList Problem(s)

Joseph Carr
Greenhorn

Joined: Dec 11, 2003
Posts: 7
I am having trouble with an ArrayList that I am using, I substituted it for a loop with a regular array and now i am getting an error when I compile: "<identifier> expected." I've tried implementing the ArrayList in a variety of ways, but keep getting the same error. For clarification I am using BlueJ 2.2.1 on Mac OS X 10.5.2
Some of the other code may be wrong, but just because I've been moving everything around while tearing my hair out trying to find out what went wrong...


[ February 20, 2008: Message edited by: Joseph Carr ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

It always helps to cut/paste the actual error message rather than paraphrasing it. The error message contains valuable information that gets lost when you do not post it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In general, a class contains fields (variables) and methods.

When you want to "do" something (like add elements to a List), these statements need to go inside a method body.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Joseph Carr
Greenhorn

Joined: Dec 11, 2003
Posts: 7
I understand. I'll try and be more clear, when I compiled this code in BlueJ it gives me a popup box that I cannot copy so I will type it word for word here:


It's blueJ so the error message is weird. I'd do it in Netbeans or Xcode but I cannot get either of them to compile a single class for some reason...

When you want to "do" something (like add elements to a List), these statements need to go inside a method body.


I'd like to initialize this ArrayList immediately, the other ArrayList (gameStatIntArrayList) will be more dynamic so i'll be initializing it inside of a method.
I can still use the while loop on an Array ratehr than implementing this ArrayList, I just thought that this was the more efficient way to go....four hours later...
[ February 20, 2008: Message edited by: Joseph Carr ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Try the command line?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Joseph Carr:
...when I compiled this code in BlueJ it gives me a popup box...

<identifier> expected...

At that point in the code, you are inside a class definition, so the compiler is expecting an identifier for either a variable or a method. Instead, you are providing a statement to be executed...

gameStatStrArray.add("statStr1");

This type of statement needs to be inside the body of a method (or constructor).
Joseph Carr
Greenhorn

Joined: Dec 11, 2003
Posts: 7
Thanks Bear. I don't thinkn the compiler liked me trying to add stuff into ArrayList outside of a method. Another snafu was using an integer type in an ArrayList (superfluous and just plain wrong). So I just created an initializer method that I have to run before the other methods will work. Thanks for all your help, it was really simpler than I was making it (usually the case).

So, I fixed the code:
Sean Connery
Greenhorn

Joined: Feb 20, 2008
Posts: 5
Originally posted by Joseph Carr:
....
Another snafu was using an integer type in an ArrayList (superfluous and just plain wrong).
....


Just an FYI, but prior to 1.5, ArrayList could only take Objects (NOT primitive variables). However, since 1.5, they introduced autoboxing, so you could do something like:



But you are correct, the generic portion (<Integer> of the List must take an Object.

Cheers,
UbuntuClimber
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"Ubuntu Climber ",
Please check your private messages.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
Originally posted by Ubuntu Climber:
[QB]
Aaaah!

Don't declare a generic object and then instantiate it with a non-generic reference. That is called using a raw type, which for reasons I have forgotten is very much frowned upon. In fact Sun keep threatening to upgrade it to a compiler error.

The correct syntax would be
List<Integer> myList = new ArrayList<Integer>();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList Problem(s)
 
Similar Threads
Exception thrown by getter for property
array[] [] ?
Could anyone help with this???
ParserImpl for DOM
Why am I getting this error?