+ 1

Just curious why a new Array String [0] wont work?

/*i know it will work with another number but i thought [0] is an array number. thanks*/ public class Program { static void println(String y){System.out.println(y);} public static void main(String[] args) { String [ ] x = new String[0]; x[0] = "Hello"; println(x[0]); x[0] = "World"; println(x[0]); } }

29th Aug 2017, 9:40 AM
D_Stark
D_Stark - avatar
5 ответов
+ 13
it is of zero size. (no elements) i think you mean to say string[1]
29th Aug 2017, 9:41 AM
jay
jay - avatar
+ 9
the 0 used with x is a reference to the first element of the array. the 0 you have used with string declares the number of elements in the array
29th Aug 2017, 10:00 AM
jay
jay - avatar
+ 3
ah so new string [1] < is referenced as element [0] in the array 😉 thanks
29th Aug 2017, 10:15 AM
D_Stark
D_Stark - avatar
+ 1
@jay, even with "new String [1]"it still throws an error i need at least "new String [2]" for some reason and i can create as many arrays as i want??. i thought 0 was 1 array it seems like that in a single array. if im assigning only 1 declared array i dont understand why i need 2 or would that be 3 lol.
29th Aug 2017, 9:55 AM
D_Stark
D_Stark - avatar