简体   繁体   English

我的代码抛出ArrayIndexOutOfBoundsException

[英]My code throws an ArrayIndexOutOfBoundsException

I need to write code that will write all negative and positive numbers from array, i did that, other part of code is to write all duplicate numbers and how many times do they repeat , it works, but it also writes error, im new to java, so if anyone can help with my code, thank you 我需要编写代码来写出数组中所有的负数和正数,我这样做了,代码的另一部分是写了所有重复的数,它们重复了多少次,这是可行的,但是它也会写错误, java,所以如果有人可以帮助我的代码,谢谢

   public static void main(String[] args) {
        int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
        int cntPoz = 0, cntNeg = 0;

        //for petlja broji pozitivne i negativne clanove array-a
        for (int a = 0; a < array.length; a++) {
            if (array[a] > 0) {
                cntPoz++;
            } else if (array[a] < 0) {
                cntNeg++;
            }
        }

        // kreiranje novih nizova 
        int[] pArr = new int[cntPoz];
        int[] nArr = new int[cntNeg];
        //for petlja iz array-a premjesta sve pozitivne brojeve
        cntPoz = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > 0) {
                pArr[cntPoz++] = array[i];
            }
        }

        //for petlja iz array-a premjesta sve negativne brojeve
        cntNeg = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] < 0) {
                nArr[cntNeg++] = array[i];
            }
        }

        //sortiranje i ispis nizova
        Arrays.sort(pArr);
        Arrays.sort(nArr);
        System.out.println("Originalni niz je : \n" + java.util.Arrays.toString(array) + "\n");
        System.out.println("Niz pozitivnih brojeva : \n" + java.util.Arrays.toString(pArr) + "\n");
        System.out.println("Niz negativnih brojeva : \n" + java.util.Arrays.toString(nArr));
        System.out.println();
        System.out.println();

        int[] arr2 = new int[array.length];
        Arrays.sort(array);

        for (int i = 0; i < array.length; i++) {
            arr2[i]++;
            if (i + 1 < array.length) {
                if (array[i] == array[i + 1]) {
                    arr2[i]++;
                    i++;
                }
            }
        }
        for (int j = 0; j<array.length; j++){
        for (int i = j+1; i < array.length; i++) {
            if (arr2[i] > 0 && array[i]==array[i+1]) {
                System.out.println("Duplikati su : "+array[i] + " i pojavljuju se " + arr2[i]);
            }
        }
        System.out.println(Arrays.toString(array));
    }

ERROR: 错误:

this is the output : 这是输出:

Originalni niz je : 
[12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87]

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
Niz pozitivnih brojeva : 
[12, 12, 23, 43, 43, 545]

Niz negativnih brojeva : 
[-999, -87, -55, -22, -4]


Duplikati su : 0 i pojavljuju se 2
    at assignment.Assignment.main(Assignment.java:65)
Duplikati su : 12 i pojavljuju se 2
Duplikati su : 43 i pojavljuju se 2
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

I think that the error is here: 我认为错误在这里:

if (arr2[i] > 0 && array[i]==array[i+1]) {

At the last iteration, i+1 exceeds the maximum dimension of array . 在最后一次迭代中,i + 1超出了array的最大尺寸。

Maybe you want to stop the cycle at the previous iteration in order to allow the final comparison, like this: 也许您想在上一次迭代时停止循环,以便进行最终比较,如下所示:

for (int i = j+1; i < array.length - 1; i++) {

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

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