int数组到Java中的int数 [英] int array to int number in Java

查看:256
本文介绍了int数组到Java中的int数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个网站上是新手,如果我在这里,那是因为我没有在网上任何地方找到答案并相信我:我一直在谷歌搜索相当长的一段时间,但我能找到的只是如何将数字转换为数组而不是arround的其他方式。
我正在寻找一种简单的方法或函数来将int数组转换为int数。让我解释一下例如我有这个:

I'm new on this site and, if I'm here it's because I haven't found the answer anywhere on the web and believe me: I've been googling for quite a time but all I could find was how to convert a number to an array not the other way arround. I'm looking for a simple way or function to convert an int array to an int number. Let me explain for example I have this :

int[] ar = {1, 2, 3};

我希望这样:

And I want to have this:

int nbr = 123;

在我看来,它看起来像这样(即使我知道这不是正确的方式):

In my head it would look like this (even if I know it's not the right way):

int nbr = ar.toInt(); // I know it's funny

如果你知道我该怎么做,那就是非常棒。

If you have any idea of how I could do that, that'd be awesome.

推荐答案

0 的结果开头。循环遍历 int 数组的所有元素。将结果乘以 10 ,然后从数组中添加当前数字。在循环结束时,你得到你的结果。

Start with a result of 0. Loop through all elements of your int array. Multiply the result by 10, then add in the current number from the array. At the end of the loop, you have your result.


  • 结果:0

  • 循环1 :结果* 10 => 0,结果+ 1 => 1

  • 循环2:结果* 10 => 10,结果+ 2 => 12

  • 循环3:结果* 10> = 120,结果+ 3 => 123

  • Result: 0
  • Loop 1: Result * 10 => 0, Result + 1 => 1
  • Loop 2: Result * 10 => 10, Result + 2 => 12
  • Loop 3: Result * 10 >= 120, Result + 3 => 123

这可以通过更改对任何基础进行推广 10 的基数(此处)为其他内容,例如 16 为十六进制。

This can be generalized for any base by changing the base from 10 (here) to something else, such as 16 for hexadecimal.

这篇关于int数组到Java中的int数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆