Piotr Pawlik
Piotr Pawlik

Reputation: 31

I don't know how assign new value to the empty array

I am a beginner in coding. I have to write a code that will divide array with random numbers into two different arrays. One array will contain odd numbers, the other one even numbers. But something is wrong, and i don't really know what to do.

According to the console the problem is in the place where there is a lot of exclamation marks. when i change those lines to System.out.println("x") it works perfectly fine.

public void P_N () {
    int I_E = 0; // amount of even numbers
    int I_O = 0; // amount of odd numbers
    for (int i = 0; i < tab2.length; i++) { // tab2 is a array with random numbers
        if (tab2[i] % 2 == 0)   
            I_E = I_E + 1;
        else
            I_O = I_O+1;
    }
    int [] tab_E = new int[I_E]; // array with even numbers
    int [] tab_O = new int [I_O]; // array with odd numbers
    for (int i = 0; i < tab2.length; i++){
        if (tab2[i] % 2 == 0){
             tab_E[i] = tab2[i]; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        }
    }
    for (int i = 0; i < tab2.length; i++){
        if (tab2[i] % 2 != 0){
            tab_O[i] = tab2[i]; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        }
    }
    for (int i = 0; i< tab_E.length; i++) {
        System.out.println("Even array: " + tab_E[i]);
        System.out.println("------------------------------------------------");
    }
    for (int i = 0; i< tab_O.length; i++) {
        System.out.println("Odd array: " + tab_O[i]);
    }
}

Upvotes: 2

Views: 72

Answers (3)

Shivang Agarwal
Shivang Agarwal

Reputation: 1923

I would rather use 2 ArrayLists one for even numbers and another one is for odd numbers and later convert it into array using toArray() method.

public void P_N(){
    ArrayList<Integer> evenNumberList = new ArrayList<Integer>();
    ArrayList<Integer> oddNumberList = new ArrayList<Integer>();
    for (int i = 0; i < tab2.length; i++) { // tab2 is a array with random numbers
        if (tab2[i] % 2 == 0) {  
            evenNumberList.add(tab2[i]);
        } else {
            oddNumberList.add(tab2[i]);
        }   
    }
    int[] evenNumberArray = evenNumberList.toArray();
    int[] oddNumberArray = oddNumberList.toArray();
}   

This will take some extra space but makes your application more efficient, I hope this helps.

Upvotes: 1

Artyom Rebrov
Artyom Rebrov

Reputation: 691

You have initialized the even/odd number arrays with a quantity of the even/odd numbers accordingly:

int [] tab_E = new int[I_E]; // array with even numbers
int [] tab_O = new int [I_O]; // array with odd numbers

Ii is reasonable to assume that the sizes of even or odd number arrays are might be much smaller than the size of the original source array.

But in this even number filtering loop (as well as in the odd filtering loop) you use source array index values to address target array positions, end eventually face the ArrayIndexOutOfBoundsException.

for (int i = 0; i < tab2.length; i++)
{
    if (tab2[i] % 2 == 0)
    {
         tab_E[i] = tab2[i]; //here the same i value is used to address non existing index in tab_E array
    }
}

A quick fix might be the following:

 int tab_E_index = 0;
 for (int i = 0; i < tab2.length; i++){
    if (tab2[i] % 2 == 0){
         tab_E[tab_E_index] = tab2[i]; //i value gets incremented every loop iteration
         tab_E_index++;                //tab_E_index value get incremented only when even number is added to the tab_E array
    }
}

Please don't just copy/paste it, but try to understand what caused the issue on the first place. Good luck and happy coding.

Upvotes: 0

user11942935
user11942935

Reputation: 482

Problem is in going out of bounds for arrays tab_E and tab_O, when variable i is more tab_E.length. Just create another variable, for example "j". And iterate throug your array using it. Like I'v written below

int j = 0;
for (int i = 0; i < tab2.length; i++) {
    if (tab2[i] % 2 == 0) {
        tab_E[j++] = tab2[i];
 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
}
j = 0;
for (int i = 0; i < tab2.length; i++) {
    if (tab2[i] % 2 != 0) {
        tab_O[j++] = tab2[i];
//                !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
}

Upvotes: 1

Related Questions