Home  >  Article  >  Do I need to provide unique names for all objects in the list?

Do I need to provide unique names for all objects in the list?

王林
王林forward
2024-02-08 21:30:25620browse

In programming, we often encounter situations where we need to provide unique names for objects in a list. But is it really necessary to give each object a unique name? PHP editor Strawberry tells you that the answer is not absolute. In some cases, giving objects unique names can better organize and manage your code, especially if the objects need to be referenced or distinguished. However, in other cases, it may not be necessary for the object to have a unique name and can be identified and manipulated in other ways. Therefore, whether you need to provide unique names for all objects in the list depends on your specific needs and design.

Question content

I am a first-year computer science student. As part of my java class assignment. I need to create a deck class which is an arraylist containing card objects.

Here is an example of the card object I coded:

public class Card
{
    // instance variables 
    private int face;
    private int suit;

    /**
     * Constructor for objects of class Card.
     * Note: Hearts = 1, Diamonds = 2,
     * Spades = 3, Clubs = 4
     * 
     * All face cards equal their common value
     * Ex. A = 1
     */

    public Card(int face, int suit)
    {
        // initialize instance variables
        this.face = face;
        this.suit = suit;
    }
    
    public int getFace()
    {
        return face;
    }
    
    public int getSuit()
    {
        return suit;
    }
    
    public String toString()
    {
        return face + "" + suit;
    }
}

I understand how to load each individual card into a standard deck. I just can't figure out how to give each card a unique name. For example, how do you display the cards and suits on the screen so players can see the cards in their hands? (We should also create a class that contains the list of cards in the player's hand.)

If I create a list of card objects, can I somehow reference the slot they are in to call their respective methods? What should I do? Will things change if I move them from the deck to my hand?

Normally, when you use methods on an object, you create the object with a name (let's say, card1). Then, to call a method that uses that card's data, if I wanted to return the suit value, you would say card1.getsuit() . But I don't know how to do this when creating many objects in the list.

I feel like my brain might be overcomplicating things, but I think it would be better to understand different perspectives of java better anyway, so I like asking these kinds of questions. Thanks to anyone who can help!

Note: I've just started my second semester of java classes, so probably the furthest we've learned is like inheritance. We are not expected/shouldn't know about things like enums, constant lists, or anything more complex I guess. I hear we'll be doing a lot of javafx.

Workaround

No, you can store a reference to the object in a list instead of creating separate objects with different names.

List<Object> ls = new ArrayList<>();
 //for adding obj into it 
ls.add(new Card(face,suit));
 //for getting data
ls.get(index).getFace();

This way you can add many objects to the list without giving them different names.

Think of human as a class, and each person is an instance of that class. Do all humans need to have unique names? How many "Joes" or "Ellens" do you know? Can a person have only one name or can he have multiple names? (My mother calls me..., but my friends call me...)

Names are used to refer to humans, just like pointers can refer to objects.

But please note: If java does not find a reference to an object and the object does not exist (i.e. no thread is running in it), the object will be garbage collected and discarded.

The above is the detailed content of Do I need to provide unique names for all objects in the list?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete