0

I need to sort even and odds using array list

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class SortedEvensAndOdds {

public static void main(String[] args) {
    Random rnd = new Random();
    ArrayList<Integer> evenPrint = new ArrayList<Integer>();
    ArrayList<Integer> oddPrint = new ArrayList<Integer>();
    int odd[] = new int[25];
    int numOdd = 0;
    int even [] = new int[25];
    int numEven = 0;
    for (int i=0;i<25;i++) {
        int num = rnd.nextInt((100 - 1) + 1) + 1;
        if (num%2 ==0) {
            even[numEven] = num;
            numEven++;
        } 
        else {
            odd[numOdd] = num;
            numOdd++;
        }
    }
    for (int i = 0; i < even.length; i++)
        evenPrint.add(even[i]);
    for (int i = 0; i < even.length; i++)
        oddPrint.add(odd[i]);
    Collections.sort(evenPrint);
    Collections.sort(oddPrint);
    System.out.println("Even:");
    for (int i=0;i<numEven;i++) 
        System.out.print(evenPrint.get(i)  + " ");
    System.out.println("\nOdd:");
    for (int i=0;i<numOdd;i++) 
        System.out.print(oddPrint.get(i)  + " ");


}
}

this is my output

Even:
0 0 0 0 0 0 0 0 0 6 6 14 28 36 38 54  
Odd:
0 0 0 0 0 0 0 0 0 

How do i prevent all the 0's

btw, if i take out the "Collections.sort()" method it will be

Even:
16 32 22 54 90 70 50 60 40 12 60 78 86 52 
Odd:
59 35 53 35 87 67 75 33 75 59 87 

so this tells me that somthing is going wrong with the sorting please help

3 Answers 3

2

When you create a array like this: int odd[] = new int[25]; what you gets it's a array of zeros, 25 of them. That's because int cannot be null.

So when you sort all the zeros at the end of your array are moved to the beginning of the array fe. before 1,5,9,13,19,0,0,0,0 was sorted to 0,0,0,0,1,5,9,13,19 and because you print only to index where you know that you assiged number there

for (int i=0;i<numEven;i++) 
    System.out.print(evenPrint.get(i)  + " ");

Notice numEven. It is not printing whole array. To print whole array you should rather use:

for (int i = 0; i < evenPrint.size(); i++)
    System.out.print(evenPrint.get(i) + " ");

You will get something like this:

0 0 0 0 0 0 0 0 0 0 0 0 10 32 32 38 40 50 60 64 70 74 82 88 96

How to prevent this?

Don't put every single element of array to list. Change:

for (int i = 0; i < even.length; i++)
    evenPrint.add(even[i]);
for (int i = 0; i < even.length; i++)
    oddPrint.add(odd[i]);

to:

for (int i = 0; i < numEven; i++)
    evenPrint.add(even[i]);
for (int i = 0; i < numOdd; i++)
    oddPrint.add(odd[i]);
Sign up to request clarification or add additional context in comments.

Comments

1

The 2 loops for (int i = 0; i < even.length; i++): instead of using even.length you should use numEven and numOdd, as you want to add just the quantity of even and odd numbers you've found. This will get rid of zeroes (when you initialize odd and even arrays, they're filled with zeroes, which you are adding to evenPrint and evenOdd arrays). When you sort, all the zeroes go to the beginning, which doesn't happen when you don't sort.

Comments

1

If you do not use the int odd[] and int even[] arrays elsewhere in your code, than you can get rid of those mystical zeros by omitting the arrays totally. You will get shorter - witch is always nice - and more readable code.

So if it is possible I would add the randomly generated numbers to the lists directly.

public static void main(String[] args) {
    Random rnd = new Random();

    ArrayList<Integer> evenPrint = new ArrayList<Integer>();
    ArrayList<Integer> oddPrint = new ArrayList<Integer>();

    for (int i = 0; i < 25; i++) {
        int num = rnd.nextInt((100 - 1) + 1) + 1;

        if (num % 2 == 0) {
            evenPrint.add(num);
        } 
        else {
            oddPrint.add(num);
        }
    }

    Collections.sort(evenPrint);
    Collections.sort(oddPrint);

    System.out.println("Even:");
    evenPrint.forEach(e -> System.out.print(e + " "));

    System.out.println("\nOdd:");
    oddPrint.forEach(e -> System.out.print(e + " "));
}

Notice: I used lambda expressions at the printing section for shortening.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.