Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating a list

 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a list of items that I keep track of in a method in my character class. In another class, I want to be able to print out and keep track of whichever of those items i still have in that method in the character class. In other words, if i have an apple, banana, and orange, I want to have a code that can scan my list of items, see which ones i have and register those in this new class. here is my code. There is a good chance this simply involves making a list, but I don't think I have learned to do that and for some reason the internet is so hard to find the answer to simple questions.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don’t call a class Character, because there already is a class of that name in the java.lang package.
 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, that asnwered another question I had of why it made me specifically import that class
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand. From what I gather, you want to insert something at line 35 that will display a list of available items in the Character class. Right?

Your items() method is currently just assigning values to variables. Is that your current "list"? If so, what do the values of '1' represent? Are these quantities (e.g., how many mpotions are available)? What exactly would you want returned from that example?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Meanwhile, here is an introduction to Java Lists, Sets, Maps, etc... Java Tutorial - Collection Interfaces.)
 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ive done some more reading and figured out how to make an array and solve this problem. I am not going to even bother posting my further questions about arrays because I know that I will figure this stuff out on my own as I keep reading. Thanks for the help guys.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got so many fields in that class? Why have you given them public access?
 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ritchie if i dont make them public, my other classes dont recognize them
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brent carter wrote:ritchie if i dont make them public, my other classes dont recognize them

See Java Encapsulation.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should give the other classes access via getXXX() methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic