Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Initialise
Insert
Count
Retrieve by index
Slice
Search
Sort
Compare
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
Java Arrays
Initialise
Fixed contents n/a int a[]={1,2,3,4,5};
1 2 3 4 5
Repeat value 'e' 'n' times e=3;
n=5;
int a[]=new int[5];
Arrays.fill(a,e);
3 3 3 3 3
Insert
Prepend element 'e' int a[]={2,3,4};
e=1;
a=Arrays.copyOf(a,a.length+1);
System.arraycopy(a,0,a,1,a.length-1);
a[0]=e;
1 2 3 4
Append element 'e' int a[]={1,2,3};
e=4;
a=Arrays.copyOf(a,a.length+1);
a[a.length-1]=e;
1 2 3 4
Insert element 'e' at index 'n' int a[]={1,2,3,4,5};
e=666;
n=3;
a=Arrays.copyOf(a,a.length+1);
System.arraycopy(a,n,a,n+1,a.length-n-1);
a[n]=e;
1 2 3 666 4 5
Prepend array 'b' int a[]={1,2,3,4,5};
int b[]={10,20,30};
a=Arrays.copyOf(a,a.length+b.length);
System.arraycopy(a,0,a,b.length,a.length-b.length);
System.arraycopy(b,0,a,0,b.length);
10 20 30 1 2 3 4 5
Append array 'b' int a[]={1,2,3,4,5};
int b[]={10,20,30};
a=Arrays.copyOf(a,a.length+b.length);
System.arraycopy(b,0,a,a.length-b.length,b.length);
1 2 3 4 5 10 20 30
Insert array 'b' at index 'n' int a[]={1,2,3,4,5};
int b[]={10,20,30};
n=3;
a=Arrays.copyOf(a,a.length+b.length);
System.arraycopy(a,n,a,n+b.length,a.length-n-b.length);
System.arraycopy(b,0,a,n,b.length);
1 2 3 10 20 30 4 5
Concatenate array 'a' and array 'b' int a[]={1,2,3};
int b[]={4,5,6};
int c[]=new int[a.length+b.length];
System.arraycopy(a,0,c,0,a.length);
System.arraycopy(b,0,c,a.length,b.length);
1 2 3 4 5 6
Insert element 'e' at indices in 'b' int a[]={1,2,3,4,5};
int b[]={0,2,4};
e=666;
for (n=b.length-1;n>=0;--n)
{
  // copy with extra element
  a=Arrays.copyOf(a,a.length+1);
  // create gap
  System.arraycopy(
    a, // src
    b[n], // srcPos
    a, // dest
    b[n]+1, // destPost
    a.length-b[n]-1); // len
  // set element in gap
  a[b[n]]=e;
}
666 1 2 666 3 4 666 5
Overwrite
Overwrite first element int a[]={1,2,3};
a[0]=666;
666 2 3
Overwrite last element int a[]={1,2,3};
a[a.length-1]=666;
1 2 666
Overwrite element at index 'n' int a[]={1,2,3};
n=1;
a[n]=666;
1 666 3
Overwrite elements from index 'n' (inclusive) int a[]={1,2,3,4,5,6};
int b[]={7,8,9};
n=2;
System.arraycopy(b,0,a,n,b.length);
1 2 7 8 9 6
Count
Length of array int a[]={1,2,3};
n=a.length;
3
Retrieve by index
Retrieve first element int a[]={1,2,3};
e=a[0];
1
Retrieve last element int a[]={1,2,3};
e=a[a.length-1];
3
Retrieve element 'n' int a[]={1,2,3};
n=2;
e=a[n];
3
Retrieve out-of-bounds element 'n' int a[]={1,2,3};
n=4;
String err="in bounds";
try
{
  e=a[n];
}
catch ( ArrayIndexOutOfBoundsException ex )
{
  err="out of bounds";
}
out of bounds
Slice
Slice from index 'm' to 'n' (inclusive) int a[]={1,2,3,4,5};
m=1;
n=3;
a=Arrays.copyOfRange(a,m,n+1);
2 3 4
Slice from index 'n' onwards (inclusive) int a[]={1,2,3,4,5};
n=2;
a=Arrays.copyOfRange(a,n,a.length);
3 4 5
Slice up to index 'n' (inclusive) int a[]={1,2,3,4,5};
n=3;
a=Arrays.copyOfRange(a,0,n+1);
1 2 3 4
Search
Find index of unique element 'e' int a[]={1,2,3,4,5};
e=3;
Arrays.sort(a);
n=Arrays.binarySearch(a,e);
2
Sort
Sort in ascending order int a[]={5,1,4,3,2};
Arrays.sort(a);
1 2 3 4 5
Sort index range 'm' to 'n' (inclusive) int a[]={5,1,4,3,2};
m=1;
n=3;
Arrays.sort(a,m,n+1);
5 1 3 4 2
Compare
Test for equality (true) int a[]={1,2,3,4,5};
int b[]={1,2,3,4,5};
t=Arrays.equals(a,b);
1
Test for equality (false) int a[]={1,2,3,4,5};
int b[]={1,20,3,4,5};
t=Arrays.equals(a,b);
0