Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #303 Array with non-default lower bound

Declare an array a of integers with six elements, where the first index is 42 and consecutive elements have the indices 43, 44, 45, 46, 47.

A : array (42 .. 47) of Integer;
  integer, dimension(42:47) :: a
import java.util.ArrayList;
class A<T> extends ArrayList<T> {
    int i;
    A(int lowerBound) { i = lowerBound; }
    public void add(int i, T x) {
        super.add(i - this.i, x);
    }
    public T set(int i, T x) {
        return super.set(i - this.i, x);
    }
    public T get(int i) {
        return super.get(i - this.i);
    }
}
var
  a: array[42..47] of integer;
use feature 'say';
use Array::Base +42;

my @a = ('A'..'Z');

say $a[42]; # prints A
say $a[43]; # prints B

no Array::Base;  # restore indexing to base 0
import array
a = array.array("i", range(42,48))

New implementation...
< >
tkoenig