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.
Asked
Active
Viewed 274 times
-2

user3500198
- 57
- 1
- 5
-
Do not delete and repost questions. – Raedwald Sep 26 '19 at 14:08
-
@user3500198 Try it yourself the compiler will tell you if its possible. – Sep 26 '19 at 14:08
-
Are you asking whether the syntax is valid (it's not), or how you could create such a structure logically? – Gus Sep 26 '19 at 14:10
1 Answers
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