(Not logged on) | Log On
View  History

  

Initializing arrays

1/24/2008 1:36 PM
You can subscribe to this wiki article using an RSS feed reader.

[Up to jsr166y.forkjoin Examples]

Initializing data in a Parallel Array

There are several ways to inject data into a Parallel Array. You can assign a single value to all elements, or you can add a primitive array or the contents of another Parallel Array,

The examples below use ParallelDoubleArray, but analogous operation exist for ParallelArray<T> and ParallelLongArray. We will assume you are familiar with Creating arrays and have a ParallelDoubleArray reference, pda.

Assign a single value to all elements

pda.replaceWithValue(0.0);

Add a primitive array or another Parallel Array

The addAll methods work like java.util collections: they add the content of another array to a Parallel Array. (addAll() does not perform numeric addition.)

// adds the elements of a double[] to the end of a Parallel Array
double[] array = // some operation to obtain a double[]
pda.addAll(array);

// Given an existing Parallel Array 'pda', we can constuct
// another Parallel Array 'other'
// with the same contents. However, the two arrays are not linked.
ParallelDoubleArray other = ParallelDoubleArray.createFromCopy(pda.size, pda.getExecutor())
other.addAll(pda);

See also Modifying arrays.

David Biesack