简体   繁体   English

在Java中解析字符串数组

[英]Parsing a string array in java

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

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 + " "); 
  }

You're recreating the array each time in the for loop. 每次在for循环中都要重新创建数组。 Also you're using a for each at the second for and not using it, using i instead. 另外,您在第二个中for each都使用a而不是使用它,而是使用i That would not compile since i was never declared in that scope.. Anyway I suggest you forget about arrays in Java and use Lists, they're much more convenient. 因为i从未在该范围内声明过,所以无法编译。无论如何,我建议您忘记Java中的数组并使用List,它们更加方便。

    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 + " "); 
    }

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]); 
}
  1. You are misnaming among num and number . 您在numnumber之间混淆。

  2. you are creating a new array each time the first for loop body. 每次第一个for循环体都在创建一个新数组。 rather remove it from the loop body and place it before the loop statement as you were doing. 而是将其从循环主体中删除,然后像您所做的那样将其放置在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. You are printing it wrong with enhanced-for loop: 您使用enhanced-for循环打印错误:

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

    Rather directly print with item : 而是直接使用item打印:

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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM