-2

I've been asked this question twice and i got confused either the question is correct or not. i searched and found nothing. Object[] arr = {1,2,[3,4,[5]],6}; The question was to add all the objects of the above mentioned array.

user3500198
  • 57
  • 1
  • 5

1 Answers1

2

No. That is not a valid syntax in Java.

You could do this though:

Object[] arr = new Object[] {
    new Integer(1), 
    new Integer(2),
    new Object[] {
        new Integer(3),
        new Integer(4),
        new Object[] {
            new Integer(5)
        }
    },
    new Integer(6)
};

In python you can do:

arr = [1, 2, [3,4,[5]],6];

In JavaScript:

let arr = [1, 2, [3,4,[5]],6];
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292