为了账号安全,请及时绑定邮箱和手机立即绑定

往列表插入元素出错,错误描述和代码截图如下

 创建数组,往数组里添加元素,然后把数组转换成List ,但是转换过来以后 List里面的所有元素都变成了数组里的最后一个元素 。

截图没发上,代码如下:

public void pokerAdd(){

  System.out.println("~~~~~~~~~~创建扑克牌~~~~~~~~~~");

  PokerGame[] pokerGame =

  {new PokerGame("黑桃","2"),new PokerGame("黑桃","3"),new PokerGame("黑桃","4"),new PokerGame("黑桃","5"),new PokerGame("黑桃","6"),new PokerGame("黑桃","7"),new PokerGame("黑桃","8"),new PokerGame("黑桃","9"),new PokerGame("黑桃","10"),new PokerGame("黑桃","J"),new PokerGame("黑桃","Q"),new PokerGame("黑桃","K"),new PokerGame("黑桃","A")

  ,new PokerGame("红桃","2"),new PokerGame("红桃","3"),new PokerGame("红桃","4"),new PokerGame("红桃","5"),new PokerGame("红桃","6"),new PokerGame("红桃","7"),new PokerGame("红桃","8"),new PokerGame("红桃","9"),new PokerGame("红桃","10"),new PokerGame("红桃","J"),new PokerGame("红桃","Q"),new PokerGame("红桃","K"),new PokerGame("红桃","A")

  ,new PokerGame("梅花","2"),new PokerGame("梅花","3"),new PokerGame("梅花","4"),new PokerGame("梅花","5"),new PokerGame("梅花","6"),new PokerGame("梅花","7"),new PokerGame("梅花","8"),new PokerGame("梅花","9"),new PokerGame("梅花","10"),new PokerGame("梅花","J"),new PokerGame("梅花","Q"),new PokerGame("梅花","K"),new PokerGame("梅花","A")

  ,new PokerGame("方块","2"),new PokerGame("方块","3"),new PokerGame("方块","4"),new PokerGame("方块","5"),new PokerGame("方块","6"),new PokerGame("方块","7"),new PokerGame("方块","8"),new PokerGame("方块","9"),new PokerGame("方块","10"),new PokerGame("方块","J"),new PokerGame("方块","Q"),new PokerGame("方块","K"),new PokerGame("方块","A")

  };    //创建牌组,插入52张牌

this.pokers = new ArrayList(Arrays.asList(pokerGame)); //将数组转换为List,并且插入pokers表中

     System.out.println("~~~~~~~~~扑克牌创建成功~~~~~~~~~");

     System.out.print("牌组为:");

    for(PokerGame poker: pokers){

       System.out.print(poker.getColor()+poker.getPoint());

       System.out.print(" ");

    }

     System.out.println("");

  }


正在回答

3 回答

package pkpyouxi;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

class PokerGame {

public String colour;

public String point;

public PokerGame(String colour,String point){

this.colour=colour;

this.point=point;

}

 }

public class PokerTest {

public List PockSelect;

public PokerTest(){

this.PockSelect=new ArrayList();

}

public void pokerAdd(){

 System.out.println("~~~~~~~~~~创建扑克牌~~~~~~~~~~");

 PokerGame[] pokerGame =

 {new PokerGame("黑桃","2"),new PokerGame("黑桃","3"),new PokerGame("黑桃","4"),new PokerGame("黑桃","5"),new PokerGame("黑桃","6"),new PokerGame("黑桃","7"),new PokerGame("黑桃","8"),new PokerGame("黑桃","9"),new PokerGame("黑桃","10"),new PokerGame("黑桃","J"),new PokerGame("黑桃","Q"),new PokerGame("黑桃","K"),new PokerGame("黑桃","A")

 ,new PokerGame("红桃","2"),new PokerGame("红桃","3"),new PokerGame("红桃","4"),new PokerGame("红桃","5"),new PokerGame("红桃","6"),new PokerGame("红桃","7"),new PokerGame("红桃","8"),new PokerGame("红桃","9"),new PokerGame("红桃","10"),new PokerGame("红桃","J"),new PokerGame("红桃","Q"),new PokerGame("红桃","K"),new PokerGame("红桃","A")

 ,new PokerGame("梅花","2"),new PokerGame("梅花","3"),new PokerGame("梅花","4"),new PokerGame("梅花","5"),new PokerGame("梅花","6"),new PokerGame("梅花","7"),new PokerGame("梅花","8"),new PokerGame("梅花","9"),new PokerGame("梅花","10"),new PokerGame("梅花","J"),new PokerGame("梅花","Q"),new PokerGame("梅花","K"),new PokerGame("梅花","A")

 ,new PokerGame("方块","2"),new PokerGame("方块","3"),new PokerGame("方块","4"),new PokerGame("方块","5"),new PokerGame("方块","6"),new PokerGame("方块","7"),new PokerGame("方块","8"),new PokerGame("方块","9"),new PokerGame("方块","10"),new PokerGame("方块","J"),new PokerGame("方块","Q"),new PokerGame("方块","K"),new PokerGame("方块","A")

 };    //创建牌组,插入52张牌

  

this.PockSelect = new ArrayList(Arrays.asList(pokerGame)); //将数组转换为List,并且插入pokers表中

    System.out.println("~~~~~~~~~扑克牌创建成功~~~~~~~~~");

    System.out.print("牌组为:");

   for(PokerGame poker: pokerGame){

      System.out.print(poker.colour+poker.point);

      System.out.print("; ");

   }

   

 }

public static void main(String[] args){

PokerTest ff=new PokerTest();

ff.pokerAdd();

}


}


0 回复 有任何疑惑可以回复我~

Course[] course3={new Course("3","VB"),new Course("4","VC")};
courseToSelect.addAll(Arrays.asList(course3));

0 回复 有任何疑惑可以回复我~

ArryList.aslist(Arry);这样就可以了。你这种问题最好带上代码提问。

0 回复 有任何疑惑可以回复我~
#1

温柔的晚风 提问者

我添加上代码了,帮忙看看
2016-07-27 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

往列表插入元素出错,错误描述和代码截图如下

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信