为什么这个源代码给出输出“system.int32 []”而不是合并的array.please建议的值 [英] Why this source code giving output "system.int32[]" instead of values of merged array.please suggest

查看:94
本文介绍了为什么这个源代码给出输出“system.int32 []”而不是合并的array.please建议的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Program dn = new Program();

            int[] array1 = { 1, 2, 3 };
            int[] array2 = { 1, 2, 3 };
            int[] res = dn.merge(array1, array2);

            Console.WriteLine(res);
        }

        public int[] merge(int[] array1, int[] array2)
        {
            int[] array3 = new int[6];
            int[] arr4 = new int[6];
            int i, j;
            int array1length = array1.Length;
            int array2length = array2.Length;
            //array3 = array1.Concat(array2).ToArray();
            for (i = 0; i < array1length; i++)
            {
                array3[i] = array1[i];
            }

            int FinalLength = array1length + array2length;
            for (j = 0; j < array2length; j++)
            {
                i++;
                array3[(i - 1)] = array2[j];

            }
            for (int k = 0; k <= arr4.Length; k++)
            {
                arr4 = array3;
            }
            return arr4;
        }
    }
}





我尝试了什么:



我调试了代码,它返回了确切的输出,即两个数组的值作为合并数组。但是在控制台屏幕上显示system.int32 []。



What I have tried:

I have debug the code and it is returning the exact output i.e the values of two arrays as a merged array.But on console screen it is showing "system.int32[]".

推荐答案

您遇到的问题是因为使用Console.WriteLine()写入控制台的文本是调用ToString()的结果。输入对象。因此,要解决此问题,您需要做的就是自己将数组转换为字符串。一种方法是使用string.Join()。如果您决定使用该方法,那么您将替换:
The problem you are seeing is because the text written to the console with Console.WriteLine() is the result of calling ToString() on the input object. So to fix the problem all you need to do is convert the array to a string yourself. One way of doing this is to use string.Join(). If you decide to use that method then you would replace:
Console.WriteLine(res);

with:

Console.WriteLine (string.Join(", ", res));



这有意义吗?


Does that make sense?


对不起先生,因为我在我的代码中犯了一个错误的错误。我已经改了,现在它显示了确切的输出。



更改如下:



Sorry sir, as I made a blunder mistake in my code. I have retified that and now it is showing the exact output.

The change are given below :

for (int i = 0; i <= res.Length-1; i++)
     {
         Console.WriteLine("{0}",res[i]);
     }


这篇关于为什么这个源代码给出输出“system.int32 []”而不是合并的array.please建议的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆