我正在使用Blackjack模拟器。我有以下字符串数组deck
(我更喜欢在枚举上使用数组):
string[] deck = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", };
用户将使用输入对话框输入卡组号:
byte deckNumber;
deckNumber = Convert.ToByte(Interaction.InputBox("Enter The Deck Number", "Deck Number", "3", 10, 10));
如何使用deckNumber
复制数组?例如,用户输入2
,数组将为:
{ "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2","3","4","5","6","7","8","9","10","J","Q","K","A" };
我想我需要一个for循环,但我无法做到。
答案 0 :(得分:4)
string[] deck = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", };
var newDeck = Enumerable.Repeat(deck, 2).SelectMany(x => x).ToArray();
答案 1 :(得分:1)
如果您使用的是.NET 3.5或更高版本,则可以使用LINQ作为L.B.已经表明:
var newDeck = Enumerable.Repeat(deck, 2).SelectMany(x => x).ToArray();
Enumerable.Repeat(deck, 2)
为您提供了两个元素,每个元素都是string[]
,.SelectMany(x => x)
将其展平为一系列字符串,.ToArray()
显然将该系列转换为数组。< / p>
否则,您可以使用循环:
var newDeck = new string[deck.Length * deckNumber];
for (int d = 0; d < deckNumber; d++)
{
for (int i = 0; i < deck.Length; i++)
{
newDeck[d * deck.Length + i] = deck[i];
}
}
答案 2 :(得分:0)
var source = new string [] {“a”,“b”};
var copy = new List(source).ToArray();