0

I try to sorting a double array.An odd element should ascending order.An even element should descending.My classes is as follow. When I runned project I show

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

package com.fatih.sortarray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SortingArray {

public static Double[] sortItOut(Double[] array) {

    Double[] arrayOdd = {};
    Double[] arrayEven = {};
    Double[] sortedArray = {};

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

        int k = 0;
        int j = 0;

        if (array[i] % 2 == 0) {
            arrayOdd[k] = array[i];
            k++;
        } else {
            arrayEven[j] = array[i];
            j++;
        }

    }

    Arrays.sort(arrayOdd, Collections.reverseOrder());
    Arrays.sort(arrayEven);

    List list = new ArrayList(Arrays.asList(arrayOdd));
    list.addAll(Arrays.asList(arrayEven));
    sortedArray = (Double[]) list.toArray();

    return sortedArray;
}

}

....

public class SortingArrayTestt {

public static void main(String[] args) {

    Double[] array = { 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d };

    Double[] sortingArray = SortingArray.sortItOut(array);

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

}

}

...

The console output is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com.fatih.sortarray.SortingArray.sortItOut(SortingArray.java:25)
at com.fatih.sortarray.SortingArrayTestt.main(SortingArrayTestt.java:9)

How can I fix this error? Thanks for everything.

Fatih Svk
  • 41
  • 1
  • 3
  • 9

3 Answers3

0

All of the array you are initializing are of size 0. initialize the arrays like following.

Double[] sortedArray = new Double[array.length];
stinepike
  • 54,068
  • 14
  • 92
  • 112
0

Shorthand array initializers (example like {1, 2, 3, 4}) automatically calculate the size of the array depending upon the elements that you have placed, but you did not place any elements in arrayOdd, arrayEven arrays, so it will be of zero size.

In short, you have created arrayOdd, arrayEven arrays with zero size and you are trying place/access the array elements(of that 0 sized array) which is causing the ArrayIndexOutOfBoundsException, so you need to create the array with size as shown below:

Double[] arrayOdd = new Double[array.length];
Double[] arrayEven = new Double[array.length];
Double[] sortedArray = new Double[array.length];
Vasu
  • 21,832
  • 11
  • 51
  • 67
0

I think you should try this.

public class Main {
  public static void main(String[] args) {
    Double[] array = { 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d };

    List sortingArray = SortingArray.sortItOut(array);

    for (Object item : sortingArray) {
        System.out.print(item);
    }
 }

}

class SortingArray {

public static List sortItOut(Double[] array) {

    ArrayList<Double> arrayOdd = new ArrayList<>();
    ArrayList<Double> arrayEven = new ArrayList<>();
    ArrayList<Double> sortedArray = new ArrayList<>();


    for(Double item : array){
        if(item % 2 == 0){
            arrayOdd.add(item);
        }else{
            arrayEven.add(item);
        }
    }



    Arrays.sort(new ArrayList[]{arrayOdd}, Collections.reverseOrder());
    Arrays.sort(new ArrayList[]{arrayEven});

    ArrayList list = new ArrayList(Arrays.asList(arrayOdd));
    list.addAll(Arrays.asList(arrayEven));
    sortedArray = list;

    return sortedArray;
}

}