【问题标题】:Another arraylist to another arraylist of different type另一个数组列表到另一个不同类型的数组列表
【发布时间】:2017-08-20 16:09:17
【问题描述】:
public class Game {

    private ArrayList<Player> players;
    private ArrayList<Card> deck;
    private ArrayList<Card> pool;
    **private ArrayList<Capture> capture;**
    private int currentPlayer;
    private int playDirection;
    int index;
    int count = 0;
    int passNo = 0;

    Scanner input = new Scanner(System.in);
    Scanner input2 = new Scanner(System.in);
    Scanner input3 = new Scanner(System.in);

    public Game()
    {
        deck = new ArrayList<Card>();
        pool = new ArrayList<Card>();
        capture = new ArrayList<Capture>();

        for(int c=0; c<4; c++){
            for(int i=0; i<13; i++){
                deck.add(new Card(c,i));
            }
            Collections.shuffle(deck);
        }   

    }       

     public Player play() {
         Player player = getCurrentPlayer();
         int pass = -1;
         int option =0;
         int count =0;
         boolean play = false;
         boolean check = true;

         capture = new ArrayList<Capture>();

         System.out.println();
         System.out.println(player + ":" + player.getCards());

         do{
             System.out.println("Please select an option: ((1) Capture (2) Discard a card)");
             option = input2.nextInt();


             play=player.isPlayable();


             switch(option)
             {
             case 1:
                 if(play == true){

                     System.out.print("HandCard:" + player.getCards());
                     System.out.print("  Choose a Number from 0 to " + (player.getCards().size()-1)+ "  :   ");
                     int num = input.nextInt();
                     player.getCards().remove(num);
                  //after prompting user for entering the cards they wanted
                  //the following sentence has following error                                                          
                     **capture.add(player.getCards().get(num));**
                   //"The method add(Capture) in the type ArrayList<Capture> is  
                    //not applicable for the arguments (Card)"  
                    System.out.print("How many card you want capture from pool: (Choose 1 number from  1 to " + pool.size()+ ")" + "  :  ");
                    option = input.nextInt();
                    System.out.println("Please choose your card in the pool:");
                    System.out.println("Pool");
                    for(int j=0; j<pool.size(); j++)
                    {
                        System.out.print("(" + j + ")" + pool.get(j) + " ");
                    }

                    for(int i=0; i<option;i++)
                    {

                            count = input.nextInt();
                            System.out.print(pool.get(count) + " ");
                            pool.remove(count);
                            //same problem as the above error
                            **capture.add(pool.get(count));**
                    }

                    System.out.print(player.getCards().get(num) + "  is selected");
                    System.out.println();
                    System.out.println("=================================================");
                    check=false;
                 }
                 else
                     System.out.println("Invalid Capture, Please choose either (1) Capture or (2) Discard a Card");
                 break;

             case 2:
                 if(play == true){
                     Card discard = player.cardDiscard();
                     System.out.println(discard + " has been added to pool");
                     pool.add(discard);
                     player.removeCard(discard);
                     check=false;
                 }
                 else
                     System.out.println("Invalid Capture Please choose either (1) Capture or (2) Discard a Card");
                 break;

                 default:
                     System.out.println("Invalid option choose");

             }
         }while(check);

         if(pass==currentPlayer)
         {
             passNo++;
         }
         else{
             passNo = 0;
         }

         if(player.getCards().size() == 0 ){
             int i = 1;
             int [] point = new int[players.size()+1];
             point[0] = 100;
             int lowest = 0;
             System.out.println();

             for(Player p : players){
                 System.out.println(p + ":" + p.getTotalScores() + " points");
                 point[i] = p.getTotalScores();

                 if(point[i] < point[i-1]){
                     lowest = point[i];
                 }
                 i++;
             }
             for(Player p:players){
                 if(p.getTotalScores()==lowest)
                 {
                     player = p;
                 }
             }
             return player;
             }

         goToNextPlayer();
         System.out.println();
         System.out.println("=========================================================================");
         System.out.println("Pool"); 
         for(int i=0; i<pool.size(); i++)
            System.out.print("(" + i + ")" + pool.get(i) + " ");
            return null;
         }

//捕获类 导入 java.util.ArrayList;

       public abstract class Capture {
              private static ArrayList<Capture> capture;
              private static ArrayList<Card> card;
              boolean result = false;


public Capture()
{
    // leave bank 
}


// this part is the functions that hv to implement in pair , run , combo class
public abstract boolean formCapture(ArrayList<Capture> c);
public abstract double getScore();


public abstract void showMessage();}

很抱歉这篇长文,但我的问题在于评论的部分,出现的错误使我无法将玩家手牌和台球牌添加到 ArrayList&lt;Capture&gt; Capture 列表中,但此列表无法删除,因为它正在使用在另一个类中检测其他子类使用的其他特征。如何解决错误并将 arraylist 捕获添加到新类型的数组列表中?
**已编辑,已添加 Capture 类但尚未完成

【问题讨论】:

  • CaptureCard是什么关系?
  • 您只能添加 List 与 Capture 成员函数。
  • 是否要将 Card 后代添加到 ArrayList?是否也捕获 Capture 的后代?因为您可以将 so 对象添加到类型符合(它们具有共同祖先或共同接口)到列表项类型的通用列表中。
  • 请给出 Capture 类的代码
  • 没有为您提供任何答案所需的信息?

标签: java oop arraylist


【解决方案1】:
capture.add(player.getCards().get(num));

在这一行

player.getCards().get(num)

正在返回一个 Card 对象引用。 但是您将 Arraylist 清除为

private ArrayList<Capture> capture;

所以

capture.add();

此方法只能将 Capture 对象引用作为参数或任何可能与 Capture 具有相同父级的对象引用。当您向它提供 Card Object 引用时,我假设它 Card 没有相同的祖先或从与 Capture 相同的接口实现。这就是为什么它给你这个错误。

【讨论】:

  • 您也可以添加其他类型。只是它们应该类型符合(它们有共同的祖先或接口)到列表项类型
  • 由于他没有给出完整的代码,所以我假设 Cards 和 Capture 没有相同的祖先或接口。正如他所提到的,错误显示“ArrayList 类型中的方法 add(Capture) 不适用于参数 (Card)”我认为它们没有相同的父项
  • 是的,确实如此。但是您写道,他可以添加 Capture 类型的对象。它关闭了解决方案的大门。
【解决方案2】:

创建一个接口,假设 CardAndCapture 和你的 Card 和 Capture 类应该实现这个接口。现在在您的代码中,更改这一行

capture = new ArrayList();

进入

capture = new ArrayList();

现在您的代码应该可以编译并运行了。您在这里制作 Card 和 Capture 兄弟姐妹。因此,请确保与 CardCapture 接口正确集成。

【讨论】:

    【解决方案3】:

    如果 Capture 将 Card 作为实例变量,如下所示:

     public class Capture{
        private Card card;
    
        public Capture(Card card){
           this.card = card;
        }
       ...
     }
    

    用法

     capture.add(new Capture(player.getCards().get(num)));
    

    【讨论】:

      【解决方案4】:

      将对象存储在通用列表中,只要它们具有通用用法。如果它们有共同的用法,则将此功能划分为一个接口,并通过旨在以相同方式处理的类来实现此接口。使用此接口类型创建一个通用列表并在接口上执行方法调用:

      public interface ICommonUsage
      {
        public void foo();
        public void bar();
      }
      
      public class Class1 implements ICommonUsage
      {
        //...
        public void foo() {}
        public void bar() {}
        //...
      }
      
      public class Class2 implements ICommonUsage
      {
        //...
        public void foo() {}
        public void bar() {}
        //...
      }
      
      public class Class3
      {
        //...
        private List<ICommonUsage> commonUsedObjects;
      
        public void commonUsage()
        {
          for ( ICommonUsage item : commonUsedObjects )
          {
            item.foo();
            item.bar();
          }
        }
        //...
      }
      

      如果您想将对象/接口添加到不符合其类的通用列表(它无法调用其上的方法),编译器会记录错误消息。

      【讨论】:

        猜你喜欢
        • 2013-03-15
        • 1970-01-01
        • 2017-11-18
        • 1970-01-01
        • 1970-01-01
        • 2013-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多