int array to int number in Java

33,351

Solution 1

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.

  • 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

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

Solution 2

You have to cycle in the array and add the right value. The right value is the current element in the array multiplied by 10^position.

So: ar[0]*1 + ar[1]*10 + ar[2] *100 + .....

    int res=0;
    for(int i=0;i<ar.length;i++) {
         res=res*10+ar[i];
    }

Or

   for(int i=0,exp=ar.length-1;i<ar.length;i++,exp--)
        res+=ar[i]*Math.pow(10, exp);

Solution 3

First you'll have to convert every number to a string, then concatenate the strings and parse it back into an integer. Here's one implementation:

int arrayToInt(int[] arr)
{
    //using a Stringbuilder is much more efficient than just using += on a String.
    //if this confuses you, just use a String and write += instead of append.
    StringBuilder s = new StringBuilder(); 

    for (int i : arr)
    {
         s.append(i); //add all the ints to a string
    }

    return Integer.parseInt(s.toString()); //parse integer out of the string
}

Note that this produce an error if any of the values past the first one in your array as negative, as the minus signs will interfere with the parsing.

This method should work for all positive integers, but if you know that all of the values in the array will only be one digit long (as they are in your example), you can avoid string operations altogether and just use basic math:

int arrayToInt(int[] arr)
{
    int result = 0;

    //iterate backwards through the array so we start with least significant digits
    for (int n = arr.length - 1, i = 1; n >= 0; n --, i *= 10) 
    {
         result += Math.abs(arr[n]) * i;
    }

    if (arr[0] < 0) //if there's a negative sign in the beginning, flip the sign
    {
        result = - result;
    }

    return result;
}

This version won't produce an error if any of the values past the first are negative, but it will produce strange results.

There is no builtin function to do this because the values of an array typically represent distinct numbers, rather than digits in a number.

EDIT:

In response to your comments, try this version to deal with longs:

long arrayToLong(int[] arr)
{
    StringBuilder s = new StringBuilder(); 

    for (int i : arr)
    {
         s.append(i);
    }

    return Long.parseLong(s.toString());
}

Edit 2:

In response to your second comment:

int[] longToIntArray(long l)
{
    String s = String.valueOf(l);   //expand number into a string

    String token;

    int[] result = new int[s.length() / 2]; 

    for (int n = 0; n < s.length()/2; n ++) //loop through and split the string
    {
        token = s.substring(n*2, (n+2)*2);
        result[n] = Integer.parseInt(token); //fill the array with the numbers we parse from the sections
    }

    return result;
}

Solution 4

yeah you can write the function yourself

int toInt(int[] array) {
    int result = 0;
    int offset = 1;
    for(int i = array.length - 1; i >= 0; i--) {
        result += array[i]*offset;
        offset *= 10;
    }
    return result;
}

I think the logic behind it is pretty straight forward. You just run through the array (last element first), and multiply the number with the right power of 10 "to put the number at the right spot". At the end you get the number returned.

Solution 5

int nbr = 0;
for(int i = 0; i < ar.length;i++)
    nbr = nbr*10+ar[i];

In the end, you end up with the nbr you want.

For the new array you gave us, try this one. I don't see a way around using some form of String and you are going to have to use a long, not an int.

int [] ar = {2, 15, 14, 10, 15, 21, 18};
long nbr = 0;
double multiplier = 1;
for(int i = ar.length-1; i >=0 ;i--) {
    nbr += ar[i] * multiplier;
    multiplier = Math.pow(10, String.valueOf(nbr).length());
}

If you really really wanted to avoid String (don't know why), I guess you could use

multiplier = Math.pow(10,(int)(Math.log10(nbr)+1));

which works as long as the last element in the array is not 0.

Share:
33,351
gotgot1995
Author by

gotgot1995

By day: Software designer, developer and sometimes sys-admin By night: multilingual pianist I really enjoy the StackExchange platform because you can always find relevant info.

Updated on September 24, 2020

Comments

  • gotgot1995
    gotgot1995 over 3 years

    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.