Can anyone tell me that, In java how to return int array with square brackets like for python we use list then how to do java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone tell me that, In java how to return int array with square brackets like for python we use list then how to do java

13th Feb 2022, 1:58 PM
Onkar Ambuse
Onkar Ambuse - avatar
9 Answers
+ 3
Put the array as the return type of the function, like this. It can be also not static, if you instantiate your class. static int[] getNumbers() https://code.sololearn.com/cwFCAiLQ82W5/?ref=app
13th Feb 2022, 2:14 PM
Tibor Santa
Tibor Santa - avatar
+ 3
1. Java is not Python, Java code is typically more verbose. Generally, trying to compress a program to one line, and breaking the usual style conventions, is a bad idea. 2. The problem statement on Leetcode is sort of pseudocode. Array syntax in Java is different. You can find lots of ways to declare Java array, here: https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java
13th Feb 2022, 2:33 PM
Tibor Santa
Tibor Santa - avatar
+ 2
You can try this way : int[] result = { 1,2 } ; System.out.print(java.util.Arrays.toString( result ));
13th Feb 2022, 2:57 PM
Jayakrishna 🇮🇳
+ 2
Thank you Jayakrishna🇮🇳 I got output thank you
13th Feb 2022, 3:18 PM
Onkar Ambuse
Onkar Ambuse - avatar
+ 1
Thank you so much brother
13th Feb 2022, 2:23 PM
Onkar Ambuse
Onkar Ambuse - avatar
+ 1
You're welcome.. Onkar Ambuse
13th Feb 2022, 3:19 PM
Jayakrishna 🇮🇳
0
Actually I'm saying like this [1, 2,3,4] How to return this from function in java With square brackets and on one line
13th Feb 2022, 2:25 PM
Onkar Ambuse
Onkar Ambuse - avatar
0
I have facing this for the following Leetcode problem https://leetcode.com/problems/two-sum
13th Feb 2022, 2:27 PM
Onkar Ambuse
Onkar Ambuse - avatar
0
Ok thank you🙏
13th Feb 2022, 2:39 PM
Onkar Ambuse
Onkar Ambuse - avatar