Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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 - Fatal编程技术网

Java扫描阵列

Java扫描阵列,java,Java,我在java方面遇到了麻烦。我需要扫描数字(例如:1234567)并将它们放入数组。问题是我不知道要多久。是否有一个命令可用于确定放入扫描仪的数字的长度?如果需要可变大小的集合,请使用 ArrayList<Integer> lst = new ArrayList<Integer>(); lst.add(scanner.getInt()); ArrayList lst=new ArrayList(); lst.add(scanner.getInt()); 如果您不想使用A

我在java方面遇到了麻烦。我需要扫描数字(例如:
1234567
)并将它们放入数组。问题是我不知道要多久。是否有一个命令可用于确定放入扫描仪的数字的长度?

如果需要可变大小的集合,请使用

ArrayList<Integer> lst = new ArrayList<Integer>();
lst.add(scanner.getInt());
ArrayList lst=new ArrayList();
lst.add(scanner.getInt());

如果您不想使用ArrayList,作为一种解决方法,我建议您阅读整行内容,然后拆分以获得长度和单个值:

   String line = scanner.nextLine();
   String[] values = line.split(" ");
   int[] intValues = new int[values.length];
   int indx = 0;
   for(String value:values){
     intValues[indx++] = Integer.parseInt(value);
   }
编辑:第二种方法:

   List<Integer> numList = new ArrayList<Integer>();
   int number = 0;
   while (number != -1) {
      System.out.println("Enter a positive integer value (or -1 to stop): ");
      number = Integer.parseInt(in.nextLine());
      if (number != -1){
        numList.add(number);
      }
   }
   in.close();

   Integer[] numArray = numList.toArray(new Integer[numList.size()]);
List numList=new ArrayList();
整数=0;
while(数字!=-1){
System.out.println(“输入正整数值(或-1停止):”;
number=Integer.parseInt(in.nextLine());
如果(数字!=-1){
numList.add(数字);
}
}
in.close();
整数[]numArray=numList.toArray(新整数[numList.size()]);
EDIT2:处理同一行中的多个数字,并在空行终止

List<Integer> numList = new ArrayList<Integer>();
while(true) {
   System.out.println("Enter a positive integer value (or -1 to stop): ");
   String line = in.nextLine();
   if(line.length() <1){
      break;
   }
   String [] numbers = line.split(" ");
   for(String num: numbers){
          numList.add(Integer.parseInt(num));
   }
 }
 in.close();

 Integer[] numArray = numList.toArray(new Integer[numList.size()]);
List numList=new ArrayList();
while(true){
System.out.println(“输入正整数值(或-1停止):”;
String line=in.nextLine();

如果(line.length()您可以使用
ArrayList
。只需填充它并在最后将其转换为整数数组。或者更好的是,保留该列表并使用它

Scanner in = new Scanner(System.in);

ArrayList<Integer> intList = new ArrayList<Integer>();
while (true) {
  System.out.println("Enter a positive integer value (or -1 to stop): ");
  int number = in.nextInt();
  if (number < 0) break;
  intList.add(number);
}
// just use this last line if you really want an array. Work with the list instead if possible.
Integer[] yourFinalArray = intList.toArray(new Integer[0]);
Scanner-in=新的扫描仪(System.in);
ArrayList intList=新的ArrayList();
while(true){
System.out.println(“输入正整数值(或-1停止):”;
int number=in.nextInt();
如果(数量<0)中断;
intList.add(编号);
}
//如果你真的想要数组,就用最后一行。如果可能的话,用列表代替。
整数[]yourFinalArray=intList.toArray(新整数[0]);

如果您使用google Guava库(),您可以说:

String numbers="1 3 4 2 5 6 7";
Splitter.on(" ").split( numbers );
Iterables.toArray(Splitter.on(" ").split( numbers ), String.class)
这将为您提供一个iterable。如果您坚持获取字符串数组,您可以说:

String numbers="1 3 4 2 5 6 7";
Splitter.on(" ").split( numbers );
Iterables.toArray(Splitter.on(" ").split( numbers ), String.class)

我使用过的大多数扫描设备都用作键盘,只是“键入”不管你扫描什么。所以你可以抓取所有的输入,然后解析它。@user1791473:你有两个选项。第一:如果你知道最大输入大小,定义为足够大的数组。接受所有的输入,并在最后截断数组(如果部分填充)。第二:使用开放式集合,例如,最后使用列表和转换为数组(在其他答案中建议)。如果您计划使用选项一并需要帮助,请告诉我。@user1791473:添加了第二种方法的答案。@user1791473:更新为使用
nextLine()
。当您输入
-1
@user1791473时,此操作将立即停止:您需要定义一些终止条件。
-1
只是一个示例。您可以使用任何您喜欢的条件。请共享,您想要使用的条件是什么?@user1791473是。更新了答案中的
EDTI2
部分。请检查。现在它将在n
仅空行