Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to set all values of an array?

Tags:

java

arrays

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):

  char f = '+';   char [] c = new char [50];   for(int i = 0; i < c.length; i++){       c[i] = f;   } 

But I was wondering if there's a way that I can utilize System.arraycopy or something equivalent that would bypass the need to iterate. Is there a way to do that?

EDIT : From Arrays.java

public static void fill(char[] a, int fromIndex, int toIndex, char val) {         rangeCheck(a.length, fromIndex, toIndex);         for (int i = fromIndex; i < toIndex; i++)             a[i] = val;     } 

This is exactly the same process, which shows that there might not be a better way to do this.
+1 to everyone who suggested fill anyway - you're all correct and thank you.

like image 245
rtheunissen Avatar asked Feb 03 '12 12:02

rtheunissen


People also ask

How do you set all values in an array?

Use the fill() method to set the elements on an array to a specific value, e.g. new Array(3). fill('example') creates an array of 3 elements with the value example . The fill method changes the elements in an array setting them to a static value and returns the modified array. Copied!

How do I change all the values in an array Java?

In order to replace all elements of ArrayList with Java Collections, we use the Collections. fill() method. The static void fill(List list, Object element) method replaces all elements in the list with the specified element in the argument.

How do you assign all elements to an array to zero?

If your array has static storage allocation, it is default initialized to zero. However, if the array has automatic storage allocation, then you can simply initialize all its elements to zero using an array initializer list which contains a zero.


2 Answers

Try Arrays.fill(c, f) : Arrays javadoc

like image 127
Kurt Du Bois Avatar answered Sep 25 '22 01:09

Kurt Du Bois


As another option and for posterity I was looking into this recently and found a solution that allows a much shorter loop by handing some of the work off to the System class, which (if the JVM you're using is smart enough) can be turned into a memset operation:-

/*  * initialize a smaller piece of the array and use the System.arraycopy   * call to fill in the rest of the array in an expanding binary fashion  */ public static void bytefill(byte[] array, byte value) {   int len = array.length;    if (len > 0){     array[0] = value;   }    //Value of i will be [1, 2, 4, 8, 16, 32, ..., len]   for (int i = 1; i < len; i += i) {     System.arraycopy(array, 0, array, i, ((len - i) < i) ? (len - i) : i);   } } 

This solution was taken from the IBM research paper "Java server performance: A case study of building efficient, scalable Jvms" by R. Dimpsey, R. Arora, K. Kuiper.

Simplified explanation

As the comment suggests, this sets index 0 of the destination array to your value then uses the System class to copy one object i.e. the object at index 0 to index 1 then those two objects (index 0 and 1) into 2 and 3, then those four objects (0,1,2 and 3) into 4,5,6 and 7 and so on...

Efficiency (at the point of writing)

In a quick run through, grabbing the System.nanoTime() before and after and calculating a duration I came up with:-

  • This method : 332,617 - 390,262 ('highest - lowest' from 10 tests)
  • Float[] n = new Float[array.length]; //Fill with null : 666,650
  • Setting via loop : 3,743,488 - 9,767,744 ('highest - lowest' from 10 tests)
  • Arrays.fill : 12,539,336

The JVM and JIT compilation

It should be noted that as the JVM and JIT evolves, this approach may well become obsolete as library and runtime optimisations could reach or even exceed these numbers simply using fill(). At the time of writing, this was the fastest option I had found. It has been mentioned this might not be the case now but I have not checked. This is the beauty and the curse of Java.

like image 22
Ross Drew Avatar answered Sep 24 '22 01:09

Ross Drew