ArrayIndexOutOfBound问题

时间:2016-01-08 13:59:34

标签: java performance

我必须从给定的数组中找到重复的值。我还必须打印不重复的值,我不能这样做,因为它给了我一个ArrayIndexOutOfBoundException。我抓住了这个例外。我是否会因不正确的关闭或内存泄漏而遇到任何性能问题?我的代码如下,

import org.apache.commons.lang.ArrayUtils;
public class Duplicates {

    public static void main(String[] args) {
        Duplicates.duplicate();
    }

    public static void duplicate() {
        // int array[]={1,22,32,66,56,754,22,0,66,55,83,21,67,45,99,666,85,37};
        int[] array = { 2, 4, 2, 5, 4, 7, 9, 2, 3, 3 };
        int tempArray[] = array;

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

                try {

                    //this part will print the duplicate values
                    if ((array[i] == (array[j])) && (i != j) && (i < j)) {
                        System.out.println("Duplicates values are: " + array[i]);

                        /* this forloop below is for removing duplicate number from
                           the entire array and gives a fresh array without              
                           that particular duplicate value */

                        int removeVal = array[i];
                        for (int k = i; k < array.length; k++) {
                            tempArray = ArrayUtils.removeElement(tempArray, removeVal);
                        }

                        j = 0;
                    } 
                      /* This part will print values not duplicate, but need to   
                      print like { 5,7,9 } together */
                    else {
                        if (i < j) {
                            j++;
                        } else {
                            j = i + 1;
                        }
                    }
                    array = tempArray;
                }

                catch (ArrayIndexOutOfBoundsException ae) {

                    ae.getMessage();
                }
            }
        }
    }
}

那么我在这里做错了什么,不能一起打印并给出ArrayIndexOutOfBoundException?但是另一个数组(大数组)注释掉它没有给出ArrayIndexOutOfBoundException。

2 个答案:

答案 0 :(得分:1)

这个for循环:

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

导致无限循环尝试将其替换为:

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

答案 1 :(得分:1)

我认为不正确的尝试...抓住阻止是原因。现在程序正在终止。这里是代码,

 import org.apache.commons.lang.ArrayUtils;
 public class Duplicates {

public static void main(String[] args) {
    Duplicates.duplicate();
}

public static void duplicate() {
    // int array[]={1,22,32,66,56,754,22,0,66,55,83,21,67,45,99,666,85,37};
    int[] array = { 2, 4, 2, 5, 4, 7, 9, 2, 3, 3 };
    int tempArray[] = array;
    try{
    for (int i = 0; i < array.length - 1; i++) {
        for (int j = 1; j < array.length;) {

                   //this part will print the duplicate values
                if ((array[i] == (array[j])) && (i != j) && (i < j)) {
                   System.out.println("Duplicates values are: " + array[i]);

                 /* this forloop below is for removing duplicate number from
                       the entire array and gives a fresh array without              
                       that particular duplicate value */

                    int removeVal = array[i];
                    for (int k = i; k < array.length; k++) {
               tempArray = ArrayUtils.removeElement(tempArray,  removeVal);
                    }

                    j = 0;
                } 
                  /* This part will print values not duplicate, but need to   
                  print like { 5,7,9 } together */
                else {
                    if (i < j) {
                        j++;
                    } else {
                        j = i + 1;
                    }
                }
               array = tempArray;

            }
            System.out.println("values are not duplicate are" + array[i]);
        }

        } catch (ArrayIndexOutOfBoundsException ae) {

        ae.getMessage();
       }

       }

       }