'ArrayList of Integers to one int?

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?

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



Solution 1:[1]

The simplest way in Java is:

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:

  • 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.

Solution 2:[2]

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:

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.

Solution 3:[3]

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

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.

Solution 4:[4]

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.

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

Solution 5:[5]

Another solution, This will accept numbers > 9

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

Solution 6:[6]

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.

Solution 7:[7]

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

Solution 8:[8]

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.

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

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

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

Solution 9:[9]

In Python! Just for fun:

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

Solution 10:[10]

In Haskell,

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

In Python,

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Myers
Solution 2
Solution 3
Solution 4 Aric TenEyck
Solution 5 thkala
Solution 6 Jonas
Solution 7 Marco Leung
Solution 8 John Christman
Solution 9 Robbie
Solution 10 Clint Miller