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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.