|
|
|
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) { a=Arrays .copyOf(a ,a .length +1) ; System .arraycopy(a ,b[n] ,a ,b[n]+1,a.length-b[n]-1); 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 element 'e' (forward)
|
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
|
|