-2

How can i fill Array from Array. Example:

Array Main = {1,2,3,4,5,6,7,8,9,10}

and i want to fill Array 1 and Array 2 like :

Array 1 = {1,3,5,7,9}
Array 2 = {2,4,6,8,10}

I guess i need to use a for-loop on Array Main and push the int values to Array 1 and Array 2. but i don't know how to do this in Java Anyone can help me

my code:

import java.util.ArrayList;
import java.util.List;


public class myArray {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Your Prepared data.
        List<int[]> values = new ArrayList<int[]>(2);

        int[] element1 = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        values.add(element1);

        // Add the result to ArrayList.
        List<Integer> temp = new ArrayList<Integer>();
        List<Integer> temp1 = new ArrayList<Integer>();


        for(int j=0;j<values.size(); j++) {
            for (int i = 0; i < values.get(0).length; i++) {
                if (values.get(0)[i] < 2 ){
                    temp.add(values.get(0)[i]);
                } else {
                    temp1.add(values.get(0)[i]);

                }
            }
        }

         System.out.println(temp);
         System.out.println(temp1);
    }
}
Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
A3_Star
  • 11
  • 1
  • 3
  • 1
    It seems you don't know how to determine if a number is odd. Check out the answers to this question: https://stackoverflow.com/questions/7342237/check-whether-number-is-even-or-odd – Erwin Bolwidt Jun 25 '17 at 10:59
  • You use System.arrayCopy()... Unless this is homework. – GhostCat Jun 25 '17 at 11:03
  • This question does not make clear whether you are trying to fill the first result array with the values which have an odd index in the source array, or simply the values which are themselves odd. – Bobulous Jun 25 '17 at 11:07

2 Answers2

0

This is a better solution:

import java.util.ArrayList;
import java.util.List;


public class myArray {

    /**
     * @param args
     */
    public static void main(String[] args) {


        int[] values = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };


        // Add the result to ArrayList.
        List<Integer> temp = new ArrayList<Integer>();
        List<Integer> temp1 = new ArrayList<Integer>();


        for(int i=0;i<values.length; i++) {
                if (values[i] % 2 == 0){
                    temp.add(values[i]);
                } else {
                    temp1.add(values[i]);
                }
        }

         System.out.println(temp);
         System.out.println(temp1);
    }
}
Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
  • ow yes thanks this is what i was looking for (values.get(0)[i] % 2 == 0) – A3_Star Jun 25 '17 at 10:59
  • Wait, I realise now that the question is unclear: are you trying to collect odd-index values into the first result array, or simply odd values into the first result array? – Bobulous Jun 25 '17 at 11:03
  • no this is an example for me cuz what i need to do in really is: filling 5 array for example : array 1= {1,5,9.....} array 2 = {2,6.....} array 3 ={3,7.....} array 4 ={4,8.......} something like this . – A3_Star Jun 25 '17 at 11:23
-1

I Think This is a Better Solution

import java.awt.List; import java.util.ArrayList;

public class myArray {

    public static void main(String[] args) {
        // Your Prepared data.
        ArrayList<int[]> values = new ArrayList<int[]>(2);

        int[] element1 = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        values.add(element1);

        // Add the result to ArrayList.
        ArrayList<Integer> temp = new ArrayList<Integer>();
        ArrayList<Integer> temp1 = new ArrayList<Integer>();


        for(int j=0;j<values.size(); j++) {
            for (int i = 0; i < values.get(0).length; i++) {
                if (values.get(0)[i] % 2 == 0 ){
                    temp.add(values.get(0)[i]);
                } else {
                    temp1.add(values.get(0)[i]);

                }
            }
        }

         System.out.println(temp);
         System.out.println(temp1);
    }

}

Malinda
  • 524
  • 2
  • 5
  • 11
  • And can you explain why wrapping the int array with an ArrayList would make any sense? And how that is even a better solution? – Tom Jun 25 '17 at 15:31