-3

Here is my array, it consists of array of integers. Actually, these are the key of my HashMap which consists of some sentences or say "STRING" of a complete paragraph as a key value pair. Now I wanted to join those sentences from taking the key from the integer array one after another in given order.

 int[] arr = {3, 2, 0, 5, 3};

 HashMap<Integer, String> h = new HashMap<Integer, String>() {{
      put(0,"This is my third sentence.");
      put(3,"This is my first sentence.");
      put(5,"This is my forth sentence.");
      put(2,"This is my second sentence.");
 }};

The final output should be all the sentences combined as mentioned order and outout should be like a paragraph as :

This is my first sentence.This is my second sentence.This is my third sentence. This is my forth sentence.This is my first sentence.

Rabin Pantha
  • 941
  • 9
  • 19

9 Answers9

3

Instead of converting the value to a character type you can perform math. For each digit in the array, the corresponding power of 10 is the array length (minus one) minus the index (because Java arrays use 0 based indexing and the last digit corresponds to 100). Something like,

int[] arr = { 3, 2, 0, 5, 3 };
int result = 0;
for (int i = 0; i < arr.length; i++) {
    result += arr[i] * Math.pow(10, arr.length - i - 1);
}
System.out.println(result);

Output is (as expected)

32053
Optimization

It's possible to optimize the code further by keeping the current power of ten and dividing 10 while iterating each digit. This would also allow the use of a for-each loop like

int[] arr = { 3, 2, 0, 5, 3 };
int result = 0;
int pow = (int) Math.pow(10, arr.length - 1);
for (int digit : arr) {
    result += digit * pow;
    pow /= 10;
}
System.out.println(result);

Alternatively, iterate the digits from right to left and multiply pow by 10 on each iteration. That might look something like,

int result = 0;
int pow = 1;
for (int i = arr.length - 1; i >= 0; i--) {
    result += arr[i] * pow;
    pow *= 10;
}

And the above might also be written like

int result = 0;
for (int i = arr.length - 1, pow = 1; i >= 0; i--, pow *= 10) {
    result += arr[i] * pow;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    I'm not sure but I would guess that the pow call is expensive. I would have just multiple it by 10 in each loop. – rekire May 04 '16 at 04:21
  • @rekire True enough, I added a more *optimized* version. – Elliott Frisch May 04 '16 at 04:32
  • 1
    Better yet, iterate using a for loop (instead of a for each) from last index to first and multiply by ten each time - that way you don't have to find the final power of ten you need for the first index, thus eliminating the `Math.pow` call entirely. – MercyBeaucou May 04 '16 at 04:52
2
int number = Integer.parseInt(Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining()));
shmosel
  • 49,289
  • 6
  • 73
  • 138
2

Yet another way:

int[] arr = {3, 2, 0, 5, 3};
int i = Integer.parseInt(Arrays.toString(arr).replaceAll("[\\[,\\] ]", ""));
System.out.println(i); // prints 32053
Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
1

Though fairly simple, you should have tried yourself. Still providing a solution, just debug and understand it.

Working Code

public static void main(String[] args) throws Exception {
        int[] arr = {3, 2, 0, 5, 3};

        StringBuilder numberStr = new StringBuilder();
        for (int item : arr) {
            numberStr.append(item);
        }
        int finalInt = Integer.parseInt(numberStr.toString());
        System.out.println(finalInt);
    }

Output

32053
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
1

First convert the array into string by appending elements one by one and the convert string into integer. Try this code:

 public class NewClass63 {
public static void main(String args[]){
int[] arr = {3, 2, 0, 5, 3};
StringBuffer s = new StringBuffer();
   for(int i=0;i<arr.length;i++){
       s.append(arr[i]);
   }
int x = Integer.parseInt(s.toString());
    System.out.println(x);
}
}
Kaushal28
  • 5,377
  • 5
  • 41
  • 72
1

use a loop:

      int[] arr = { 3, 2, 0, 5, 3 };
        String itotal = "";
        for (int i = 0; i < arr.length; i++)
        {
           itotal=itotal + String.valueOf(arr[i]);
        }
        int a = Integer.parseInt(itotal);
D T
  • 3,522
  • 7
  • 45
  • 89
1
int[] array = {3,2,0,5,3};
String x = "";
for(int i = 0;i<=array.length-1;i++){
    x = x + String.valueOf(array[i]);
}
System.out.println(Integer.parseInt(x));
user2966968
  • 133
  • 1
  • 9
0

There exist various ways. If I am right, hidden assumption is that the higher element of integer array matches with higher digit of result integer.

int[] arr = {3, 2, 0, 5, 3};
int result = 0;
int power = (int) Math.pow(10, arr.length-1);
for(int element : arr){
    result += element * power;
    power /= 10;
}
0

Easiest solution answer is this. Assume, each alphabet in the example is a single digit.

  1. Empty Array {} : Expected value = 0
  2. {a}: Expected value = a
  3. {a,b}: Expected value = 10*a + b
  4. {a,b,c}: Expected value = 10 * (10*a + b) + c

Code: Test this is online java compiler IDE

public class ConvertDigitArrayToNumber {
  public static void main(String[] args) {
    int[] arr = {3, 2, 0, 5, 3};
    int value = 0;
    for (int i = 0; i < arr.length; i++) { 
      value = (10*value) + arr[i]; 
    }
    System.out.println(value);
  }
}

This is actually simpler and better solution than the other ones.

  • Only simple multiplication and addition (No powers).
  • No String conversions
JackDaniels
  • 985
  • 9
  • 26