• Post Reply Bookmark Topic Watch Topic
  • New Topic

sort even/odd integers of an array into their own arrays  RSS feed

 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

So I was able to store 50 random integers in the range of 1-10 in an array (thanks to a previous post). Now, I'm trying to call class methods that will sort the odd and even numbers of the array into arrays of their own. The problem is that I don't know the size of each array because I don't know how many even & odd numbers were in the original array, as the numbers were randomly generated.


I get the following output that repeats for 25 lines:
[I@addbf1
[I@42e816

As you can see, I'm really stumped as to how to extract the even & odd integers of the first array and sort them into separate arrays. My code may look really convoluted. Wrong approach? Fundamental misunderstanding of arrays? Any help is appreciated. Thanks.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't just print an array like that. What you really want to do is print each element in the array. it is not too hard to write a method that takes an array as an argument, loops through it, and prints each element, if that is waht you want.
 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:you can't just print an array like that. What you really want to do is print each element in the array. it is not too hard to write a method that takes an array as an argument, loops through it, and prints each element, if that is waht you want.


Thanks for pointing that out. What I want is to take those elements, differentiate between the odd and even numbers, and sort those numbers into arrays of their own (an array of odds and an array of evens). I'm doing something fundamentally wrong in the sortEvenOdd() method, but I don't know what it is.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, for starters...

your method should be array-size neutral. what would happen if you pass in an array of length 100? or 10? All arrays have a length instance variable. So I would say that your for loop should be

for (int k = 0; k < a.length;, k++)

second...why do you make each of the even and odd arrays 25? Do you know you will have an even dispersion? What would happen if your array of random numbers just happens to be all even? unlikely yes, but possible. I think you need to define each even/odd array to be the same size as the input array, just to be safe.

Your method name choice of "sortEvenOdd" is confusing. It is not sorting anything.

Your two arrays of even and odd disappear once you exit your method. That may be what you want, but it may not.

As you loop through array 'a', and get to let's say the 10th element, you insert it into your even/odd array at the same index. So if the first 9 elements of a were odd, you'd insert the 10th element of 'a' into the 10th element of evenArray, and the fist 9 would be empty. Is that what you want? Or do you need two new indexes, one for storing where the next spot in evenArray is, and one for oddArray?
 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made some changes. For some reason I'm getting an output of [i@addbf1 that repeats 25 times and [I@42e816 that also repeats 25 times (not italicized). Why am I getting these weird outputs instead of even and odd numbers? Sorry, I'm still in the process of understanding arrays.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I told you already. You can't do this and get what you want:

What you are saying here is "print the name of the array". What you need to do is print the elements of the array, one at a time. You can either write a method to do so, or use your loop's index to print the current element as you insert it.
 
Khair Bahir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your help. I've got it to work.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!