将值添加到未定义大小的数组

时间:2017-12-01 14:44:46

标签: java arrays for-loop initializing

我已经研究并尝试了几个小时来解决我的问题,但事实是我无法找到任何东西。这很简单。我需要初始化未定义大小的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;
    }
}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我仍然无法实现我真正想要实现的目标,但我已经使用了我的代码来妥协。它允许用户在输入整数之前指定长度。

&#13;
&#13;
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;
    }
    

}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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

根据我的理解,问题是关于创建具有未定义长度的一维数组,并且该数组的长度将通过“扫描器”输入来增加。我看到的很多答案都与使用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(input == 0) break; if 语句来解决了这个问题。一旦我不想使用Scanner类进行任何输入,我只需键入'0'并按Enter键,该数组将不接受任何其他输入,并且将'0'之前进行的输入保存为定义的 int [ ]数字数组


现在回到数组的最大长度...我发现文章中,数组的最大长度为 int 最大长度减去8(或类似值)。这对我不起作用。我在此处阅读有关堆栈溢出的一些帖子,其中数组长度取决于JVM和我未作进一步探讨的其他因素。我认为最大数组长度也取决于某些设置,但我不想说谎。这就是为什么我将数组长度设置为4000000000的原因。当我将长度设置为5000000000时,我得到了错误:

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

如果要使用此代码,只需找出最大数组长度并使用它即可。


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

相关问题