How do I find the minimum in a newly created array after putting two different arrays together?

Asked 2 years ago, Updated 2 years ago, 29 views

I asked you a question yesterday, but something else...There are two different classes (ArrayUtils, ArrayUtilsTester). The first class is to write down the methods, and the second class is to print out the values. The array arr1 and arr2 combined to create and print a new array called mergeArray, but what should we do to find the minimum value in the mergeArray array? The same method and method used to find the minimum values of the arr1 and arr2 arrays did not work. Both the size of each array and its integers are random numbers between 0 and 100.


    import java.util.Random;
    import java.util.stream.IntStream;

    public class ArrayUtils 
    {
        private int[] arr;
        ArrayUtilsTester aut = new ArrayUtilsTester();

        public ArrayUtils(int sizeOfArray)
        {
            arr = new int [sizeOfArray];
            Random random = new Random();

            for (sizeOfArray = 0; sizeOfArray < arr.length; sizeOfArray++)
            {
                    arr [sizeOfArray] = random.nextInt(101);
            }

        }

        public int[] getArr() 
        {
                return arr;
        }

        public void setArr(int[] arr) 
        {
                this.arr = arr;
        }

        public int findMin()  
        {
            int minValue = arr[0];

            for(int i=1;i < arr.length;i++)
            {
                    if(arr[i] < minValue)
                {
                        minValue = arr[i];
                }
            }
            return minValue;    
        }

        public int[] mergeArrays(int[] arr1, int[] arr2) 
        {
            int length = arr1.length+arr2.length;  
            int [] mergedArray = new int[length];

            for(int i=0; i < length; i++) 
            {
                if(i < arr1.length) 
                {
                    mergedArray[i] = arr1[i];   
                }
                else 
                {
                    mergedArray[i] = arr2[i - arr1.length];
                }
            }
            return mergedArray; 
        } 
     }

    import java.util.Arrays;
    import java.util.Random;

    public class ArrayUtilsTester
    {
        public static void main(String[] args)
        {
            Random random = new Random();
            int sizeOfArray = random.nextInt(101);

            ArrayUtils arr1 = new ArrayUtils(sizeOfArray);
            ArrayUtils arr2 = new ArrayUtils(sizeOfArray);
            int[] mergedArray = arr1.mergeArrays(arr1.getArr(), arr2.getArr());

        System.out.println("The randomly generated array arr1 is : " + (Arrays.toString(arr1.getArr())));
        System.out.println("Smallest integer in the arr1 is = "  + (arr1.findMin()))

        System.out.println("The randomly generated array arr2 is : " + (Arrays.toString(arr2.getArr())));
        System.out.println("Smallest integer in the arr1 is = "  + (arr2.findMin()));

        System.out.println("The merged array between arr1 and arr2 is  = " + (Arrays.toString(mergedArray)));

java

2022-09-22 20:15

2 Answers

If it's Java 8 or higher, you can do it as follows.

import java.util.Arrays;
import java.util.stream.IntStream;

public class Main02 {
    public static void main(String[] args) {
        int[] arr01 = IntStream.of(4, 8, 6, 30).toArray();
        int[] arr02 = IntStream.rangeClosed(11, 20).toArray();

        IntStream.concat(Arrays.stream(arr01), Arrays.stream(arr02)).forEach(System.out::println); // Merged Array Output
        int min = IntStream.concat(Arrays.stream(arr01), Arrays.stream(arr02)).min();getAsInt(); //Minimum
        int max = IntStream.concat(Arrays.stream(arr01), Arrays.stream(arr02)).max();getAsInt(); // Maximum

        System.out.println(min);    // 4
        System.out.println(max);    // 30
    }
}


2022-09-22 20:15

Arr1 and arr2 created in the main function are objects in the ArrayUtils class, not int[]. On the other hand, MergedArray is int[] type, not class. Therefore, mergeArray cannot write the method you declared in ArrayUtils. Therefore, you must also make the mergedArray a class. To do that, the return type of the mergeArrays method should be ArrayUtils, not int[], right?


2022-09-22 20:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.