• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about ArrayList  RSS feed

 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im not sure if this question is a beginner or intermediate, but here it is:
my friend told me that with the new update of java 5.0 update7 you can make ArrayLists for a specific object, which i though would save me god knows how much trouble with casting objects back to their true state, but when it came to adding things to these arraylists, this is were my problem starts.
when i try to add something(of the same type the arraylist stores) using the .add(*) method, i get a compilation error telling me "<identifier> expected". what is my problem? i suppose its because i dont know anything about the update except that you can make an ArrayList of a specific type...
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the code you wrote?
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


whereas Jutsu and HandSeal are (obviously) custom classes. lol, if you know the anime Naruto you will get it.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that you've got these lines outside of a method in your class definition.
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thats true! why is this important though? not that i wont put them in a method now, but how come it cant do this outside a method?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't have regular code outside of a method or block in a class definition. You can declare instance variables and initialize them when you declare them outside a method, but assignment statements and things like that can't be outside of a method or at least a block in the class definition.
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a block, isn't that just like {/*code*/}? when i just though about it, making a method in this situation would actually be very troublesome.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why troublesome?

You have to have a main method if you're going to run an application.
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its going to be a library of these "jutsus" and now i just realized something, i can initialize all these values in main or in a method....is that what i want to do? i want to be able to call something like "JutsuLibrary.jutsuList.get(x)" or something....
[ September 23, 2006: Message edited by: Micah Pezdirtz ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare the ArrayLists static and populate them in a static initializer block.
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot, i was really kinda stuck with this!
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ArrayList declarations might also generate some sort of "unchecked conversion" compilation warning. To be even more clear and safe, change:



to:


[ September 25, 2006: Message edited by: Bridget Kennedy ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Micah Pezdirtz:
its going to be a library of these "jutsus" and now i just realized something, i can initialize all these values in main or in a method....is that what i want to do? i want to be able to call something like "JutsuLibrary.jutsuList.get(x)" or something....


Sounds like very hacky code. Why are you using an index to get the Jutsu? Could this all be better served by some simple enumerations?

What exactly is the use of this? There's undoubtedly a better way.
 
Brian Buchholtz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for digging up an old post...

I'm afraid that I've run into the same problem and was wondering if anyone did find a solution.

This is where I'm at:



How do I accomplish something like this:



Thanks!

-Brian
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be intuitive ot think like:
myList.Member3.add("data");

...but it doesn't look like that.

Generics were introduced with Java 1.5 - it allows you to supply compile-time type declarations for collections (among other things).

An ArrayList is still a List, though

1.4 syntax


1.5 syntax


I simply don't have to cast.

I could do something like:


Does that make sense?
 
Brian Buchholtz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adam,

Your suggestion takes me a step closer. Thank you for your help!

For whatever reason, the compiler thinks that I've neglected to import 'java.lang.String' when trying to add items to the list.

I'm using Java 1.5. Should I be using the Generic data type instead?

Best,

-Brian
 
Brian Buchholtz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it... never mind!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!