I have been trying to figure this out for a while and need some help. I need to find the min/max values and print them out for a multidimensional array. Here are the two way
Your problem is: You are sorting the array of int arrays instead of sorting each individual int in each int array.
To solve this: Loop through each int array in the array of int arrays.
Instructions for finding the maximum and minimum of a 2D int array using Arrays.sort():
int array to sort called data.ints, one to hold the maximum value, the other the minimum value.
Integer.MIN_VALUE and the initial value of the minimum should be Integer.MAX_VALUE to make sure that negative values are handled.data from 0 to data.length:
data[i]data[i] is less than the minimum and change it if it is.data[i] is greater than the maximum and change it if it is.Example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] data = {{3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
int maximum = Integer.MIN_VALUE;
int minimum = Integer.MAX_VALUE;
for(int i = 0; i < data.length; i++) {
Arrays.sort(data[i]);
if(data[i][0] < minimum) minimum = data[i][0];
if(data[i][data[i].length - 1] > maximum) maximum = data[i][data[i].length - 1];
}
System.out.println("Minimum = " + maximum);
System.out.println("Maximum = " + minimum);
}
}