Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Parsing character by character is going to be slow. Perhaps you could consider alternative implementations:

  • Split the string with a regular expressions
  • Use a regular expression to match the numbers and iterate over the matches

Here's an example:

public static List<Float> parse(String data) {
 String[] parts = data.replaceAll("[^\\d.-]|(?=-)", " ").trim().split(" +");
 List<Float> numbers = new ArrayList<>(parts.length);
 for (String s : parts) {
 numbers.add(Float.parseFloat(s));
 }
 return numbers;
}

For the sample you gave, this method will return a list with numbers:

 245.792, 247.275, -0.828, 1.036, 0.794, 2.333, 0.924, 3.53, 0.161, 1.442, ...

For other approaches using regular expressions, see this related question this related question on stackoverflow (I asked just for this question ;-)

Parsing character by character is going to be slow. Perhaps you could consider alternative implementations:

  • Split the string with a regular expressions
  • Use a regular expression to match the numbers and iterate over the matches

Here's an example:

public static List<Float> parse(String data) {
 String[] parts = data.replaceAll("[^\\d.-]|(?=-)", " ").trim().split(" +");
 List<Float> numbers = new ArrayList<>(parts.length);
 for (String s : parts) {
 numbers.add(Float.parseFloat(s));
 }
 return numbers;
}

For the sample you gave, this method will return a list with numbers:

 245.792, 247.275, -0.828, 1.036, 0.794, 2.333, 0.924, 3.53, 0.161, 1.442, ...

For other approaches using regular expressions, see this related question on stackoverflow (I asked just for this question ;-)

Parsing character by character is going to be slow. Perhaps you could consider alternative implementations:

  • Split the string with a regular expressions
  • Use a regular expression to match the numbers and iterate over the matches

Here's an example:

public static List<Float> parse(String data) {
 String[] parts = data.replaceAll("[^\\d.-]|(?=-)", " ").trim().split(" +");
 List<Float> numbers = new ArrayList<>(parts.length);
 for (String s : parts) {
 numbers.add(Float.parseFloat(s));
 }
 return numbers;
}

For the sample you gave, this method will return a list with numbers:

 245.792, 247.275, -0.828, 1.036, 0.794, 2.333, 0.924, 3.53, 0.161, 1.442, ...

For other approaches using regular expressions, see this related question on stackoverflow (I asked just for this question ;-)

Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

Parsing character by character is going to be slow. Perhaps you could consider alternative implementations:

  • Split the string with a regular expressions
  • Use a regular expression to match the numbers and iterate over the matches

Here's an example:

public static List<Float> parse(String data) {
 String[] parts = data.replaceAll("[^\\d.-]|(?=-)", " ").trim().split(" +");
 List<Float> numbers = new ArrayList<>(parts.length);
 for (String s : parts) {
 numbers.add(Float.parseFloat(s));
 }
 return numbers;
}

For the sample you gave, this method will return a list with numbers:

 245.792, 247.275, -0.828, 1.036, 0.794, 2.333, 0.924, 3.53, 0.161, 1.442, ...

For other approaches using regular expressions, see this related question on stackoverflow (I asked just for this question ;-)

lang-java

AltStyle によって変換されたページ (->オリジナル) /