0

Array

Please why am I getting an error after running this code?? public class program{ public static void main(String[] args){ String[ ] myClass = {"a", "b"}; System.out.println(myClass[ ]); } }

13th Mar 2021, 11:01 AM
Emmanuel Manu
Emmanuel Manu - avatar
7 Answers
+ 1
Manuel🔳 What's the error that you're getting? Mine is working fine
13th Mar 2021, 11:43 AM
Soumik
Soumik - avatar
+ 1
Manuel🔳 You can also print an array along with its constituent elements using Arrays.toString(). Normally, when you print any reference data type, it outputs the address of the variable in Heap (this is not the case if the toString() method is overridden). https://code.sololearn.com/cESkEaiKerJ7/?ref=app
13th Mar 2021, 11:50 AM
Soumik
Soumik - avatar
+ 1
thanks bro actually that was what i was looking for and i thought it was impossible.. thanks
13th Mar 2021, 11:54 AM
Emmanuel Manu
Emmanuel Manu - avatar
+ 1
Manuel🔳 You're welcome 😄
13th Mar 2021, 11:56 AM
Soumik
Soumik - avatar
0
Don't use brackets to print the array.
13th Mar 2021, 11:25 AM
Soumik
Soumik - avatar
0
still error and this time around 2 errors
13th Mar 2021, 11:42 AM
Emmanuel Manu
Emmanuel Manu - avatar
0
i actually solved it, with value 1 inside the [ ].. i went back to the definition of an Array to get this right so it should rather be public class program{ public static void main(String[] args){ String[ ] myClass = {"a", "b"}; System.out.println(myClass[1]); } }
13th Mar 2021, 11:47 AM
Emmanuel Manu
Emmanuel Manu - avatar