4 回答

TA貢獻1802條經驗 獲得超6個贊
Write而不是WriteLine, 加上使用LINQ更少的代碼:
int[] arr1 = { 37, 45, 65 };
int[] arr2 = { 70, 89, 118 };
Console.WriteLine("Combined array elements..");
arr1.Concat(arr2).ToList().ForEach(x => Console.Write($"{x} "))

TA貢獻1820條經驗 獲得超3個贊
int[] arr1 = { 37, 45, 65 };
int[] arr2 = { 70, 89, 118 };
var myList = new List<int>();
myList.AddRange(arr1);
myList.AddRange(arr2);
int[] arr3 = myList.ToArray();
Console.WriteLine("Combined array elements..");
foreach (int res in arr3)
{
Console.Write(" " + res + " ");
}

TA貢獻1846條經驗 獲得超7個贊
只需使用Console.Write或
string str;
foreach (int res in arr3)
{
str += $"{res} ";
}
Console.WriteLine(str);
在這種情況下,如果數組很大,你應該使用StringBuilder
但在我看來,最好的方法是這樣的:
int[] arr1 = { 37, 45, 65 };
int[] arr2 = { 70, 89, 118 };
Console.Write(String.Join(" ", arr1.Concat(arr2)));
- 4 回答
- 0 關注
- 176 瀏覽
添加回答
舉報