java 整数的 ArrayList 到一个 int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/940766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:29:02  来源:igfitidea点击:

ArrayList of Integers to one int?

javaarraylist

提问by Michael Myers

what would be the easiest way to convert an ArrayList of Integers to one int, with the 1st Integer in the list being the 1st number in the int, etc. in Java?

将整数的 ArrayList 转换为一个 int 的最简单方法是什么,列表中的第一个整数是 Java 中 int 中的第一个数字,等等?

For example an ArrayList of Integers: 1 4 6 7 8 3 8
becomes the int value 1467838

例如整数的 ArrayList: 1 4 6 7 8 3 8
变成 int 值 1467838

回答by Michael Myers

The simplest way in Java is:

Java中最简单的方法是:

int total = 0;
for (Integer i : list) { // assuming list is of type List<Integer>
    total = 10*total + i;
}

For example, if the list consists of 1 4 6 7 8 3 8, you get:

例如,如果列表包含 1 4 6 7 8 3 8,您将得到:

  • total = 0
  • total = 10*0 + 1 = 1
  • total = 10*1 + 4 = 14
  • total = 10*14 + 6 = 146
  • ...
  • total = 10*146783 + 8 = 1467838
  • total = 0
  • total = 10*0 + 1 = 1
  • total = 10*1 + 4 = 14
  • total = 10*14 + 6 = 146
  • ...
  • total = 10*146783 + 8 = 1467838

which is the correct answer.

这是正确答案。

回答by Alex Beardsley

Note: easiest is not always most efficient. this is in java since you didn't specify a language. but you could do something like this:

注意:最简单的并不总是最有效的。这是在 Java 中,因为您没有指定语言。但你可以做这样的事情:

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(4);
list.add(6);

for (Integer x : list) {
    s += x.toString();
}

Integer finalResult = Integer.parseInt(s);

EDIT: to please all the people noting this in comments, if you're really worried about efficiency but for some reason want to use this string method, it should be done like this:

编辑:为了取悦所有在评论中注意到这一点的人,如果你真的担心效率但出于某种原因想要使用这个字符串方法,应该这样做:

StringBuilder sb = new StringBuilder();
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(4);
list.add(6);

String s;

for (Integer x : list) {
    sb.append(x.toString());
}

Integer finalResult = Integer.parseInt(sb.toString());

In the first example, StringBuilder was not used for simplicity's sake, because this looks like a homework assignment.

在第一个示例中,为了简单起见,没有使用 StringBuilder,因为这看起来像一个家庭作业。

回答by driis

Assuming C# (you didn't specify :-), but the general algorithm will work in whatever language you need:

假设 C#(您没有指定 :-),但是通用算法可以在您需要的任何语言中工作:

int num = 0;
for( int i = 0 ; i < list.Count ; i++ ) 
{
    num *= 10;
    num += (int)list[i];
}

Obviously the code assumes that the resulting number is small enough to be represented by int, and that each of the items in your ArrayList is between 0 and 9 both inclusive.

显然,代码假定结果数字小到可以用 int 表示,并且 ArrayList 中的每个项目都在 0 和 9 之间(包括这两个值)。

回答by Aric TenEyck

Only handles numbers up to 2 billion or so. Use long, long-long, or your favorite bigint class if you want bigger numbers. Won't work with negative numbers unless they're all negative.

最多只能处理 20 亿左右的数字。如果您想要更大的数字,请使用 long、long long 或您最喜欢的 bigint 类。不会处理负数,除非它们都是负数。

int runningtotal = 0;
foreach(int i in myList) {
    runningtotal *= 10;
    runningtotal += i;
}
return runningtotal;

回答by Peter Lawrey

Another solution, This will accept numbers > 9

另一种解决方案,这将接受数字> 9

List<Integer> list = 
int num = Integer.parseInt(list.toString().replaceAll("\D",""));

回答by Jonas

int result = 0;
for(int i=list.Count - 1;i>=0;--i)
{
  result += list[i] * (int)(Math.Pow((double)10, (double)(list.Count - 1 - i)));
}

Also won't work with negative numbers...you could use Math.Abs() to handle that.

也不适用于负数……您可以使用 Math.Abs​​() 来处理。

回答by Marco Leung

Without knowing which language you want, I think your best approach is to find the sum of 1000000 + 400000 + 60000 + 7000 + 800 + 30 + 8

在不知道你想要哪种语言的情况下,我认为你最好的方法是找到 1000000 + 400000 + 60000 + 7000 + 800 + 30 + 8 的总和

回答by John Christman

I am assuming from your question that index 0 of the ArrayList is the most significant value. Your example show the first number being equivalent to 1,000,000.

我从您的问题中假设 ArrayList 的索引 0 是最重要的值。您的示例显示第一个数字等于 1,000,000。

You could either treat them as characters then concatenate them then parse back to Integer.

您可以将它们视为字符,然后将它们连接起来,然后解析回 Integer。

Or you can add each item to the result, then multiply the result by ten to set the magnitudes.

或者您可以将每个项目添加到结果中,然后将结果乘以 10 以设置大小。

Or you can add each element's value * 10^(count-index); where count is the number of elements in the ArrayList.

或者您可以添加每个元素的值 * 10^(count-index); 其中 count 是 ArrayList 中的元素数。

回答by Robbie

In Python! Just for fun:

在 Python 中!纯娱乐:

i = int( "".join( [str(z) for z in x] ) )

i = int( "".join( [str(z) for z in x] ) )

回答by Clint Miller

In Haskell,

在哈斯克尔,

listToNumber = foldl (\a b -> a*10 + b) 0

In Python,

在 Python 中,

def list_to_number(some_list):
    return reduce(lambda x, y: x*10 + y, some_list, 0)