繁体   English   中英

错误:不兼容的类型:尝试合并两个已排序的数组时,int []无法转换为int

[英]error: incompatible types: int[] cannot be converted to int when trying to merge two sorted arrays

试图合并两个排序的数组,但我收到一个错误:

错误:不兼容的类型:int []无法转换为int

我做了一些研究,但我无法弄清楚为什么会发生这种情况。 我知道我的程序存在排序数组的问题,但我不确定修复程序是什么。

这是我的代码:

public class MedianOfTwoSortedArrays 
{   
    //[1,2],{3,4}
    static int[] nums1 = new int[]{1, 3};
    static int[] nums2 = new int[]{2};


    public static void main(String args[])
    {
        System.out.println(findMedianSortedArrays(nums1, nums2));
    }

        public static int findMedianSortedArrays(int[] nums1, int[] nums2) 
        {
            int[] sorted = new int[nums1.length + nums2.length];
            int i = 0, j = 0, k = 0;

            while (i < nums1.length && j < nums2.length)
            {
                if (nums1[i] < nums2[j])
                {
                    sorted[k] = nums1[i];
                    i++;
                }
                else
                {
                    sorted[k] = nums2[j];
                    j++;
                }
                k++;
            }

            while (i < nums1.length)
            {
                sorted[k] = nums1[i];
                i++;
                k++;
            }

            while (j < nums2.length)
            {
                sorted[k] = nums2[j];
                j++;
                k++;
            }

            return sorted;
        }

}

快速解决! 只需将您的return语句更改为:

  return sorted[sorted.length / 2];

您返回的是int [](已排序的数组),而不是已排序数组的中值。

我测试了偶数个元素,它返回了两个中间值fyi中的第一个。 如果您愿意,可以在方法开始时验证组合长度是否为奇数。 奇怪的工作如预期。 别忘了接受!

你正在返回数组,你的方法说int,因为哪些代码甚至不会编译。 采用

public static int[] findMedianSortedArrays(int[] nums1, int[] nums2)

代替

public static int findMedianSortedArrays(int[] nums1, int[] nums2)

您还必须在main方法中迭代结果数组以打印值。

你正在做的第一个错误是你返回int数组,你的方法返回类型是int。

    public class MedianOfTwoSortedArrays 
    {   
        //[1,2],{3,4}
        static int[] nums1 = new int[]{1, 3};
        static int[] nums2 = new int[]{2};


        public static void main(String args[])
        {
            int a[] = findMedianSortedArrays(nums1, nums2);

            for(int i=0;i<a.length;i++){
                System.out.println(a[i]);
            }
          }

            public static int[] findMedianSortedArrays(int[] nums1, int[] nums2) 
            {
                int[] sorted = new int[nums1.length + nums2.length];
                int i = 0, j = 0, k = 0;

                while (i < nums1.length && j < nums2.length)
                {
                    if (nums1[i] < nums2[j])
                    {
                        sorted[k] = nums1[i];
                        i++;
                    }
                    else
                    {
                        sorted[k] = nums2[j];
                        j++;
                    }
                    k++;
                }

                while (i < nums1.length)
                {
                    sorted[k] = nums1[i];
                    i++;
                    k++;
                }

                while (j < nums2.length)
                {
                    sorted[k] = nums2[j];
                    j++;
                    k++;
                }

                return sorted;
            }

    }

你必须将结果存储在数组中并循环它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM