简体   繁体   English

如何在ArrayList上调用方法

[英]How to invoke methods on an ArrayList

I'm building a little deck of cards class using an ArrayList. 我正在使用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. 我遇到的问题是,当我尝试调用Deck方法填充列表时,遇到了“找不到符号”。 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. alArrayList的实例,而ArrayList类没有Deck方法。 What you should do is to declare and initialize a variable of type Deck which has a Deck method: 您应该做的是声明并初始化一个Deck类型的变量,该变量具有Deck方法:

//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. 但是似乎您想初始化Deck类构造函数中的al变量中的元素,您目前还没有。 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: 只需删除void关键字,它将成为一个构造函数:

public Deck() {
    ...
}

So, after you declared a real constructor, then there's no Deck method anymore, and your code will only be: 因此,在声明了一个真正的构造函数之后,就不再有Deck方法了,您的代码将只有:

//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. al没有Deck方法,因为它是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. 您可以通过传递al作为参数来调用Deck()(例如Deck(al)),否则您可以调用Deck(),并且由于al在同一类中,因此您不会有任何问题。 However in the second case al must be a field of the Deck class, otherwise deck method won't find al symbol. 但是,在第二种情况下,al必须是Deck类的字段,否则deck方法将找不到al符号。

The method named Deck() is the constructor for your class. 名为Deck()的方法是您的类的构造函数。 In your main method, you create an instance of Deck with something like: 在您的主要方法中,您将创建一个Deck实例,其内容如下:

Deck deck = new Deck();

At the top of your Deck class, you have: 在Deck班级的顶部,您有:

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. 这意味着Deck的每个实例将具有一个名为al的ArrayList成员,该成员最初是一个空的ArrayList,用于容纳字符串。

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".... 通过使用构造函数实例化Deck时,构造函数中的代码运行,并且ArrayList中填充了“黑桃A”,“黑桃A”。

The main method is a static method. 主要方法是静态方法。 That means that you can not directly access the ArrayList named al. 这意味着您不能直接访问名为al的ArrayList。 To access it, you must go through an instance of Deck. 要访问它,您必须浏览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. 例如,如果您想查看卡片组中的卡片,则必须实例化Deck,然后以deck.al的身份访问阵列列表。 Typically you don't want to access the member fields directly from main. 通常,您不想直接从main访问成员字段。 You would want to create methods like shuffle() and deal() that each use the member al as their internal data. 您可能想要创建诸如shuffle()和deal()之类的方法,每个方法均使用成员al作为其内部数据。

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

Very quick primer on Java Object-oriented programming (beginner level advice). Java面向对象编程的快速入门(初学者级建议)。 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. 对于您的情况,您有原型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.) (应该将Deck始终是52张牌,4套AKQJ ... 2;或者应该创建一个标准Deck扩展Deck并具有这些特征。其他套牌可能是Bid Whist套牌(额外的笑话),黑桃牌, Canasta甲板,Samba甲板等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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