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

ArrayList Problem(s)

 
Joseph Carr
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Carr
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the command line?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ubuntu Climber ",
Please check your private messages.
-Ben
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic