Untitled

 avatar
unknown
plain_text
2 months ago
1.1 kB
3
Indexable
// 2D Arrays
// Creation of 2D Arrays
import java.util.*;
public class Matrices {
    public static boolean search(int matrix[][], int key) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] == key) {
                    return true;
                }
            }
        }
        System.out.println("Key " + key + " not found");
        return false;
    }
    public static void main(String[] args) {
        int matrix[][] = new int[3][3];
        int n = matrix.length, m = matrix[0].length;

        Scanner sc = new Scanner(System.in);
        //input
        System.out.println("Enter the elements of the matrix : ");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }
        //output
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }

        // int key = sc.nextInt();
        search(matrix, 5);
    }
}

Editor is loading...
Leave a Comment