将字符串转换为反向int数组

时间:2019-06-17 06:58:46

标签: java arrays string

我想(习惯上)优化此解决方案。

我有一个仅包含整数值的字符串。我想将此字符串转换为反向int数组。输出应为整数数组

这是我的解决方法:

private static int[] stringToReversedIntArray(String num) {
    int[] a = new int[num.length()];
    for (int i = 0; i < num.length(); i++) {
        a[i] = Integer.parseInt(num.substring(i, i + 1));
    }
    a = reverse(a);
    return a;
}

/*
 * Reverses an int array
 */
private static int[] reverse(int[] myArray) {
    int[] reversed = new int[myArray.length];
    for (int i = 0; i < myArray.length; i++) {
        reversed[i] = myArray[myArray.length - (i + 1)];
    }
    return reversed;
}

Input: "1256346258"
Output: {8,5,2,6,4,3,6,5,2,1}

请提出建议。

8 个答案:

答案 0 :(得分:3)

var input = "1256346258";
var result = Arrays
        .stream(new StringBuffer(input).reverse().toString().split(""))
        .map(Integer::parseInt)
        .collect(Collectors.toList());
System.out.println(result);

答案 1 :(得分:2)

由于您一次提取一个字符,因此使用alscoffee belindamarkbakery noodleshop38 而不是replace(lower(merchants), '&', '')更有意义。

由于将单个字符转换为charAt,因此substringint更有意义。

因此我会改变

Character.getNumericValue()

parseInt

如果您不介意使用a[i] = Integer.parseInt(num.substring(i, i + 1)); 而不是a[i] = Character.getNumericValue(num.charAt(i)); ,还可以按如下方式简化反面部分:

Integer[]

答案 2 :(得分:0)

如果您想使用流,则可以使用以下方法:

Deque<String> deque = num.chars()
   .map(c -> Character.toString((char) c))
   .map(Integer::parseInt)
   .collect(Collector.of(
        ArrayDeque::new,
        (stack, i) -> stack.addFirst(i),
        (s1, s2) -> { s2.addAll(s1); return s2; })); //this combiner won't really be called unless the stream was parallel

Integer[] reversed = deque.toArray(new Integer[deque.size()]);

这只会在列表中循环一次。 Deque可以作为堆栈(后进先出)工作,因此每个addFirst()将自动颠倒顺序,因为它将每个项目添加到列表的前面而不是列表的后面。之后,您可以将其转换为数组,也可以按原样使用Deque,这是常规的Collection并实现Iterable

正如提到的其他答案一样,如果您100%确信只有数字,也可以将其简化一下,因为使用简化版本时,您将不会得到NumberFormatException

Deque<String> deque = num.chars()
   .map(c -> Character.getNumericValue((char) c))
   .collect(Collector.of(
        ArrayDeque::new,
        (stack, i) -> stack.addFirst(i),
        (s1, s2) -> { s2.addAll(s1); return s2; }));

Integer[] reversed = deque.toArray(new Integer[deque.size()]);

答案 3 :(得分:0)

无需循环2次,您可以使用StringBuilder反转字符串,然后对其进行循环以将结果存储在int数组中:

  private static int[] stringToReversedIntArray(String num) {
    num = new StringBuilder(num).reverse().toString();
    int[] result = new int[num.length()]; 
    for (int i = 0; i < result.length; i++){
      result[i] = Character.getNumericValue(num.charAt(i)); 
    }
    return result;
  }

其他选择要么只是反转您的for循环,然后将其倒计数:

for(int i = array.length - 1; i >= 0; i--){
  ...
}

或者在Java的最新版本中使用Stream API,如上面在一个很好的示例中已经提到的那样。

答案 4 :(得分:0)

String str = "123456";
// Reverse String
str = new StringBuilder(str).reverse().toString();
String strArr[] = str.split("(?!^)");
// Convert to Int Array
int[] intarray = java.util.Arrays.stream(strings).mapToInt(Integer::parseInt).toArray();

答案 5 :(得分:0)

Erans解决方案对我来说很好,但是我不喜欢for循环,如果可能的话,我更喜欢流api

  var numb = document.documentElement.scrollTop;
  if( numb <= 20) {
    numb = 20;
  }
  if ( numb >= 120) {
    numb = 120;
  }
  return numb;
}

只需用一种不错的方法将其包裹起来...

答案 6 :(得分:0)

这是一个带有单个for循环的简单解决方案。您可以在提取数值的同时“反转”这些数字,只需对索引进行一点操作即可。

private static int[] stringToReversedIntArray(String num) {
    int len = num.length();
    int[] a = new int[len];
    for (int i = 0; i < len; i++) {
        a[i] = Character.getNumericValue(num.charAt(len - i - 1));
    }
    return a;
}

答案 7 :(得分:0)

单线:

return IntStream
    .range(0, num.length())
    .map(i -> Character.getNumericValue(num.charAt(num.length() - i - 1)))
    .toArray();