How do I fill arrays in Java?

后端 未结 8 1670
旧巷少年郎
旧巷少年郎 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:59

    Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false.

    To fill the array with something else you can use Arrays.fill() or as part of the declaration

    int[] a = new int[] {0, 0, 0, 0};
    

    There are no shortcuts in Java to fill arrays with arithmetic series as in some scripting languages.

    0 讨论(0)
  • 2020-11-27 15:11

    Arrays.fill(). The method is overloaded for different data types, and there is even a variation that fills only a specified range of indices.

    0 讨论(0)
提交回复
热议问题