import java.util.*

public class ArrayMinMax {
    public static void main(String[] args) 
        Integer[] numbers={8,2,6,7,1,4,9,5,3};

        int min=(int)Collections.min(Arrays.asList(numbers));
        int max=(int)Collections.max(Arrays.asList(numbers));

        System.out.println("Min number: "+min);
        System.out.println("Max number: "+max);

Hi guys, me again.
Okay so I use this code in a single array if i want to know what the minimum and maximum numbers are. Now i'm trying to figure out how to implement the same program in a 2d array, with a 3x5 matrix and the user has to input the columns.

import javax.swing.*;
import java.util.*;
public class CMPROG2threeXfive{
    public static void main(String[] args){
        int[][] matrix=new int[3][5];
        for(int row=0;row<matrix.length;row++){
            for(int col=0;col<matrix[row].length;col++)
                        ("Enter 15 integers for the 3x5 matrix:"));

and that's what i've come up with for the code of the 3x5 matrix. i'm not sure if i have to use another nested for loops for the min and max counting. any help would be very much appreciated.

Recommended Answers

All 6 Replies

The old fashin way to find min and max is to iterate through the collection and update a min and max variables.
like so:

min = matrix[3][2];//just init this with something from the matrix
for(int i=0; i<numRows; i++)
for(int j=0; j<numCols; j++)

like this:

I think you can also build an arry that hold the maximum value from every row and then call the max() method on it.
but I cannot compile this on my machine.

int number_of_rows_in_matrix = ...
int[] maxCandidates = new int[number_of_rows_in_matrix];
for(int i=0;i< number_of_rows_in_matrix; i++){
maxCandidates = (int)Collections.max(Arrays.asList(matrix);
max = (int)Collections.max(Arrays.asList(maxCandidates);

Oh, heck. I send a reply before I done writing it properly.

So, the old fasion code is this:

min = matrix[3][2];//just init this with something from the matrix
for(int i=0; i<numRows; i++){
    for(int j=0; j<numCols; j++){
      if(matrix[i][j] < min)
         min = matrix[i][j];

thats all. straightforward and simple!

the second algorithem should also work, but I cannot complie the Arrays.asList code...

int[] maxCandidates = new int[3];
        for(int k=0; k<3; k++)
            maxCandidates[k] = Collections.max(Arrays.asList(matrix[k]));
        int max = Collections.max(Arrays.asList(maxCandidates));

Bye now

import javax.swing.*;
public class Exer3 {
    public static void main(String[] args){
        JOptionPane.showMessageDialog(null,"Welcome to the MinMax© Program");

        int col=0,row=0;
        int[][] arr=new int[3][5];
        int min=arr[row][col];
        int max=arr[row][col];

                        ("Enter Row: "+row+".\nColumn: "+col+".\n(Integer)."));

            System.out.println("The maximum number of Row "+row+" is "+max+".");
            System.out.println("The minimum number of Row "+row+" is "+min+".\n");

        }JOptionPane.showMessageDialog(null,"Thank you for using this program.");

okay so, this is the code that i've been working on, btw thank you so much apple pi. but the thing is i cant figure out why my minimum always shows up to be 0 even if the user doesn't input any 0's at all. i would really appreciate any help. thank you to all ^_^

I fined some important code here
<spam link deleted>

Hello Anil
I assume that you are the Anil who wrote the code that link refers to? Please remember that this is not a forum for self-promotion. The DaniWeb rules that you agreed to include "Do ensure that all posts contain relevant content and substance and are not simply vehicles for external links".
Also - thius thread is two years old - any answer would be too late anyway

and why fine code anyway? It's never going to pay...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.