Proteome Informatics Group > Java Proteomic Library
 

Recipe 0.3. Making integer sequences with gap(s)

Problem

You want to create a sequence of integers with gap(s).

Solution

Here is a code that build a gapped sequence of integers:

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

// [11, 20[
IntegerSequence seq2 =
	new IntegerSequence.Builder(11, 20).build();
	
// new instance
IntegerSequenceWithGap.newInstance seq =
	IntegerSequenceWithGap.newInstance();

// add sequences
seq.add(seq1);
seq.add(seq2);

// the sequence of integers can be extracted
int[] ints = seq.toInts();

// seq: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19
Assert.assertEquals(18, seq.size());

you can also build it from integers:

// the gapped sequence of integers 
int[] ints = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14,
 15, 16, 17, 18, 19};
 
// the sequence with gap
IntegerSequenceWithGap seq =
	IntegerSequenceWithGap.fromIntegers(ints, 1);

Discussion

See Also

See also the sequence of integer cookbook.