2

I would like to convert the elements of an array of integers into a single integer. Note that I haven't learn the join or number methods in class yet so they cannot be use for this program.

For example, {1, 2, 3} would become 123.

Here is my thought of process:

  1. Convert the first element of the array into a String
  2. Concatenate the remaining elements
  3. Use Integer.parseInt() to convert the String back to an integer

Can anyone confirm I am doing this the right way or suggest an alternative way?

Also, by curiosity, how could we compare the elements of two integer arrays without using the class Arrays?

6
  • Hi, you want to sum them ? Or you want to convert element of integer to string and concatanate them and parse the result value to int ? Commented Mar 11, 2019 at 23:53
  • If the assignment doesn't specify how you need to do this conversion, then there are a few much easier ways to do it. The first one is return 1; - it converts any array to a single integer. The second one links the result to the array to a degree: return array.length;. If your assignment does say how, then you forgot to mention that in your question. Commented Mar 11, 2019 at 23:57
  • @MykhailoMoskura No, I do not want to sum them, just concatenate them. For example, {1, 2, 3} would become 123. Commented Mar 11, 2019 at 23:57
  • 1
    See String::concat. Commented Mar 12, 2019 at 0:00
  • Provide further detail as edits to your Question rather than as Comments. Commented Mar 12, 2019 at 0:04

2 Answers 2

1

This is how it can be done using java 8:

 final int[] array = new int[]{1,2,3};
   final String result = IntStream
                    .of(array)
                    .boxed()
                    .map(String::valueOf)
                    .collect(Collectors.joining());
   final int intResult = Integer.parseInt(result);

I also would add check if array contains one element for example

 public int arrayOfIntsAsSingleInt ( final int[] array){
            if (Objects.isNull(array) || array.length == 0) {
                throw new IllegalArgumentsException("array could not be null or empty")
            } else if (array.length == 1) {
                return array[0];
            }
            return convertArraysofIntAsSingleInt(array);
        }

We can compare two integers by writing custom Comparator or

invoke Integer.compareTo(anotherInteger) method

Sign up to request clarification or add additional context in comments.

7 Comments

I would like to compare the elements inside the array not integers
Why do you need it ? The problem what you want to solve?
I am just curious
What do you mean by inside an array ?
I want to compare the elements inside an array (numbers inside the array) such that {1, 2, 3} and {1, 2, 3} would return true while {4, 5, 6} and {6, 5, 4}
|
1

Can anyone confirm I am doing this the right way

If you input is valid,your method is right to do what you want. @Mykhailo Moskura give a method use Java 8 Stream API to join string.

suggest an alternative way?

if you element in your input array within a certain range(0,9) and the first element must not be 0,you can try this code: int[] inputs = {1, 2, 3};

    int factor = 0;
    int result = 0;
    for (int i = 0; i < inputs.length; i++) {
        result = inputs[i] + factor * 10;
        factor = result;
    }

    System.out.println(result);

the output is: 123

compare two array not use Arrays

Assume not use any tool,you can use two map to compare two arrays:

public static boolean compareArray(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }

        Map<Integer, Integer> map1 = new HashMap<>();
        Map<Integer, Integer> map2 = new HashMap<>();
        for (int arr : arr1) {
            Integer times = map1.getOrDefault(arr, 0);
            map1.put(arr, times + 1);
        }
        for (int arr : arr2) {
            Integer times = map2.getOrDefault(arr, 0);
            map2.put(arr, times + 1);
        }

        for (Integer key : map1.keySet()) {
            if (!map1.get(key).equals(map2.get(key))) {
                return false;
            }
        }
        return true;
    }

4 Comments

Hi , it can be done with less code just iterating through array.length and comparing them using index from cycle
compare two arrays:array1={1,2,3,2},array2={2,1,2,3},they are the same array,can we solve this?
Same array is array which have the same order of elements
Not actually O(n) , if would be collision worst case would be like O(n^2) , you have not mentioned this case because big o notation is worst case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.