Reputation: 2630
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?
Upvotes: 3
Views: 53256
Reputation: 140534
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.
Upvotes: 4
Reputation: 51
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.
Upvotes: 0
Reputation: 1826
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);
Upvotes: 1
Reputation: 11087
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)));
Upvotes: 1
Reputation: 7365
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);
Upvotes: 20
Reputation: 117675
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
Upvotes: 7