Advertisement
dm6801

20171116 - 05 - 03

Nov 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. package myApp;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class class05_03 {
  6.     /*public final static int SIZE = 5;*/
  7.    
  8.     //isIn() method, accepts 2 args: 1st int, 2nd int array
  9.     //if the method locates the number inside the array, return true, otherwise return false
  10.     public static boolean isIn(int number, int arr[]) {
  11.         for (int test: arr) {
  12.             if (test == number) { return true; }
  13.         }
  14.        
  15.         return false;
  16.     }
  17.    
  18.     //newArr() method, accepts 2 args: 1st int array, 2nd int array
  19.     public static int[] newArr(int arr1[], int arr2[]) {
  20.         /*int newArr[] = new int[SIZE];*/
  21.        
  22.         int newArr[] = new int[arr1.length];
  23.         int newIndex = 0;
  24.        
  25.         //loop through arr1
  26.         for (int number1: arr1) {
  27.            
  28.             //for each iteration of arr1, loop through arr2
  29.             for (int number2: arr2) {
  30.                
  31.                 //compare systematically every item from arr1, with every item from arr2
  32.                 if (number1 == number2 && !(isIn(number1, newArr))) {
  33.                    
  34.                     //if condition holds true,
  35.                     //update new array with item, and raise new array's index count by 1
  36.                    
  37.                     newArr[newIndex] = number1;
  38.                     newIndex += 1;
  39.                 }
  40.             }
  41.         }
  42.        
  43.         return newArr;
  44.     }
  45.    
  46.     public static void main(String[] args) {
  47.        
  48.         //init vars
  49.         final int SIZE = 5;
  50.         int[]   arr1 = new int[SIZE],
  51.                 arr2 = new int[SIZE],
  52.                 arr3;
  53.        
  54.         //open in_stream
  55.         Scanner s = new Scanner(System.in);
  56.        
  57.         //populate array #1
  58.         System.out.printf("Enter %d numbers: ", arr1.length);
  59.         for (int i=0; i<arr1.length; i+=1) {
  60.             arr1[i] = s.nextInt();
  61.         }
  62.         s.nextLine(); //eat 'Enter', disregard it
  63.        
  64.         //populate array #2
  65.         System.out.printf("Enter %d numbers: ", arr2.length);
  66.         for (int i=0; i<arr2.length; i+=1) {
  67.             arr2[i] = s.nextInt();
  68.         }
  69.         s.nextLine();
  70.         s.close(); //close in_stream
  71.        
  72.         //assemble array #3
  73.         arr3 = newArr(arr1, arr2);
  74.        
  75.         //print array #3
  76.         System.out.print("New array: ");
  77.         for (int number: arr3) {
  78.             System.out.printf("%d - ", number);
  79.         }
  80.        
  81.     }
  82.    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement