-1

我已经研究并尝试了几个小时来解决我的问题,但现实是我在上面找不到任何东西。真的很简单。我需要初始化未定义大小的java数组,然后比较两者。在测试我的程序的过程中,当我将数组定义为特定长度时(例如)

int[] array = new int[6];

代码一直等到我输入了六个对象才能进入下一段代码,因为它正在等待定义为数组长度的 6 个整数。但我无法使用定义数组

int[] array = {};

它显然不起作用,因为 array.length 函数将为 0。

我的代码如下。

import java.util.Scanner;
import java.util.Arrays;
public class Test {
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);


        // My problem is in the definition of the arrays or the for loops defining them below.
        int[] list1 = new int[]; // undefined
        int[] list2 = new int[]; // undefined
        // ask user to fill the two arrays to see if they are equal
        System.out.print("Enter list one >> ");

        for (int i = 0; i < list1.length; i++){
            list1[i] = input.nextInt();
        }

        System.out.print("Enter list two >> ");

        for (int i = 0; i < list2.length; i++){
            list2[i] = input.nextInt();
        }


        // call the equality testing method and output whether or not the two lists are strictly identical or not.
        if (equals(list1, list2) == true)
            System.out.println("The two lists are strictly identical");
        else
            System.out.println("The two lists are not strictly identical");

    }

    // this method 
    public static boolean equals(int[] list1, int[] list2){
        boolean bool = false;
        if (Arrays.equals(list1, list2))
            bool = true;
        else
            bool = false;

        return bool;
    }
}
4

3 回答 3

1

我需要初始化未定义大小的java数组,

您需要使用 ArrayList 或在开始时询问长度。

List<Integer> list1 = new ArrayList<>();
System.out.println("Enter numbers, with a blank line to end");
for (String line; !(line = input.nextLine()).trim().isEmpty(); ) {
     list1.add(Integer.parseInt(line));
}

// later
if (list1.equals(list2))

或使用数组

System.out.println("Enter the number of numbers, followed by the numbers");
int[] array1 = new int[input.nextInt()]; // enter the size first.
for (int i = 0; i < array1.length; i++)
    array[i] = input.nextInt();

// later
if (Arrays.equals(array1, array2))

int[] array = {};

它显然不起作用,因为 array.length 函数不起作用。

这可以按预期工作,并且array.length始终0

于 2017-12-01T14:51:56.703 回答
0

我看到这个问题是一个较旧的问题,但我有相同的问题(或至少相似的问题)并且找不到我正在寻找的答案。现在我相信我已经找到了答案并愿意分享。也许对某人来说这会很方便。

根据我的理解,问题是关于创建一个长度未定义的一维数组,并且该数组的长度将由 Scanner 输入增加。我看到的很多答案都是关于使用 ArrayList。但我仍然想知道如何使用一维数组来做到这一点。先给大家分享一下代码,再解释一下:

public class Main {

    static Scanner scanner = new Scanner(System.in);
    final static int ARRAY_MAX_LENGTH = 400_000_000;

    public static void main(String[] args) {


        int[] numbers = createIntArray();

        displayArray(numbers);

    }

    public static int[] createIntArray() {
        int[] ints = new int[ARRAY_MAX_LENGTH];

        System.out.print("Enter numbers: ");
        for (int i = 0; i < ints.length; i++) {
            ints[i] = scanner.nextInt();
            if (ints[i] == 0) {
                break;
            }
        } return ints;
    }

    public static void displayArray(int[] ints) {
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i] + " ");
            if (ints[i] == 0) {
                break;
            }
        }
    }
}

现在解释一下:我们想要未定义/无限的数组长度。事实是:你不能拥有它。在编程中,一切都有限制。字节限制在 -128 到 127 之间,短限制在 -32,768 到 32,767 之间,int限制在 -2,147,483,648 到 2,147,483,647 之间。那么如何创建长度未定义的数组呢?矛盾的是:设置数组长度。并且长度应该是数组可以容纳的最大长度。然后在您希望数组不再接受来自 Scanner 类的输入时创建一个退出点。我通过在我的代码中包含带有break关键字的if语句来解决它(if(input == 0) break;)。一旦我不想使用 Scanner 类进行任何输入,我只需键入“0”并按 Enter,该数组不接受任何其他输入,并且在“0”之前进行的输入保存在定义的int[] 数字数组中.


现在回到数组最大长度......我发现数组最大长度是int最大长度减 8(或类似的东西)的文章。这对我不起作用。我在 Stack Overflow 上阅读了一些帖子,其中数组长度取决于 JVM 以及我尚未进一步探索的其他因素。我认为最大数组长度也取决于某些设置,但我不想撒谎。这就是我将数组长度设置为 400 000 000 的原因。当我将长度设置为 500 000 000 时,出现错误:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

如果您想使用此代码,只需找出您的最大数组长度并使用它。


对我来说,这个问题思考起来很有趣,但我绝对不会在大型程序中使用它。它根本没有效率。对于新手:如果您刚刚学习了一维数组,请耐心等待,ArrayList 将在您以后的学习中出现,它可以使事情变得更容易。

于 2020-09-29T06:05:09.717 回答
0

我仍然无法完成我真正想要完成的事情,但我已经使用我的代码进行了妥协。它是允许用户在输入整数之前指定长度。

import java.util.Scanner;
import java.util.Arrays;
public class Test {
    public static void main(String[] args){
        
        Scanner input = new Scanner(System.in);
        
        System.out.print("How many variables long is the first list? ");
        int n = input.nextInt();
        int[] list1 = new int[n];
        
        System.out.print("How many variables long is the second list? ");
        n = input.nextInt();
        int[] list2 = new int[n];
        
        // ask user to fill the two arrays to see if they are equal
        System.out.print("Enter list one >> ");
        
        for (int i = 0; i < list1.length; i++){
            list1[i] = input.nextInt();
        }
        
        System.out.print("Enter list two >> ");
        
        for (int i = 0; i < list2.length; i++){
            list2[i] = input.nextInt();
        }
        
        
        // call the equality testing method and output whether or not the two lists are strictly identical or not.
        if (equals(list1, list2) == true)
            System.out.println("The two lists are strictly identical");
        else
            System.out.println("The two lists are not strictly identical");
        
    }
    
    // this method 
    public static boolean equals(int[] list1, int[] list2){
        boolean bool = false;
        if (Arrays.equals(list1, list2))
            bool = true;
        else
            bool = false;
        
        return bool;
    }
    

}

于 2017-12-01T19:10:33.477 回答