How to initialize all the elements of an array to any specific value in java

后端 未结 10 1477
故里飘歌
故里飘歌 2020-12-12 17:33

In C/C++ we have memset() function which can fulfill my wish but in Java how can i initialize all the elements to a specific value? Wh

10条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 18:30

    If it's a primitive type, you can use Arrays.fill():

    Arrays.fill(array, -1);
    

    [Incidentally, memset in C or C++ is only of any real use for arrays of char.]

提交回复
热议问题