Shuffling a deck in Java

余生长醉 提交于 2019-12-04 13:30:02

Your Deck type isn't an array type. It has an array in it, but you can't use it like one - which is what's generating your error. Consider this... what if your Deck had two Card[52]s in it? then if you called deck[25] it wouldn't make any sense. Typically when you're hiding an array inside an object you'll want to expose the array with accessor methods like public Card get(int i) to retrieve elements out of an inner array.

In order to solve your problem, change the lines of code that you highlighted to:

Card temp = deck.cards[i];
deck.cards[i] = deck.cards[rand];
deck.cards[rand] = temp;

There were 2 problems with the code.

  • The first was that Deck is not an array, and you wanted to access the cards array within it(Hence adding .cards to the arrays).
  • Also the Deck[] expects to be given an array of decks, instead what you were trying giving it, a Card.

Hope this helps,

Lee

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!