Mảng 1 chiều là tập hợp nhiều phần tử cùng kiểu dữ liệu, được lưu trữ liên tiếp trong bộ nhớ.
Khai báo:
kiểu_dữ_liệu[] tên_mảng = new kiểu_dữ_liệu[kích_thước];
Gán giá trị:
int[] arr = {1, 2, 3, 4, 5};
Truy xuất phần tử:
System.out.println(arr[0]); // In phần tử đầu tiên
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]);
}
}
}
Mảng 2 chiều là tập hợp các mảng 1 chiều (dạng bảng, ma trận).
Khai báo:
int[][] arr = new int[3][4]; // mảng 3 hàng, 4 cột
Gán giá trị:
int[][] arr = {
{1, 2, 3},
{4, 5, 6}
};
Truy xuất phần tử:
System.out.println(arr[1][2]); // In phần tử hàng 1, cột 2 => 6
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();
}
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}