Java 比较线性表大小:三种解决方案

本文提供三种 Java 代码实现方案,用于比较两个线性表的大小。包含数组、ArrayList 和 Integer 数组的实现方式。

解决方案 1:使用数组

import java.util.Scanner;

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

        // 输入A表
        System.out.print("请输入A表的元素个数:");
        int m = scanner.nextInt();
        int[] A = new int[m];
        System.out.println("请输入A表的元素:");
        for (int i = 0; i < m; i++) {
            A[i] = scanner.nextInt();
        }

        // 输入B表
        System.out.print("请输入B表的元素个数:");
        int n = scanner.nextInt();
        int[] B = new int[n];
        System.out.println("请输入B表的元素:");
        for (int i = 0; i < n; i++) {
            B[i] = scanner.nextInt();
        }

        // 比较A、B两表的大小
        int i = 0;
        int j = 0;
        while (i < m && j < n) {
            if (A[i] < B[j]) {
                System.out.println("A < B");
                return;
            } else if (A[i] > B[j]) {
                System.out.println("A > B");
                return;
            } else {
                i++;
                j++;
            }
        }
        if (i == m && j == n) {
            System.out.println("A = B");
        } else if (i == m) {
            System.out.println("A < B");
        } else {
            System.out.println("A > B");
        }
    }
}

解决方案 2:使用 ArrayList

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

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

        // 输入A表
        System.out.print("请输入A表的元素个数:");
        int m = scanner.nextInt();
        List<Integer> A = new ArrayList<>();
        System.out.println("请输入A表的元素:");
        for (int i = 0; i < m; i++) {
            A.add(scanner.nextInt());
        }

        // 输入B表
        System.out.print("请输入B表的元素个数:");
        int n = scanner.nextInt();
        List<Integer> B = new ArrayList<>();
        System.out.println("请输入B表的元素:");
        for (int i = 0; i < n; i++) {
            B.add(scanner.nextInt());
        }

        // 比较A、B两表的大小
        int i = 0;
        int j = 0;
        while (i < m && j < n) {
            if (A.get(i) < B.get(j)) {
                System.out.println("A < B");
                return;
            } else if (A.get(i) > B.get(j)) {
                System.out.println("A > B");
                return;
            } else {
                i++;
                j++;
            }
        }
        if (i == m && j == n) {
            System.out.println("A = B");
        } else if (i == m) {
            System.out.println("A < B");
        } else {
            System.out.println("A > B");
        }
    }
}

解决方案 3:使用 Integer 数组

import java.util.Scanner;

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

        // 输入A表
        System.out.print("请输入A表的元素个数:");
        int m = scanner.nextInt();
        Integer[] A = new Integer[m];
        System.out.println("请输入A表的元素:");
        for (int i = 0; i < m; i++) {
            A[i] = scanner.nextInt();
        }

        // 输入B表
        System.out.print("请输入B表的元素个数:");
        int n = scanner.nextInt();
        Integer[] B = new Integer[n];
        System.out.println("请输入B表的元素:");
        for (int i = 0; i < n; i++) {
            B[i] = scanner.nextInt();
        }

        // 比较A、B两表的大小
        int i = 0;
        int j = 0;
        while (i < m && j < n) {
            if (A[i].compareTo(B[j]) < 0) {
                System.out.println("A < B");
                return;
            } else if (A[i].compareTo(B[j]) > 0) {
                System.out.println("A > B");
                return;
            } else {
                i++;
                j++;
            }
        }
        if (i == m && j == n) {
            System.out.println("A = B");
        } else if (i == m) {
            System.out.println("A < B");
        } else {
            System.out.println("A > B");
        }
    }
}

三种解决方案都采用了逐个比较元素的方式,并根据比较结果输出最终结果。您可以根据实际情况选择适合的方案。


原文地址: https://www.cveoy.top/t/topic/pqhI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录