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 关注
- 177 浏览
添加回答
举报