• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ArrayList Problem(s)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the command line?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ubuntu Climber ",
Please check your private messages.
-Ben
 
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>();

 
Hey! Wanna see my flashlight? It looks like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic