• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

access objects with same name but different attributes

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest:
In your code:

I think that you intended to declard m_cards as an array type, like this:
private Card[] m_cards;
Right?
 
Peter Merker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic