public class Test
{
    public static void main (String [] args)
    {
      Card [] cardDeck= new Card[52];
      int i;
      for(i=0;i<52; i++ )
      {
      cardDeck[i]= new Card(i+"");
      }


      List<Card> cardList= Arrays.asList(cardDeck);

      List crd= cardList.subList(1, 10);

      for(int x=0; x<crd.size(); x++)
         System.out.println(crd.get(x).getName());  //here is the error, can't use
                                                    //getName()
   }
};

class Card{

    private String name;

    public Card(String n)
    {
      name= n;
    }

    public void setName(String n)
    {
       name=n;
    }

    public String getName()
    {
        return name;
    }
}

最佳答案

更改

List crd




List<Card> crd


subList方法支持列表的通用类型

10-04 16:23