aspose file tools*
The moose likes Beginning Java and the fly likes access objects with same name but different attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "access objects with same name but different attributes" Watch "access objects with same name but different attributes" New topic
Author

access objects with same name but different attributes

Peter Merker
Ranch Hand

Joined: Oct 18, 2003
Posts: 37
Hi!
If i put a constructor inside a for loop like this:

...and the corresponding method in Card would be something like

Then I would get 52 Card Objects with the value for attribute "number" ranging from 1 to 52. How can I access these different objects as they all have the same name? Is there a possibility to access them by their attribute "number", respect. their method "showNumber"?
Actually, I have several subdecks, each with a card array and need to access these arrays. But if there's a way of doing this, it probably works for both cases.
Thanks,
Peter
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You'll want to store the whole collection of cards in an array or Collection object. Since you know there are exactly 52 cards and the number is fixed, an array would be just fine. So the class might look like
class Deck {
public static final int NUMBER_OF_CARDS=52;
private Card m_cards;
public Deck() {
m_cards = new Card[NUMBER_OF_CARDS];
for (int i=0; i<NUMBER_OF_CARDS; ++i) {
m_cards[i] = new Card(i+1);
}
}
}
[/code]
Now the "method" in Card is actually a Constructor, and Constructors don't have a return type, not even void, and they can't be static; so that class looks like
[code]
class Card {
private int m_number;
public Card(int number) {
m_number = number;
}
}
[/code]

Now, as far as "subdecks" go, I'm not a Cribbage player so I'll just have to take your word for it. I'd simply create a new Deck constructor that took an array of Card objects to include, and copied them into a new array stored in the m_cards member; so to create a subdeck, you'd create Deck by specifying only the cards it should contain. If a Deck should know about its subdecks, then Deck should have a java.util.Set or other collection of Deck children.


[Jess in Action][AskingGoodQuestions]
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Ernest:
In your code:

I think that you intended to declard m_cards as an array type, like this:
private Card[] m_cards;
Right?


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Peter Merker
Ranch Hand

Joined: Oct 18, 2003
Posts: 37
Thanks for your replies!
But I still don't know if it is possible to access instances by their attribute value. Actually, my code is similar to the one Ernest wrote. I have an array of cards in each deck object. There should be several deck objects which simulate the cards each player gets. So I had the idea of constructing these subdecks in a for loop. Is there some way to access these subdecks directly or should I put them also in an array, so I can access them by their indices?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Herb --
Yep. Typing too fast.
Peter --
Yes, you'll need to put the subdecks into an array, or a java.util.List, or a java.util.Set, or a java.util.Map, or something. There are plenty of choices, depending on precisely how you want to access them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access objects with same name but different attributes