在java中解析字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20044373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:38:17  来源:igfitidea点击:

Parsing a string array in java

javaarraysstringparsingdouble

提问by Oscar F

sNums = scanString.nextLine();    
String[] num = sNums.split(" ");    
for (int i = 0; i < num.length; ++i)    
{    
    numbers = new double[i+1];     
    numbers[i] = Double.valueOf(num[i]);    
}    
for(double item: numbers)    
    out.print(item + " ");

I'm trying to change the String of numbers I have which is "num" in this case into an array of double. I'm pretty sure this should work but for some reason it's storing "0.0" into every element entered except for the last one. For example if I enter "1 5 7 90 52[enter]" the output should be "1.0 5.0 7.0...etc" but instead what I get is "0.0 0.0 0.0 0.0 52.0"

我正在尝试将在这种情况下为“num”的数字字符串更改为双精度数组。我很确定这应该可行,但出于某种原因,它将“0.0”存储到输入的每个元素中,除了最后一个。例如,如果我输入“1 5 7 90 52[enter]”,输出应该是“1.0 5.0 7.0...etc”,但我得到的是“0.0 0.0 0.0 0.0 52.0”

采纳答案by Damian Leszczyński - Vash

The problem you have is that you create a new array in loop. You should take it out and initialized.

您遇到的问题是您在循环中创建了一个新数组。您应该将其取出并进行初始化。

  String[] num = sNums.split(" ");    
  double[] numbers = new double[num.length];   // The valid place for loop

  for (int i = 0; i < num.length; ++i)    
  {    
    numbers[i] = Double.valueOf(num[i]);    
  }    

  for(double item: numbers)  {
    out.print(item + " "); 
  }

回答by SudoRahul

You're simple creating a new array with the increasing size in the loop on every iteration. You just need to create a new array once and populate the values in it, in the loop.

您很容易在每次迭代的循环中创建一个大小不断增加的新数组。您只需要创建一个新数组并在循环中填充其中的值。

numbers = new double[num.length];
for (int i = 0; i < num.length; ++i) {
    // numbers = new double[i+1];   // Not needed  
    numbers[i] = Double.valueOf(num[i]); 
}

回答by Sage

  1. You are misnaming among numand number.

  2. you are creating a new array each time the first forloop body. rather remove it from the loop body and place it before the loopstatement as you were doing.

    String[] number = sNums.split(" ");
    for (int i = 0; i < number.length; ++i)    
    {    
      //  numbers = new double[i+1];   <-- commented out 
      numbers[i] = Double.valueOf(num[i]);    
    }  
    
  3. You are printing it wrong with enhanced-forloop:

    for(double item: numbers)    
        out.print(numbers[i] + " "); 
                     // <-- i may already have the value of number.length
    

    Rather directly print with item:

    for(double item: numbers)    
        out.print(item + " "); 
    
  1. 您在num和中命名不当number

  2. 每次第一个for循环体时,您都在创建一个新数组。而是将其从循环体中删除,并loop像您一样将其放在语句之前。

    String[] number = sNums.split(" ");
    for (int i = 0; i < number.length; ++i)    
    {    
      //  numbers = new double[i+1];   <-- commented out 
      numbers[i] = Double.valueOf(num[i]);    
    }  
    
  3. 你用enhanced-for循环打印错了:

    for(double item: numbers)    
        out.print(numbers[i] + " "); 
                     // <-- i may already have the value of number.length
    

    而是直接打印item

    for(double item: numbers)    
        out.print(item + " "); 
    

回答by m0skit0

You're recreating the array each time in the for loop. Also you're using a for eachat the second for and not using it, using iinstead. That would not compile since iwas never declared in that scope.. Anyway I suggest you forget about arrays in Java and use Lists, they're much more convenient.

您每次都在 for 循环中重新创建数组。此外,您for each在第二个使用 for 而不是使用它,i而是使用。那不会编译,因为i从来没有在那个范围内声明过。无论如何,我建议你忘记 Java 中的数组并使用列表,它们更方便。

    sNums = scanString.nextLine();    
    final String[] num = sNums.split(" ");    
    final List<Double> numbers = new ArrayList<Double>();
    for (final String cur: num) {    
        numbers.add(Double.valueOf(cur));
    }    
    for(final Double item: numbers) {    
        out.print(item + " "); 
    }

回答by Lahniep

Using 'args' from command line arguments you can use:

使用命令行参数中的“args”,您可以使用:

    List<Double> numbers = new ArrayList<Double>();
    for(String a:args){
        numbers.add(Double.valueOf(a));
    }        
    System.out.println(numbers);