1

I'm trying to figure out how to sort a list of input in one array, and make two different arrays out of that, either into even or odd numbers. I can't seem to add the integers to the array in the if-loop.

Here is my code:

 Scanner in = new Scanner (System.in);

System.out.print("Enter a number");
    int number = in.nextInt();


 int [] x = new int[0];
 int [] even = new int [0];
 int [] odd = new int [0];



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

    if (in.nextInt() == 0){
        for (i = 0; i <x.length; i++)
        {
            if (x[i] % 2 == 0)
            {
            even = even + x[i];
            System.out.print("Even numbers = " + even);
            i++;
            }
            if (x[i] % 2 != 0)
            {
            odd = odd + x[i];
            System.out.print("Odd numbers = " + odd);
            i++;
            }
        }
        break;
                }

    else {
        x[i] = number;
        i++;
        }

}
0

4 Answers 4

2

Arrays are fixed size in Java. They don't grow dynamically. Use an ArrayList if you want an array-like container that can grow and shrink.

List<Integer> even = new ArrayList<Integer>();
List<Integer> odd  = new ArrayList<Integer>();

if (...)
{
    even.add(number);
}
else
{
    odd.add(number);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You aren't using arrays correctly. I'm assuming this is homework designed to teach you how to use them. Try this tutorial:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Good luck!

Comments

0

I would not use three arrays to do this. Simply traverse through the list and have two loop variables that reference the even and odd indexes.Something like this:

int evenIndex=0,oddIndex=1;
int evenSum=0,OddSum=0;
int [] x=new int[10];

 while(evenIndex <= x.length && oddIndex <= x.length)
        {
          evenSum+=x[evenIndex];
           oddSum+=x[oddIndex];


             evenIndex+=2;
             oddIndex+=2 

            }

2 Comments

This is not what the OP is asking for. The OP wants to seperate the elements that are themselves even or odd, not add the elements at even and odd indicies. Plus, I'm not 100% sure, but your solution will always throw an ArrayIndexOutOfBoundsException at either the 7th or 8th line, depending on if the original array has an even or odd number of elements.
I thought that even = even + x[i]; and odd = odd + x[i]; in the code suggests that the OP wants to sum them up ? Anyway my bad if it did'nt serve the purpose.
0

Two good solutions:

int [] x = new int[0];
LinkedList<Integer> even = new LinkedList<Integer>(),
                     odd = new LinkedList<Integer>();

for(int num: x)
    if (x & 1 == 1)
        odd.add(num);
    else
        even.add(num);

The other option is to iterate through, counting how many evens and odds there are, allocating arrays of the correct size, and then putting the numbers into the arrays. That is also an O(n) solution.

4 Comments

Also, if you need the results as arrays, just use even.toArray() or the same for odd.
An ArrayList would not be O(n^2). Adding an item to an ArrayList takes O(1) amortized time, so adding n elements is O(n) amortized.
@JohnKugelman Actually you are right that it isn't O(n^2), come to think of it. ArrayLists grow themselves by doubling, not incrementing, so the average case time for the way you have it is actually O(n log n), I think, because add actually takes O(n) for log(n) of the n times we need to add something. Thus, each add really takes O(log n). Doing that n times is clearly then O(n log n). However, if you give the ArrayLists initial capacity parameters that ensure they will never have to be resized, you can do it in O(n) average case, although at that point you aren't saving any extra RAM.
@AJMansfield If n is the total number of items, add doesn't take O(n) when it has to do a resize. It takes O(k) where k is the size of the array at that time. With a capacity-doubling implementation k is a series of doubling numbers, the sum of which is approximately 2n. That means these intermediate resizes take O(2n). Of course mathematically speaking O(2n) = O(n), so there we are: adding n elements to an ArrayList takes O(n) amortized time. "Amortized" accounts for the occasional resize; if you average them all together you still end up with O(n).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.