Write a complete java program that reads in 10 numbers into
Write a complete java program that reads in 10 numbers into array 1 and 10 numbers into array 2. Then call a method passing the two arrays. The method called should create a third array that has in each slot the larger of the two numbers in the corresponding slots in array 1 and array 2. Your method should return this array. The calling method should then display the values in the returned array. For the first array, enter the numbers 1, 2 ,..., 10. For the second array, enter the numbers 10, 9,..., 1 (using the keyboard)
Create the code using ArrayLists as well.
Solution
With Arrays
import java.util.Scanner; //importing scanner to input numbers
 class Arrays{
     static int[] createThirdArray(int[] firstArray, int[] secondArray){ //method that creates third array
         int thirdArray[] = new int[10]; //creating the third array
         for (int i=0; i<10; i++){ //iterating through all elements of first and second array
             if (firstArray[i] >= secondArray[i]){
                 thirdArray[i] = firstArray[i]; //setting value to greater value
             }
             else{
                 thirdArray[i] = secondArray[i];
             }
         }
         return thirdArray; //returning the third array
     }
     public static void main(String[] args) {
        //declaring arrays
         int firstArray[] = new int[10];
         int secondArray[] = new int[10];
         int thirdArray[] = new int[10];
        System.out.println(\"Enter the numbers for first array\");
         Scanner reader = new Scanner(System.in); // Reading from System.in
         for (int i=0; i<10; i++){
             System.out.println(\"Enter a number: \");
             int n = reader.nextInt();
             firstArray[i] = n;
         }
        System.out.println(\"Enter the numbers for second array\");
         for (int i=0; i<10; i++){
             System.out.println(\"Enter a number: \");
             int n = reader.nextInt();
             secondArray[i] = n;
         }
        thirdArray = createThirdArray(firstArray, secondArray);
         for (int elem: thirdArray){
             System.out.println(elem+\", \");
         }
     }
 }
With ArrayLists
import java.util.Scanner;
 import java.util.ArrayList;
 class ArrayLists{
     static ArrayList<Integer> createThirdArray(ArrayList<Integer> firstArray, ArrayList<Integer> secondArray){
         ArrayList<Integer> thirdArray = new ArrayList<Integer>();
         for (int i=0; i<10; i++){
             if (firstArray.get(i) >= secondArray.get(i)){
                 thirdArray.add(firstArray.get(i));
             }
             else{
                 thirdArray.add(secondArray.get(i));
             }
         }
         return thirdArray;
     }
     public static void main(String[] args) {
         ArrayList<Integer> firstArray = new ArrayList<Integer>();
         ArrayList<Integer> secondArray = new ArrayList<Integer>();
         ArrayList<Integer> thirdArray = new ArrayList<Integer>();
         System.out.println(\"Enter the numbers for first array\");
         Scanner reader = new Scanner(System.in); // Reading from System.in
         for (int i=0; i<10; i++){
             System.out.println(\"Enter a number: \");
             int n = reader.nextInt();
             firstArray.add(n);
         }
        System.out.println(\"Enter the numbers for second array\");
         for (int i=0; i<10; i++){
             System.out.println(\"Enter a number: \");
             int n = reader.nextInt();
             secondArray.add(n);
         }
        thirdArray = createThirdArray(firstArray, secondArray);
         for (int elem: thirdArray){
             System.out.println(elem+\", \");
         }
     }
 }


