Bài 5: Mảng trong java

1. Mảng trong Java

a) Mảng 1 chiều

Ví dụ:

public class DemoMang1Chieu {
    public static void main(String[] args) {
        int[] arr = {5, 10, 15, 20};
        for (int i = 0; i < arr.length; i++) {
            System.out.println("arr[" + i + "] = " + arr[i]);
        }
    }
}

b) Mảng 2 chiều

Ví dụ:

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

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

2. Bài tập về Mảng

Mảng 1 chiều (5 bài)

Bài 1: Nhập và in mảng

import java.util.Scanner;

public class Bai1_NhapInMang {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số phần tử: ");
        int n = sc.nextInt();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            System.out.print("arr[" + i + "] = ");
            arr[i] = sc.nextInt();
        }

        System.out.println("Mảng vừa nhập:");
        for (int x : arr) {
            System.out.print(x + " ");
        }

        sc.close();
    }
}

Bài 2: Tính tổng và tìm số lớn nhất trong mảng

import java.util.Scanner;

public class Bai2_TongMax {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số phần tử: ");
        int n = sc.nextInt();

        int[] arr = new int[n];
        int sum = 0, max;

        for (int i = 0; i < n; i++) {
            System.out.print("arr[" + i + "] = ");
            arr[i] = sc.nextInt();
            sum += arr[i];
        }

        max = arr[0];
        for (int x : arr) {
            if (x > max) max = x;
        }

        System.out.println("Tổng = " + sum);
        System.out.println("Max = " + max);

        sc.close();
    }
}

Bài 3: Đếm số chẵn và số lẻ

import java.util.Scanner;

public class Bai3_DemChanLe {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số phần tử: ");
        int n = sc.nextInt();
        int[] arr = new int[n];

        int countEven = 0, countOdd = 0;

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            if (arr[i] % 2 == 0) countEven++;
            else countOdd++;
        }

        System.out.println("Số chẵn: " + countEven);
        System.out.println("Số lẻ: " + countOdd);

        sc.close();
    }
}

Bài 4: Kiểm tra số nguyên tố trong mảng

import java.util.Scanner;

public class Bai4_SoNguyenTo {
    public static boolean isPrime(int n) {
        if (n < 2) return false;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số phần tử: ");
        int n = sc.nextInt();
        int[] arr = new int[n];

        System.out.println("Các số nguyên tố trong mảng:");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            if (isPrime(arr[i])) {
                System.out.print(arr[i] + " ");
            }
        }

        sc.close();
    }
}

Bài 5: Sắp xếp mảng tăng dần

import java.util.Arrays;
import java.util.Scanner;

public class Bai5_SapXepTangDan {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số phần tử: ");
        int n = sc.nextInt();
        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        Arrays.sort(arr);

        System.out.println("Mảng sau khi sắp xếp:");
        for (int x : arr) {
            System.out.print(x + " ");
        }

        sc.close();
    }
}

Mảng 2 chiều (2 bài)

Bài 6: Nhập và in ma trận

import java.util.Scanner;

public class Bai6_MaTran {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số hàng: ");
        int m = sc.nextInt();
        System.out.print("Nhập số cột: ");
        int n = sc.nextInt();

        int[][] matrix = new int[m][n];

        System.out.println("Nhập các phần tử:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }

        System.out.println("Ma trận vừa nhập:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }

        sc.close();
    }
}

Bài 7: Tính tổng từng hàng và từng cột trong ma trận

import java.util.Scanner;

public class Bai7_TongHangCot {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số hàng: ");
        int m = sc.nextInt();
        System.out.print("Nhập số cột: ");
        int n = sc.nextInt();

        int[][] a = new int[m][n];

        System.out.println("Nhập các phần tử:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = sc.nextInt();
            }
        }

        // Tổng từng hàng
        for (int i = 0; i < m; i++) {
            int sumRow = 0;
            for (int j = 0; j < n; j++) {
                sumRow += a[i][j];
            }
            System.out.println("Tổng hàng " + i + " = " + sumRow);
        }

        // Tổng từng cột
        for (int j = 0; j < n; j++) {
            int sumCol = 0;
            for (int i = 0; i < m; i++) {
                sumCol += a[i][j];
            }
            System.out.println("Tổng cột " + j + " = " + sumCol);
        }

        sc.close();
    }
}