简体   繁体   English

索引 4 超出 4 长度的范围

[英]index 4 is out of bounds for 4 length

package dsa450;

import java.util.*;

public class countInversion {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of Array");
        int n=sc.nextInt();
        int arr[]=new int[n];
        System.out.println("Enter the size of Array");
        for(int i=0;i<n;i++){
            arr[i]=sc.nextInt();
        }

        int p=arr.length;
        int temp[]=new int[n];
        int ans=mergeSort(arr,temp,0,n-1);
        System.out.println(ans);

    }

    static int mergeSort(int[] arr,int[]temp,int l,int r) {
         int inv=0;
         int mid;
        if (l<r) {
        
            mid=(l+r)/2;
            inv+= mergeSort(arr,temp,l, mid);
            inv+= mergeSort(arr,temp,mid+1,r);
            inv+= merge(arr,temp,l,mid+1,r);
        }
           return inv;
    }

    private static int merge(int []arr,int []temp,int l,int mid,int r) {
        
           
        int i=0;
        int j=0;
        int k=0;
        int swap=0;
        while(i<=mid-1 && j<=r) {
            if(arr[i]<=arr[j]) {
                temp[k++]=arr[i++];
                
            }
            else {
                temp[k++]=arr[j++];
                swap+=(mid-i);
            }
        }
        while(i<=mid-1) {
            temp[k++]=arr[i++];
            
        }
        
        while(j<=r) {
            temp[k++]=arr[j++];
            
        }
        
        for(i=l;i<=r;i++) {
            arr[i]=temp[i];
        }
        
        return swap;
    }

}

enter image description here在此处输入图像描述

this is my code I am trying to solve it for 5 hours but I am not getting what is the problem can anybody help.这是我的代码,我试图解决它 5 个小时,但我没有得到任何人可以帮助的问题。 I check resources also but the code is the same as a source but it is not running.我也检查资源,但代码与源代码相同,但没有运行。

Hints:提示:

  1. The actual error is that you are attempting to get an array element 1 position beyond the end of the array.实际错误是您试图获取数组元素 1 超出数组末尾的位置。 (Array indexes go from 0 to array.length - 1 .) (数组索引从0array.length - 1 。)

  2. Use the line numbers in the stacktrace to work out exactly which line of your code the exception occurs on.使用堆栈跟踪中的行号来准确计算出异常发生在代码的哪一行。 It is happening in an array indexing operation in the merge method.它发生在merge方法中的数组索引操作中。 There are 6 lines where that could be happening.有 6 行可能发生这种情况。

  3. Add some trace prints to print out the values of critical variables at key points in the merge method;添加一些trace print,打印出merge方法中关键点处关键变量的值; eg what l , r and mid are, and what i , j and k are.例如lrmid是什么,以及ijk是什么。

  4. Run the modified code, look at the values output by the trace prints, and try to visualize what the code is doing.运行修改后的代码,查看跟踪打印输出的值,并尝试可视化代码在做什么。

  5. Spot where and why you are going beyond the end of an array ... and figure out what you need to change.找出超出数组末尾的位置和原因……并找出需要更改的内容。

Your merge method is failing because you are not validating k against size of temp array.您的merge方法失败,因为您没有根据temp数组的大小验证k

I am suggesting below small modifications in your merge method which will give you correct answer:我建议对您的merge方法进行以下小的修改,这将为您提供正确的答案:

while (i <= mid - 1 && j <= r && k<temp.length) {
            if (arr[i] <= arr[j] ) {
                temp[k++] = arr[i++];

            } else {
                temp[k++] = arr[j++];
                swap += (mid - i);
            }
        }
        while (i <= mid - 1 && k < temp.length) {
            temp[k++] = arr[i++];

        }

        while (j <= r && k < temp.length) {
            temp[k++] = arr[j++];

        }

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

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