Fastest way to set all values of an array?

后端 未结 14 2249
悲哀的现实
悲哀的现实 2020-12-04 17:47

I have a char [], and I want to set the value of every index to the same char value.
There is the obvious way to do it (iteration):

<         


        
14条回答
  •  有刺的猬
    2020-12-04 18:07

    Arrays.fill(myArray, 'c');

    Arrays.fill

    Although it is quite possible that this is doing the loop in the background and is therefore not any more efficient than what you have (other than the lines of code savings). If you really care about efficiency, try the following in comparison to the above:

    int size = 50;
    char[] array = new char[size];
    for (int i=0; i

    Notice that the above doesn't call array.size() for each iteration.

提交回复
热议问题