猿问

应该怎么插入元素到列表中,我这种方法哪里出错了?

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

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张牌
	 	   List<PokerGame> pokers = Arrays.asList(pokerGame);
	 	  this.pokers = new ArrayList(Arrays.asList(pokerGame)); //将数组转换为List,并且插入pokers表中
	 	   System.out.println("~~~~~~~~~扑克牌创建成功~~~~~~~~~");
	      System.out.print("牌组为:");
	     for(int h=0;h<(pokerGame.length - 1);h++){
	    	PokerGame poker =(PokerGame)pokers.get(h); 
	        System.out.print(poker.color+poker.point);
	        System.out.print(" ");


温柔的晚风
浏览 1366回答 2
2回答

温柔的晚风

为什么我上传了图片,在问题中却看不到?
随时随地看视频慕课网APP

相关分类

Java
我要回答