索引越界Java合并排序?

时间:2018-12-12 16:47:29

标签: java arrays sorting indexoutofboundsexception mergesort

我正在研究一种合并排序算法。以下是我到目前为止所写的内容。问题是,当我尝试运行它以查看其是否正常运行时,我在用 注释 标记的if语句上出现了索引超出范围错误。

为什么我在旁边的注释行中超出了索引范围?

我知道错误的含义,但无法解释其原因。

public class MergeSort {

    public static int mergeSort(int[] list1, int[] list2) {
        int[] list3 = new int[list1.length + list2.length];
        int smallest1 = list1[0];
        int smallest2 = list2[0];
        int position1 = 0;
        int position2 = 0;

        for (int i = 0; i<list1.length + list2.length; i++) {
                for (int j =0; j<= list1.length; j++) {
                    if (list1[j] < smallest1) { //here index out of bouds
                        smallest1 = list1[j];
                        System.out.print(list1[j]+"smallest value");
                        position1 = j;
                    }
                }
                for (int l =0; l<= list2.length; l++) {
                    if (list2[l] < smallest2) {
                        smallest2 = list2[l];
                        System.out.print(list2[l]+"smallest value");
                        position2 = l;
                    }
                }

                if (smallest1< smallest2) {
                    list3[i] = smallest1;
                } else if (smallest2< smallest1) {
                    list3[i] = smallest2;
                }
        }
        //print the array
        for (int l =0; l<= list2.length; l++) {
            System.out.print("new array 3" + list3[l]);
        }
        return 1;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[] list1 = {17, 22, 35, 42, 60};
        int[] list2 = {9, 14, 66};
        int[] list3;

        mergeSort(list1, list2);
    }

}   

2 个答案:

答案 0 :(得分:2)

索引从0开始运行,这意味着当list1.length = 5时,索引可以是0到4
更改

{
  "name": "AppInsights",
  "type": "Microsoft.Resources/deployments",
  "apiVersion": "2016-09-01",
  "dependsOn": [],
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "[variables('AppInsightsTemplatePath')]",
      "contentVersion": "1.0.0.0"
    },
    "parameters": {
      "tagValues": {
        "value": "[parameters('tagValues')]"
      },
      "workspaceId": {
        "value": "[parameters('workspaceId')]"
      },
      "appInsightsName": {
        "value": "[variables('appInsightsName')]"
      }
    }
  }
 },

{
  "name": "WebApp",
  "type": "Microsoft.Resources/deployments",
  "apiVersion": "2016-09-01",
  "dependsOn": [
    "AppInsights",
    "AppServicePlan"
  ],
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "[variables('WebAppTemplatePath')]",
      "contentVersion": "1.0.0.0"
    },
    "parameters": {
      "siteConfig": {
        "value": {
          "netFrameworkVersion": "v4.7",
          "phpVersion": "",
          "pythonVersion": "",
          "javaVersion": "",
          "nodeVersion": "",
          "linuxFxVersion": "",
          "use32BitWorkerProcess": "False",
          "webSocketsEnabled": "False",
          "alwaysOn": "True",
          "managedPipelineMode": "Integrated",
          "remoteDebuggingEnabled": "False",
          "appSettings": [
            {
              "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
              "value": "[reference(resourceId('Microsoft.Insights/components', variables('appInsightsName')),'2014-04-01').InstrumentationKey]"
            }
          ],
          "connectionStrings": [],
          "defaultDocuments": [],
          "handlerMappings": [],
          "virtualApplications": [
            {
              "virtualPath": "/",
              "physicalPath": "site\\wwwroot",
              "preloadEnabled": "True",
              "virtualDirectories": ""
            }
          ],
          "minTlsVersion": "1.2"
        }
      }
    }
  }
},

答案 1 :(得分:0)

  • 这是因为您的for循环条件是<=。尝试使用<