Proteome Informatics Group > Java Proteomic Library
 

Recipe 0.2. Making integer sequences

Problem

You want to create a sequence of integers.

Solution

Here is a code that build a sequence of integers given a jpl interval (see the section see also):

// [0, 10[
IntegerSequence seq = new IntegerSequence.Builder(0, 10).build();

// a sequence of integers can be converted in integers
int[] ints = seq.toInts();

// a sequence of odd integers
seq = new IntegerSequence.Builder(1, 10).by(2).build();

// seq: 1, 3, 5, 7, 9
Assert.assertEquals(5, seq.size());

It is also possible to create a sequence from an integer interval:

// interval: [0, 10[
Interval interval = new Interval.Builder(0, 10).build();

// build a sequence from the previous interval
IntegerSequence seq = new IntegerSequence.Builder(interval).build();

// generate integers
Assert.assertTrue(Arrays.equals(
	new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, seq.toInts()));

Discussion

See Also

See also the recipe about intervals.