Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java中的用户输入关闭未填充的数组_Java_Arrays - Fatal编程技术网

用Java中的用户输入关闭未填充的数组

用Java中的用户输入关闭未填充的数组,java,arrays,Java,Arrays,我正在用java处理数组。我编写了一个代码,用户输入一个带有数字和字符的字符串,然后程序打印出整数。数组的索引为10个整数,如果用户没有输入足够的整数来填充数组,则使用常量Integer.MIN_值填充。我希望能够关闭数组,因此如果数组没有填充10个整数,它将在用户输入的最后一个整数上关闭,而不是用最小值填充元素 private int[]intArray=new int[10]; 公共StringToInArray(){ 对于(inti=0;i

我正在用java处理数组。我编写了一个代码,用户输入一个带有数字和字符的字符串,然后程序打印出整数。数组的索引为10个整数,如果用户没有输入足够的整数来填充数组,则使用常量Integer.MIN_值填充。我希望能够关闭数组,因此如果数组没有填充10个整数,它将在用户输入的最后一个整数上关闭,而不是用最小值填充元素

private int[]intArray=new int[10];
公共StringToInArray(){
对于(inti=0;i


我一直在努力解决阵列的问题,所以我不确定如何才能解决这个问题。我想说,我应该有一个条件语句,循环在检测到MIN_值时结束,但我不知道这是否正确。

如果您确定10是值的上限,您可以像这样解析并将值存储在临时数组中,完成后创建一个大小合适的数组并复制值

public class StringToIntArray {

   
    private final int[] intArray; // we set this when we know the final size in the constructor

    public static void main(final String[] args) {
       final Scanner input = new Scanner(System.in);
        System.out.println("Enter a string");
        final String str = input.nextLine();
        final StringToIntArray array = new StringToIntArray(str);
        System.out.println(Arrays.toString(array.intArray));

    }

    // Constructor gets the line to parse the values from
    public StringToIntArray(final String str) {
        intArray = scanStringToIntArray(str);
    }

    // parses the values and returns an array of the right size
    public int[] scanStringToIntArray(final String str) {
        final Scanner input = new Scanner(str);
        final int[] temp = new int[10];
        int i = 0;
        while (input.hasNext() == true) {
            if (!input.hasNextInt()) {
                input.next();
            }
            else {
                temp[i] = input.nextInt();
                i++;
            }
        }
        input.close(); // REMARK: even better use try with resources
        final int[] result = new int[i];
        System.arraycopy(temp, 0, result, 0, i); // fastest way to copy between arrays
        return result;
    }

}

创建具有所需大小的新阵列,并将元素从原始阵列复制到新阵列。但是请注意,您可以使用ArrayList。后者将根据需要增长或收缩。似乎您需要使用
列表
而不是
int[]
public class StringToIntArray {

   
    private final int[] intArray; // we set this when we know the final size in the constructor

    public static void main(final String[] args) {
       final Scanner input = new Scanner(System.in);
        System.out.println("Enter a string");
        final String str = input.nextLine();
        final StringToIntArray array = new StringToIntArray(str);
        System.out.println(Arrays.toString(array.intArray));

    }

    // Constructor gets the line to parse the values from
    public StringToIntArray(final String str) {
        intArray = scanStringToIntArray(str);
    }

    // parses the values and returns an array of the right size
    public int[] scanStringToIntArray(final String str) {
        final Scanner input = new Scanner(str);
        final int[] temp = new int[10];
        int i = 0;
        while (input.hasNext() == true) {
            if (!input.hasNextInt()) {
                input.next();
            }
            else {
                temp[i] = input.nextInt();
                i++;
            }
        }
        input.close(); // REMARK: even better use try with resources
        final int[] result = new int[i];
        System.arraycopy(temp, 0, result, 0, i); // fastest way to copy between arrays
        return result;
    }

}