简体   繁体   中英

How to invoke methods on an ArrayList

I'm building a little deck of cards class using an ArrayList. My problem I'm running into is that when I try to invoke my Deck method to populate the list, I'm getting a Cannot Find Symbol. Code below.

package deckofcards;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Deck{
    ArrayList<String> al = new ArrayList<String>();
    //test method
    public void main(String[] args){
        Scanner input = new Scanner(System.in);
        int choice = 9;
        al.Deck();  //<--Right here is the problem, it gives Cannot Find Symbol
                    //Symbol: method Deck() Location: variable al of type
                    //ArrayList<String>
        while(choice != 0){
            System.out.println("Shuffle - 1 Display - 2 Exit - 0:");
            //didn't finish since it gave an error
        }
    }
    private void Shuffle(){
        Collections.shuffle(al);
    }
    private void Deck(){
        al.add(0, "Ace of Spades");
        al.add(1, "Two of Spades");
        al.add(2, "Three of Spades");
        //yadaa yadaa yadaa

I simply cannot figure out how to properly invoke my methods on the list. Does anyone know what I'm doing wrong?

NOTE: this is a secondary class file, the main class file of the package will simply contain the main test method.

al is an instance of ArrayList , and ArrayList class doesn't have a Deck method. What you should do is to declare and initialize a variable of type Deck which has a Deck method:

//al.Deck();
Deck deck = new Deck();
deck.Deck(); //technically, this will do

But seems that you want to initialize the elements in the al variable in Deck class constructor, which you currently don't have. A constructor is a method that has the same name of the class and doesn't have any return type . So this method:

public void Deck() {
    ...
}

Is not a constructor. Just remove the void keyword and it will become a constructor:

public Deck() {
    ...
}

So, after you declared a real constructor, then there's no Deck method anymore, and your code will only be:

//al.Deck();
Deck deck = new Deck();
//deck.Deck(); //no need for this line anymore

al doesn't have a Deck method since it is an ArrayList. You can call Deck() passing al as argument (eg Deck(al)) or else you may call Deck() and since al is in the same class you won't have any problem. However in the second case al must be a field of the Deck class, otherwise deck method won't find al symbol.

The method named Deck() is the constructor for your class. In your main method, you create an instance of Deck with something like:

Deck deck = new Deck();

At the top of your Deck class, you have:

ArrayList<String> al = new ArrayList<String>();

This means that each instance of Deck will have an ArrayList member called al that is an initially empty ArrayList for holding Strings.

When a Deck is instantiated by using the constructor, the code in the constructor runs and the ArrayList gets populated with "Ace of Spades", "Two of Spades"....

The main method is a static method. That means that you can not directly access the ArrayList named al. To access it, you must go through an instance of Deck. For example if you want to see the cards in the deck, you must instantiate Deck and then access the array list as deck.al. Typically you don't want to access the member fields directly from main. You would want to create methods like shuffle() and deal() that each use the member al as their internal data.

我不知道如何将评论声明为答案,但是“是的。Deck不是ArrayList的方法,它是Deck的方法(顺便说一句,类和方法的名称相同,非常容易出错。你为什么不只打电话给Deck()?– 14分钟前njzk2”是完美的解决方法,非常感谢!

Very quick primer on Java Object-oriented programming (beginner level advice). Create classes that represent real world things. You will often find the Classes you want to create will be one of four archetypes:

  1. a "moment-interval" time related (eg for an event or event-duration)
  2. a "role" - how a party, place or thing participates
  3. a "description" (often "catalog-entry-type description) a collection of things that appear
  4. a "party [person], place or thing" - these may play different roles (you [party] might be an [role] employee and a customer

Then for each class build methods that do something with the object (eg shuffle a deck).

For your situation, you have archetype 4 a Deck. It would appear that a Deck is made up of Cards (another class), and that you would want to shuffle a deck and get the next card from a deck. A Card would have some attributes, suit and value.

Continue down this path to create your classes. In your "main" class you would want to do things with objects of the classes you have created. You will want to ask yourself what a class should know. (Should Deck always be a 52-card, 4-suit AKQJ...2; or should you create a StandardDeck extending Deck and having those characteristics. Other decks might be a Bid Whist deck (extra jokers), a Spades Deck, a Canasta Deck, a Samba Deck, etc.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM