Passing Data From One Array To Another

• 01-23-2011, 02:00 AM
Sly Cooper
Passing Data From One Array To Another
I'm new to java, and I'm creating a program that takes an array with 10 integers and then sorts them into three new arrays of even, odd, and negative numbers, then prints out the three new arrays.

I have code written that sorts the array and prints out one by one each numbers and declares if its even, odd, or negative. However I'm unsure how to pass the data from the original array to each of the new arrays. Here is my code so far.

Code:

```public class Arrays81 { public static void main(String[] args) { int[] numbers = new int[10]; //int[] evens = new int[10]; //int[] odds = new int[10]; //int[] negatives = new int [10]; numbers[0] = -1; numbers[1] = -2; numbers[2] = 5; numbers[3] = 6; numbers[4] = 7; numbers[5] = 8; numbers[6] = 9; numbers[7] = 10; numbers[8] = 11; numbers[9] = 12; for (int i = 0; i < numbers.length; i ++){   if (numbers[i] % 2 == 0 & numbers[i] >= 0){     System.out.println(numbers[i] + " is even.");                           }     else if (numbers[i] % 1 == 0 & numbers[i] >= 0){       System.out.println(numbers[i] + " is odd.");                           }       else{         System.out.println(numbers[i] + " is negative.");           } } } }```
Basically I want to know what line of code to add under each if statement to sort the number into the new array. Thanks!

• 01-23-2011, 04:45 AM
subith86
Make use of TreeSet
tutorial collection map java

Declare your new arrays like shown below

Code:

```TreeSet evens = new TreeSet(); TreeSet odds = new TreeSet(); TreeSet negatives = new TreeSet(); //***** and not like these ****** //int[] evens = new int[10]; //int[] odds = new int[10]; //int[] negatives = new int [10];```
The advatage of using TreeSet is that, first of all you don't have to specify the length. It'll adjust itself when a new element gets added. Secondly it'll automatically sort elements for you (From your query, I suppose you also want a sorted array)

In each if condition you have to add elements as shown below
Code:

`evens.add(numbers[i]);`
Satisfied?
• 01-23-2011, 04:52 AM
Fubarable
Original poster: I've added code tags to your post to help make the posted code retain its formatting and be more readable. To learn how to do this in future posts, please look at the first link in Code Tags in my signature links below.

I second Subith's excellent recommendation to use the more flexible collections rather than arrays if possible. If on the other hand your assignment specifies the requirement that you use arrays, please post the actual text of your assignment so that we can better help you. If this is the case, you may need an index int for each of your three result arrays so as to have a placeholder that tells you where the last item was added in that array and where to add the next item.

Much luck!
• 01-23-2011, 05:25 AM
gcalvin
It seems you want to add the even numbers to your evens[] array, odds to your odds[] array, etc. You will want a separate int index for each array. Declare them when you declare the arrays, and initialize each one to zero. Then increment them with the ++ operator as you add values to the arrays. Give it a try and show us your code.

-Gary-
• 01-23-2011, 05:27 AM
gcalvin
By the way, you don't really want "% 1 == 0" where you have it there. Re-think that code. Also, just because a number is negative doesn't mean it's not even or odd.

-Gary-
• 01-23-2011, 05:29 PM
Sly Cooper
Code:

```import java.util.TreeSet; public class Arrays81 { public static void main(String[] args) { int[] numbers = new int[10]; int size = 0; TreeSet evens = new TreeSet(); TreeSet odds = new TreeSet(); TreeSet negatives = new TreeSet(); numbers[0] = -1; numbers[1] = -2; numbers[2] = 5; numbers[3] = 6; numbers[4] = 7; numbers[5] = 8; numbers[6] = 9; numbers[7] = 10; numbers[8] = 11; numbers[9] = 12; for (int i = 0; i < numbers.length; i ++){   if (numbers[i] % 2 == 0 & numbers[i] >= 0){     evens.add(numbers[i]);                           }     else if (numbers[i] % 1 == 0 & numbers[i] >= 0){       odds.add(numbers[i]);                           }       else{         negatives.add(numbers[i]);           } } System.out.println("Sorted Numbers!"); System.out.println(""); System.out.println("The even numbers are :" +evens); System.out.println(""); System.out.println("The odd numbers are :" +odds); System.out.println(""); System.out.println("The negatives numbers are :" +negatives); } } Output: Sorted Numbers! The even numbers are :[6, 8, 10, 12] The odd numbers are :[5, 7, 9, 11] The negatives numbers are :[-2, -1]```
Thank you very much for the help, everything works great and TreeSets are a breeze to use, just for my understanding I still want to do it with standard arrays as Gcalvin below is helping me do. Thank you very much. :)
• 01-23-2011, 05:55 PM
Sly Cooper
Code:

``` public class ArraysExercise81 { public static void main(String[] args) { int[] numbers = new int[10]; int[] evens = new int[10]; int[] odds = new int[10]; int[] negatives = new int [10]; numbers[0] = -1; numbers[1] = -2; numbers[2] = 5; numbers[3] = 6; numbers[4] = 7; numbers[5] = 8; numbers[6] = 9; numbers[7] = 10; numbers[8] = 11; numbers[9] = 12; evens[0] = 0; evens[1] = 0; evens[2] = 0; evens[3] = 0; evens[4] = 0; evens[5] = 0; evens[6] = 0; evens[7] = 0; evens[8] = 0; evens[9] = 0; odds[0] = 0; odds[0] = 0; odds[0] = 0; odds[0] = 0; odds[0] = 0; odds[0] = 0; odds[0] = 0; odds[0] = 0; odds[0] = 0; odds[0] = 0; negatives[0] = 0; negatives[0] = 0; negatives[0] = 0; negatives[0] = 0; negatives[0] = 0; negatives[0] = 0; negatives[0] = 0; negatives[0] = 0; negatives[0] = 0; negatives[0] = 0; for (int i = 0; i < numbers.length; i ++){   if (numbers[i] % 2 == 0 & numbers[i] >= 0){     numbers[i] = evens[i];                           }     else if (numbers[i] % 1 == 0 & numbers[i] >= 0){         numbers[i] = odds[i];                           }       else if (numbers[i] < 0){           numbers[i] = negatives[i];           }         else {             } } System.out.println("Evens are " + evens); System.out.println("Odds are " + odds); System.out.println("Negatives are " + negatives); } } Output: Evens are [I@3e25a5 Odds are [I@19821f Negatives are [I@addbf1```
I think I understand what you're saying, but my code gives me a crazy output. Just i'm unsure of how to replace the elements in the evens array. I declared 10 of them as 0, and now I need to replace them with the value that numbers[i] returns I believe, I'm still quite new so bare with me. lol
• 01-23-2011, 06:15 PM
subith86
Oh dude, what have you done?
First of all you have to understand that "=" operator will assign whatever is on the right side to the left side. So inside your ifs, it should be something like this
Code:

`evens[i] = numbers[i];`
Secondly it's not a good coding practice to have 10 lines to initialize evens[] array. You have to use for loop like shown below

Code:

```for (int i = 0 ; i < evens.length ; i++) evens[i] = 0;```
Thirdly, you wont get the desired output when you just do a system.out of evens. In this case you are printing the object and not the contents of the object. So here also you need a for loop

Code:

```for (int i = 0 ; i < evens.length ; i++) System.out.println(evens[i]);```
And lastly, I also feel that your logic is wrong. Let me have a look once again
• 01-23-2011, 06:22 PM
Fubarable
Quote:

Originally Posted by Sly Cooper
Output:

Code:

```Evens are [I@3e25a5 Odds are [I@19821f Negatives are [I@addbf1```
I think I understand what you're saying, but my code gives me a crazy output. Just i'm unsure of how to replace the elements in the evens array. I declared 10 of them as 0, and now I need to replace them with the value that numbers[i] returns I believe, I'm still quite new so bare with me. lol

You're seeing the toString() result returned from an array object. To see what is in the array, you'll want to loop through the array printing every item inside the for loop, or you could use Arrays.toString:

Code:

`System.out.println(java.util.Arrays.toString(myArray));`
• 01-23-2011, 06:38 PM
subith86
Try this code. It'll work except that you get some unwanted zeros at the end of evens and odds array.

Code:

```                int[] numbers = new int[10];                 int[] evens = new int[10];                 int[] odds = new int[10];                 int[] negatives = new int [10];                                 int negLength = 0;                 int evenLength = 0;                 int oddsLength = 0;                                 numbers[0] = -1;                 numbers[1] = -2;                 numbers[2] = 5;                 numbers[3] = 6;                 numbers[4] = 7;                 numbers[5] = 8;                 numbers[6] = 9;                 numbers[7] = 10;                 numbers[8] = 11;                 numbers[9] = 12;                                 for (int i = 0; i < numbers.length; i ++) {                         if (numbers[i] < 0) {                                 negatives[negLength++] = numbers[i];                         } else {                                 if ((numbers[i] % 2) == 0) {                                         evens[evenLength++] = numbers[i];                                 } else {                                         odds[oddsLength++] = numbers[i];                                 }                         }                 }                 System.out.println(java.util.Arrays.toString(evens)); //I didn't know this, thanks Fubarable                 System.out.println(java.util.Arrays.toString(odds)); //I didn't know this, thanks Fubarable                 System.out.println(java.util.Arrays.toString(negatives)); //I didn't know this, thanks Fubarable```
Now you don't need to initialize everything to zero. The statement
int[] evens = new int[10];
will create an integer array of length 10 and assigns 0 to each element in the array.

Hope you understood?
• 01-23-2011, 08:18 PM
Sly Cooper
I tried everything that you said and it seems to work, however is there any way I can get it to remove these zeros?

Code:

``` public class ArraysExercise81 { public static void main(String[] args) { int[] numbers = new int[10]; int[] evens = new int[10]; int[] odds = new int[10]; int[] negatives = new int [10]; numbers[0] = -1; numbers[1] = -2; numbers[2] = 5; numbers[3] = 6; numbers[4] = 7; numbers[5] = 8; numbers[6] = 9; numbers[7] = 10; numbers[8] = 11; numbers[9] = 12; for (int i = 0 ; i < evens.length ; i++){ evens[i] = 0; } for (int i = 0 ; i < odds.length ; i++){ evens[i] = 0; } for (int i = 0 ; i < negatives.length ; i++){ evens[i] = 0; } for (int i = 0; i < numbers.length; i ++){   if (numbers[i] % 2 == 0 & numbers[i] >= 0){     evens[i] = numbers[i];                           }     else if (numbers[i] % 1 == 0 & numbers[i] >= 0){         odds[i] = numbers[i];                           }       else if (numbers[i] < 0){           negatives[i] = numbers[i];           }         else {             } } for (int i = 0 ; i < evens.length ; i++){ System.out.println(evens[i]); } for (int i = 0 ; i < odds.length ; i++){ System.out.println(odds[i]); } for (int i = 0 ; i < negatives.length ; i++){ System.out.println(negatives[i]); } } } Output: 0 0 0 6 0 8 0 10 0 12 0 0 5 0 7 0 9 0 11 0 -1 -2 0 0 0 0 0 0 0 0```
I guess it goes through it every time and if it doesnt fall under the if statement it returns a 0?
• 01-23-2011, 08:19 PM
Sly Cooper
Thank you very much fubarable, I understand the code completely, and very much appreciate the help you've given me on the past couple of problems. Is there anyway I can get rid of the zeros from the ends of the statements?
• 01-24-2011, 12:19 AM
Junky
This
Code:

```evens[0] = 0; evens[1] = 0; evens[2] = 0; evens[3] = 0; evens[4] = 0; evens[5] = 0; evens[6] = 0; evens[7] = 0; evens[8] = 0; evens[9] = 0;```
and the replacement
Code:

```for (int i = 0 ; i < evens.length ; i++){ evens[i] = 0; }```
are totally pointless. When you declare an array it will be filled with the default value. For an int array the default value is zero.

Quote:

Is there anyway I can get rid of the zeros from the ends of the statements?
If you had followed the excellent advice and used a counter variable to keep track of where in the array you are upto then you could loop upto this counter instead of the full length of the array.
• 01-24-2011, 03:20 AM
Sly Cooper
Thank you very much Junky.

Code:

```public class ArraysExerciseTest { public static void main(String[] args) {     int[] numbers = new int[10];     int[] evens = new int[10];     int[] odds = new int[10];     int[] negatives = new int [10];     int negLength = 0;     int evenLength = 0;     int oddsLength = 0;     int countEvens = 0;     int countOdds = 0;     int countNegatives = 0;     numbers[0] = -1;     numbers[1] = -2;     numbers[2] = 5;     numbers[3] = 6;     numbers[4] = 7;     numbers[5] = 8;     numbers[6] = 9;     numbers[7] = 10;     numbers[8] = 11;     numbers[9] = 12;     for (int i = 0; i < numbers.length; i ++) {       if (numbers[i] < 0) {         negatives[negLength++] = numbers[i];         countNegatives++;       } else {         if ((numbers[i] % 2) == 0) {           evens[evenLength++] = numbers[i];           countEvens ++;         } else if (oddsLength < odds.length) {           odds[oddsLength++] = numbers[i];           countOdds++;         }             else{                 }       }     }     System.out.println(java.util.Arrays.toString(evens)); //I didn't know this, thanks Fubarable     System.out.println(java.util.Arrays.toString(odds)); //I didn't know this, thanks Fubarable     System.out.println(java.util.Arrays.toString(negatives)); //I didn't know this, thanks Fubarable     System.out.println(countEvens);     System.out.println(countOdds);     System.out.println(countNegatives); } }```
I understood everything except for the counter variable. I now have a counter that counts for each even, odd, and negative. How do I tell the program to stop printing out the numbers after it reaches the value of each of the counters? (i.e When the array evens is equal to evensCounters it stops printing) Which should remove the zero's I think. I'm just not sure how to code that.
• 01-24-2011, 03:57 AM
Junky
You would have to write the code yourself rather than use the Arrays.toString() method. Write a method that takes the array and the matching counter as parameters. Inside the method you have a loop that goes upto the counted and no further. Then call the method 3 times, once for each array and counter.
• 01-24-2011, 04:59 AM
Sly Cooper
Thank you very much for the tips Junky, that's a bit complicated for me, I'm not too great at writing seperate methods at the moment, however I was able to get this far.

Code:

``` public class ArraysExercise81 { public static void main(String[] args) { int[] numbers = new int[10]; int[] evens = new int[10]; int[] odds = new int[10]; int[] negatives = new int [10]; int countEvens = 0; int countOdds = 0; int countNegatives = 0; numbers[0] = -1; numbers[1] = -2; numbers[2] = 5; numbers[3] = 6; numbers[4] = 7; numbers[5] = 8; numbers[6] = 9; numbers[7] = 10; numbers[8] = 11; numbers[9] = 12; for (int i = 0; i < numbers.length; i ++){   if (numbers[i] % 2 == 0 & numbers[i] >= 0){     evens[countEvens] = numbers[i];     countEvens++;                           }   else {       }   if (numbers[i] >= 0 & numbers[i] % 2 != 0){         odds[countOdds] = numbers[i];         countOdds++;                           }   else {       }   if (numbers[i] < 0){           negatives[countNegatives] = numbers[i];           countNegatives++;           }   else {   } } System.out.println("Even numbers are: "); System.out.println("------------------"); System.out.println(""); for (int i = 0; i < evens.length ; i++){ System.out.println(evens[i]); } System.out.println(""); System.out.println("------------------"); System.out.println("Odd numbers are: "); System.out.println("------------------"); System.out.println(""); for (int i = 0 ; i < odds.length ; i++){  System.out.println(odds[i]); } System.out.println(""); System.out.println("------------------"); System.out.println("Negative numbers are: "); System.out.println("------------------"); System.out.println(""); for (int i = 0 ; i < negatives.length ; i++){ System.out.println(negatives[i]); } } } Output: Even numbers are: ------------------ 6 8 10 12 0 0 0 0 0 0 ------------------ Odd numbers are: ------------------ 5 7 9 11 0 0 0 0 0 0 ------------------ Negative numbers are: ------------------ -1 -2 0 0 0 0 0 0 0 0 Process completed.```
Is there anyway I can get those 0's out of there without changing the initial size of each of the evens odds and negatives arrays? (I.e i'd change the size of evens from [10] to [4] so that it wouldnt print the extra 6 0's. I don't want to do that.)
• 01-24-2011, 05:06 AM
Junky
Yes, as I have said three times now, loop upto the counter and not the length of the array. BIG HINT change the condition in the for loop.
• 01-24-2011, 07:07 AM
Sly Cooper
I just took some sleeping medication, so I'm on another planet right now, but thanks Junky and Fubarable, and everyone else who helped. Here is the working code.

Code:

``` public class ArraysExercise81 { public static void main(String[] args) { int[] numbers = new int[10]; int[] evens = new int[10]; int[] odds = new int[10]; int[] negatives = new int [10]; int countEvens = 0; int countOdds = 0; int countNegatives = 0; numbers[0] = -1; numbers[1] = -2; numbers[2] = 5; numbers[3] = 6; numbers[4] = 7; numbers[5] = 8; numbers[6] = 9; numbers[7] = 10; numbers[8] = 11; numbers[9] = 12; for (int i = 0; i < numbers.length; i ++){   if (numbers[i] % 2 == 0 & numbers[i] >= 0){         evens[countEvens] = numbers[i];         countEvens++;                                             }   else {       }   if (numbers[i] >= 0 & numbers[i] % 2 != 0){         odds[countOdds] = numbers[i];         countOdds++;                                             }   else {       }   if (numbers[i] < 0){           negatives[countNegatives] = numbers[i];           countNegatives++;           }   else    {   } } System.out.println("Even numbers are: "); System.out.println("------------------"); System.out.println(""); for (int i = 0; i < countEvens ; i++){ System.out.println(evens[i]); } System.out.println(""); System.out.println("------------------"); System.out.println("Odd numbers are: "); System.out.println("------------------"); System.out.println(""); for (int i = 0 ; i < countOdds ; i++){  System.out.println(odds[i]); } System.out.println(""); System.out.println("------------------"); System.out.println("Negative numbers are: "); System.out.println("------------------"); System.out.println(""); for (int i = 0 ; i < countNegatives ; i++){ System.out.println(negatives[i]); } } } Output: --------------------Configuration: <Default>-------------------- Even numbers are: ------------------ 6 8 10 12 ------------------ Odd numbers are: ------------------ 5 7 9 11 ------------------ Negative numbers are: ------------------ -1 -2 Process completed.```
THANK YOU SO MUCH EVERYONE, and Im sure Ill have loads more questions to come. Thank you for pointing that out 3 times Junky I can be a total idiot when it comes to this stuff.