Java Online Compiler
Example: FindMaxElementInColumnIterative in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// FindMaxElementInColumnIterative public class Main { /** * Finds the maximum element in a specified column of a 2D array. * Handles cases for empty matrices or invalid column indices. * * @param matrix The 2D integer array. * @param columnIndex The index of the column to search. * @return The maximum element in the specified column. * @throws IllegalArgumentException if the matrix is empty or columnIndex is invalid. */ public static int findMaxInColumnIterative(int[][] matrix, int columnIndex) { // Step 1: Validate the matrix and column index if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { throw new IllegalArgumentException("Matrix cannot be empty or null."); } if (columnIndex < 0 || columnIndex >= matrix[0].length) { throw new IllegalArgumentException("Invalid column index. Must be between 0 and " + (matrix[0].length - 1)); } // Step 2: Initialize max_value with the first element of the column int max_value = matrix[0][columnIndex]; // Step 3: Iterate through each row starting from the second row for (int i = 1; i < matrix.length; i++) { // Check if the current row is long enough to have the specified column if (matrix[i].length > columnIndex) { // Step 4: Compare current element with max_value and update if greater if (matrix[i][columnIndex] > max_value) { max_value = matrix[i][columnIndex]; } } // If the row is shorter, we effectively ignore it for this column search } // Step 5: Return the found maximum value return max_value; } public static void main(String[] args) { int[][] data = { {100, 150, 120, 180}, {200, 190, 210, 170}, { 90, 110, 130, 140}, {300, 80, 250, 220} }; // Example 1: Find max in column 1 (index 1) int targetColumn1 = 1; try { int maxInCol1 = findMaxInColumnIterative(data, targetColumn1); System.out.println("Max element in column " + targetColumn1 + ": " + maxInCol1); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } // Example 2: Find max in column 0 (index 0) int targetColumn0 = 0; try { int maxInCol0 = findMaxInColumnIterative(data, targetColumn0); System.out.println("Max element in column " + targetColumn0 + ": " + maxInCol0); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } // Example 3: Invalid column index int invalidColumn = 5; try { int maxInInvalidCol = findMaxInColumnIterative(data, invalidColumn); System.out.println("Max element in column " + invalidColumn + ": " + maxInInvalidCol); } catch (IllegalArgumentException e) { System.out.println("Error for invalid column " + invalidColumn + ": " + e.getMessage()); } // Example 4: Empty matrix int[][] emptyMatrix = {}; try { int maxInEmpty = findMaxInColumnIterative(emptyMatrix, 0); System.out.println("Max element in empty matrix: " + maxInEmpty); } catch (IllegalArgumentException e) { System.out.println("Error for empty matrix: " + e.getMessage()); } } }
Output
Clear
ADVERTISEMENTS