爪哇; int[] 到 int

发布于 2024-12-01 00:19:47 字数 67 浏览 1 评论 0原文

我如何以特殊的方式将整数数组转换为整数?

例如,如何将 { 1, 9, 9, 0 } 转换为 1990?

How would I convert an array of integers into an integer, but in a special way?

For example, how would I convert { 1, 9, 9, 0 } into 1990?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

嘿哥们儿 2024-12-08 00:19:48

使用:

public void turnArrToInt(int x[]){
    for(int i = 0; i < x.length; i++){
        number += x[i]*Math.pow(10,x.length-i-1);
    }

这就是我的实现方式。它获取每个元素,如果它是数组的第一个元素,则将其乘以 10^(数组的长度 - 1 - theCurrentElement),并将所有元素添加到结果中。它适用于所有尺寸。基本上是将第 100 个乘以 100,第 10 个乘以 10,依此类推。

例如: char = {1,2,3} 会将 1 乘以 100 2 乘以 10 3 乘以 1,然后得到所有结果的总和并得到 123。

Use:

public void turnArrToInt(int x[]){
    for(int i = 0; i < x.length; i++){
        number += x[i]*Math.pow(10,x.length-i-1);
    }

This is how I implemented it. It takes each element, and if it is the first of the array it multiplies it by 10^(length of the array - 1 - theCurrentElement) and it adds all to the result. It works for all sizes. Basically multiplying the 100th by 100, 10th by 10 and so on.

For example: char = {1,2,3} would multiply 1 by 100 2 by 10 3 by 1 and get the sum of all and get 123.

绻影浮沉 2024-12-08 00:19:48
Arrays.toString(nums).replaceAll("\\D+","");
Arrays.toString(nums).replaceAll("\\D+","");
白鸥掠海 2024-12-08 00:19:47

这适用于任何大小的整数数组

int nums[] = { 1, 9, 9, 0 };

StringBuilder strNum = new StringBuilder();

for (int num : nums) 
{
     strNum.append(num);
}
int finalInt = Integer.parseInt(strNum.toString());
System.out.println(finalInt);

This will work for any size integer array

int nums[] = { 1, 9, 9, 0 };

StringBuilder strNum = new StringBuilder();

for (int num : nums) 
{
     strNum.append(num);
}
int finalInt = Integer.parseInt(strNum.toString());
System.out.println(finalInt);
久隐师 2024-12-08 00:19:47
int[] array = {1,9,9,0};
int result = 0;
for(int i = 0; i < array.length; i++) result += Math.pow(10,i) * array[array.length - i - 1];
System.out.println(result);
Output: 1990
int[] array = {1,9,9,0};
int result = 0;
for(int i = 0; i < array.length; i++) result += Math.pow(10,i) * array[array.length - i - 1];
System.out.println(result);
Output: 1990
久夏青 2024-12-08 00:19:47

看到不必要的复杂解决方案令人沮丧,涉及 Math.pow、String 等。甚至还出现了正则表达式和第三方库!

您所需要的只是一个循环和乘以 10:

int num = 0;
for (int a : arr) {
  num = 10*num + a;
}

这当然假设 arr 的元素在 0-9 范围内;但无论如何,在这种情况之外的正确行为是不确定的。

它还假设数组的数字可以转换为整数而不会溢出;但由于问题询问如何转换为整数,因此溢出行为也是未定义的。


请注意,您可以通过与字符串连接解决方​​案类似的方式处理“element > 9”情况,方法是计算出如何您必须将 num 转换为多个数字:

int num = 0;
for (int a : arr) {
  if (a < 0) throw new IllegalArgumentException();
  int i = a;
  do {
    num *= 10;
    i /= 10;
  } while (i != 0);
  num += a;
}

我认为这不是处理元素中多个数字的最合适方法;至少应该指定行为。我只是指出您不需要诉诸字符串来处理它。

It is upsetting to see the unnecessarily complex solutions, involving Math.pow, String etc. Even regex and third party libraries make an appearance!

All you need is a loop and multiplication by 10:

int num = 0;
for (int a : arr) {
  num = 10*num + a;
}

This of course assumes that the elements of arr are in range 0-9; but then the correct behavior outside that case is undefined anyway.

It also assumes that the array's digits can be converted to an integer without overflow; but since the question asks about converting to an integer, overflow behavior is also undefined.


Note that you can handle the "element > 9" case in the similar way to the string concatenation solution by working out how many digits you have to shift num by:

int num = 0;
for (int a : arr) {
  if (a < 0) throw new IllegalArgumentException();
  int i = a;
  do {
    num *= 10;
    i /= 10;
  } while (i != 0);
  num += a;
}

I don't think this is the most appropriate way to handle multiple digits in an element; at the very least the behaviour should be specified. I'm just pointing out that you don't need to resort to strings to handle it.

如日中天 2024-12-08 00:19:47

假设您的数字是 Integer[] 而不是原始的 int[] 并且有 Commons Lang Library 您可能会发现下面的一行有用

    Integer[] array = {1, 9, 9, 0 };
    System.out.println(Integer.valueOf(StringUtils.join(array)));

或者如果整数太大而无法放入 int 中,请使用 BigInteger

    Integer[] piDigits = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2,
            3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1,
            9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9,
            4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6 };
    System.out.println(new BigInteger(StringUtils.join(piDigits)));

Assuming you have digits in Integer[] instead of primitive int[] and have Commons Lang Library you may find following one liner useful

    Integer[] array = {1, 9, 9, 0 };
    System.out.println(Integer.valueOf(StringUtils.join(array)));

Or if the integer is too big to fit in int use BigInteger

    Integer[] piDigits = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2,
            3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1,
            9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9,
            4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6 };
    System.out.println(new BigInteger(StringUtils.join(piDigits)));
四叶草在未来唯美盛开 2024-12-08 00:19:47

我不确定在 Java 中,但在伪 .NET 代码中:

String value = "";
for (int i = 0; i < array.length; i++)
{
    value += array[i]; // Build out the number as a string
}

int someInt = Integer.parseInt(value);

I am not sure in Java, but in pseudo .NET code:

String value = "";
for (int i = 0; i < array.length; i++)
{
    value += array[i]; // Build out the number as a string
}

int someInt = Integer.parseInt(value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文