简体   繁体   English

另一个arraylist到另一个不同类型的arraylist

[英]Another arraylist to another arraylist of different type

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;
         }

//Capture Class import java.util.ArrayList; // Capture Class import 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();}

Sorry for the long post but my problem lies on the parts commented, the errors appear making me unable to add the player hand cards and pool cards into the ArrayList<Capture> Capture list, but this list cannot be removed as it is being used in another class to detect other features used by other subclass. 对于长篇文章很抱歉,但我的问题在于评论的部分,出现的错误使我无法将播放器手牌和池卡添加到ArrayList<Capture> Capture列表中,但此列表无法删除,因为它正在被用于另一个类来检测其他子类使用的其他功能。 How do I solve the error and add the arraylist capture into a new type array list? 如何解决错误并将arraylist捕获添加到新类型数组列表中?
**Edited, Capture class has been added but not complete yet **已编辑,已添加Capture类但尚未完成

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

In this line 在这一行

player.getCards().get(num)

is returning a Card object reference. 正在返回一个Card对象引用。 But you decleared the Arraylist as 但你贬低了Arraylist

private ArrayList<Capture> capture;

So 所以

capture.add();

this method can only take a Capture Object reference as parameter or any object reference that may have same parent as Capture. 此方法只能将Capture Object引用作为参数或任何可能与Capture具有相同父对象的对象引用。 where as you are providing it a Card Object reference And I am assuming it Card does not have the same ancestor or implements from the same interface as Capture. 你在哪里提供卡片对象参考我假设它没有与Capture相同的祖先或实现相同的祖先。 That is why it is giving you that error. 这就是为什么它会给你这个错误。

Make an Interface let's say CardAndCapture and your Card and Capture class should implement this interface. 制作一个接口让我们说CardAndCapture和你的Card和Capture类应该实现这个接口。 Now in your code, change this line 现在在您的代码中,更改此行

capture = new ArrayList(); capture = new ArrayList();

into

capture = new ArrayList(); capture = new ArrayList();

Now your code should compile and run. 现在你的代码应该编译并运行。 You are making Card and Capture siblings here. 你在这里制作Card和Capture兄弟姐妹。 So, make sure to properly integrate with CardCapture interface. 因此,请确保与CardCapture接口正确集成。

If Capture has Card as instance variable like this: 如果Capture将Card作为实例变量,如下所示:

 public class Capture{
    private Card card;

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

Usage 用法

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

Store object in a common list just if they have common usage. 将对象存储在公共列表中,只要它们具有通用用法即可。 If they have common usage then partition this functionality into an interface and implement this interface by the classes intended to handle in the same manner. 如果它们具有通用用法,则将此功能划分为接口,并通过旨在以相同方式处理的类来实现此接口。 Create a generic list with this interface type and do method calls on the interfaces: 使用此接口类型创建通用列表,并在接口上执行方法调用:

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();
    }
  }
  //...
}

If you want to add an object/interface to a generic list that is not conform with its class (it hasn't the ability to call the methods on it) the compiler log an error message. 如果要将对象/接口添加到不符合其类的通用列表(它无法调用其上的方法),编译器将记录错误消息。

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

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