-1

This question was asked to me in Razorpay. I could not come up with a solution. Can anyone help me in writing Java code for this.

Object[] array = { 1, 2, new Object[]{ 3, 4, new Object[]{ 5 }, 6, 7 }, 8, 9, 10};

Answer should be:

Integer[] = {1,2,3,4,5,6,7,8,9,10};

i.e. all the Integer elements should be stored

What I did is

for(Object obj: array){
   if(obj instanceof Integer) list.add((int)(obj));
}

Which results in -> 1,2,8,9,10. How do I add 3,4,5,6,7 inside list?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

3 Answers3

1

As there's no finite depth of nesting of Object[] you'll need a recursive approach:

import java.util.ArrayList;
import java.util.List;

public class Answer {
    static void extractIntegers(Object[] source, List<Integer> destination) {
        for (Object i : source) {
            if (i instanceof Object[] array) {
                extractIntegers(array, destination);
            } else if (i instanceof Integer integer) {
                destination.add(integer);
            } else {
                throw new IllegalArgumentException("Unexpected: " + i);
            }
        }
    }

    public static void main(String[] args) {
        List<Integer> ints = new ArrayList<>();
        Object[] array = { 1, 2, new Object[]{ 3, 4, new Object[]{ 5 }, 6, 7 }, 8, 9, 10};
        extractIntegers(array, ints);
        System.out.println(ints);
    }
}

Note that I'm using the recently added "pattern matching for instanceof" feature of Java. You could ignore objects which are not Object[] or Integer. I've chosen to throw an excpetion.

tgdavies
  • 10,307
  • 4
  • 35
  • 40
1

The operation you are looking for is called flattening an array. Your input is an array of Object which can consist of either Integer or Object[]. So we have to handle both cases here, and it is easier done with recursion:

Write a function flatten(Object[] arr) that takes in an Object[] as parameter. The function will return List<Integer> which is the result after arr is flattened.

The logic is simple for the recursive flatten() function:

create empty result_array

for each obj in Object[]:
   if obj is an Integer:
      add obj to the result_array
   else obj is Object[]:
      flat_obj := flatten(obj)
      add all integers of flat_obj into result_array

return result_array

Here are the Java code for the above logic implemented. Hope it helps!

public class Test {
    public static List<Integer> flatten(Object[] array) {
        List<Integer> result = new ArrayList<>();

        for (Object o: array) {
            if (o instanceof Object[])
                result.addAll( flatten( (Object[]) o) );
            else 
                result.add((Integer) o);
        }

        return result;
    }

    public static void main(String[] args) {
        Object[] array = { 1, 2, new Object[]{ 3, 4, new Object[]{ 5 }, 6, 7 }, 8, 9, 10};    
        System.out.println( flatten(array) );
    }
}
AdmiJW
  • 83
  • 6
-2

You're only checking for integers, but you also have arrays in your array. Together with the instanceof Integer check, you should also add a check on instanceof Object[]. Be careful that also the Object[] contains an Object[]. So you'll have to have the same check inside of it as well. You shall have 3 loops in the end, each of which will have checks on instanceof Integer and instanceof Object[]

Marco
  • 322
  • 1
  • 3
  • 15