Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170703;
- import java.util.Random;
- public class functions02 {
- // Functions, exercise 02, slide 35
- // input: two arrays of integer numbers
- // output: array of the common numbers (intersection)
- // building a data sample: two arrays of integers
- // then calling function intersection to calculate
- // and print results
- public static void main(String[] args) {
- // sample data for testing only
- int[] array1 = filledArray(20, 1, 50); // create and fill an array with
- // 20 numbers in the range 1 to
- // 50
- int[] array2 = filledArray(20, 1, 50); // create and fill an array
- // with 20 numbers in the
- // range 1 to 50
- // get intersection array with repeated values not allowsed
- int[] resultArrayNoRepaeat = intersection(array1, array2, false);
- // get intersection array with repeated values not allowsed
- int[] resultArrayRepaeatAllowed = intersection(array1, array2, true);
- // print to screen
- System.out.printf("first array : %s\n", toString(array1));
- System.out.printf("second array : %s\n", toString(array2));
- System.out.printf("\nrepeated values are allowed intersection : %s \n",
- toString(resultArrayRepaeatAllowed));
- System.out.printf("\nrepeated values not allowed intersection : %s \n",
- toString(resultArrayNoRepaeat));
- }
- // create and fill an array with random integer numbers
- public static int[] filledArray(int size, int minValue, int maxValue) {
- // creating random object
- Random rnd = new Random();
- // allocate a new array
- int[] resultArray = new int[size];
- for (int i = 0; i < resultArray.length; i += 1) {
- resultArray[i] = minValue + rnd.nextInt(maxValue - minValue + 1);
- }
- return resultArray;
- }
- // find the intersection of two integer arrays
- // input: two integer arrays
- // output: an array containing the intersection the given arrays
- private static int[] intersection(int[] array1, int[] array2,
- boolean repeatAllowed) {
- // optional: to reduce function calls, array1 should be the smaller
- if (array1.length - array2.length > 10) // for example
- return intersection(array2, array1, repeatAllowed);
- // initialize an array for the result
- int[] resultArray = {};
- // for each number in array1
- for (int aNumber : array1) {
- // if it already exists in result array also
- if (isArrayContains(resultArray, aNumber)) {
- // if repeated values are allowed
- if (repeatAllowed) {
- // add this number to the result array
- resultArray = addToArray(resultArray, aNumber);
- }
- } else
- // if it exists in array2 also
- if (isArrayContains(array2, aNumber)) {
- // add this number to the result array
- resultArray = addToArray(resultArray, aNumber);
- }
- }
- // return the array of intersection result
- return resultArray;
- }
- // check if a number exists in an array
- // input: array of integers and an integer number
- // output: true if the number exists in the array, otherwise false
- private static boolean isArrayContains(int[] array, int aNumber) {
- // for each number in array
- for (int num : array) {
- // if it is equal to the number to be searched
- if (num == aNumber) {
- // return true
- return true;
- }
- }
- // not exist in the array, return false
- return false;
- }
- // append a number to an array
- // input: array of integers and an integer number
- // output:
- // array that contains the given array with the given number appended
- private static int[] addToArray(int[] array, int aNumber) {
- // determine the size of the given array
- int oldSize = array == null ? 0 : array.length;
- // define a new array with one more cell than the given array
- int[] newArray = new int[oldSize + 1];
- // copy the numbers from the given array to the new one
- for (int i = 0; i < oldSize; i += 1) {
- newArray[i] = array[i];
- }
- // add the given number to empty cell at the end of the new array
- newArray[oldSize] = aNumber;
- // return the new array
- return newArray;
- }
- // convert an array of integer numbers to a string
- // input: an array of integer values
- // output: a string with all values from the array separated by comma
- public static String toString(int[] array) {
- // if array is empty
- if (array.length == 0) {
- // return result and quit
- return "[]";
- }
- // building a string from all numbers in the array
- String result = "" + array[0]; // start with the first number
- // scan the numbers in the array without the first
- for (int i = 1; i < array.length; i += 1) {
- // convert number to string and add it to the result string
- result += ", " + array[i];
- }
- // return the result surrounded by brackets
- return "[" + result + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement