How do I fill arrays in Java?

后端 未结 8 1731
旧巷少年郎
旧巷少年郎 2020-11-27 14:09

I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn\'t get anything he

8条回答
  •  情书的邮戳
    2020-11-27 14:51

    An array can be initialized by using the new Object {} syntax.

    For example, an array of String can be declared by either:

    String[] s = new String[] {"One", "Two", "Three"};
    String[] s2 = {"One", "Two", "Three"};
    

    Primitives can also be similarly initialized either by:

    int[] i = new int[] {1, 2, 3};
    int[] i2 = {1, 2, 3};
    

    Or an array of some Object:

    Point[] p = new Point[] {new Point(1, 1), new Point(2, 2)};
    

    All the details about arrays in Java is written out in Chapter 10: Arrays in The Java Language Specifications, Third Edition.

提交回复
热议问题