Results 1 to 18 of 18
  1. #1
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default 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.

    Java 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!

    Moderator edit: code tags added
    Last edited by Fubarable; 01-23-2011 at 03:49 AM. Reason: Moderator edit: code tags added

  2. #2
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Smile Make use of TreeSet

    This link will tell you about Collections interfaces.
    tutorial collection map java

    Declare your new arrays like shown below

    Java 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
    Java Code:
    evens.add(numbers[i]);
    Satisfied?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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!

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  6. #6
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Java 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. :)

  7. #7
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Java 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

  8. #8
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    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
    Java 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

    Java 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

    Java 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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Sly Cooper View Post
    Output:

    Java 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:


    Java Code:
    System.out.println(java.util.Arrays.toString(myArray));

  10. #10
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Try this code. It'll work except that you get some unwanted zeros at the end of evens and odds array.

    Java 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?

  11. #11
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    I tried everything that you said and it seems to work, however is there any way I can get it to remove these zeros?

    Java 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?
    Last edited by Sly Cooper; 01-23-2011 at 07:20 PM.

  12. #12
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    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?
    Last edited by Sly Cooper; 01-23-2011 at 09:32 PM.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    This
    Java 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
    Java 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.

    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.

  14. #14
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Thank you very much Junky.

    Java 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.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  16. #16
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    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.

    Java 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.)

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  18. #18
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    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.

    Java 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.
    Last edited by Sly Cooper; 01-24-2011 at 06:18 AM.

Similar Threads

  1. passing data between threads
    By abdullahansari in forum New To Java
    Replies: 2
    Last Post: 01-14-2011, 08:39 PM
  2. Passing data between dialogs
    By Wintery in forum New To Java
    Replies: 7
    Last Post: 01-02-2009, 05:35 AM
  3. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:19 AM
  4. Passing data from one JFrame to another
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 05:39 AM
  5. passing data dynamically
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •