Theory:Array
# Theory:Array
# Create an array
# Declaration
int[] numbers;
// int numbers[]; less used in parctice
2
3
Declare the array variable's type and name. We must use two special characters []
after the type or the variable's name.
# Instantiation
int n = ...; // n is a length of an array
int[] number; // declaration
number = new int[n]; // instantiation
2
3
4
The new
keyword represents instantiating the object. It depends on the array's type and length.
提示
The size of an array cannot be greater than Integer.MAX_VALUE
. Actually, it is even slightly smaller than this value.
注意
Instantiation happens when memory is allocated for this object.
If you don't assign some values for the array, will use the default values of its type.
For example, the int
type's default value is 0
(zero) and the String
type's default value is null
.
So, at instantiation phases, exists implicit initialize action.
# Initialization
int n = ...; // n is a length of an array
int[] number; // declaration
number = new int[n]; // instantiation
number = { 1, 2, 3, 4 }; // initialization
2
3
4
5
# General write style
Yeah, we can separate declaration, instantiation, and initialization like above. Also, we can join them and put in fewer lines:
int[] number = new int[4];
number = { 1, 2, 3, 4 };
2
Or this:
int[] number = new int[] { 1, 2, 3, 4 };
Or even like this:
int[] numbers = { 1, 2, 3, 4 };
# To obtain the length of the array
int[] array = { 1, 2, 3, 4 }; // an array of numbers
int length = array.length; // number of elements of the array
System.out.println(length); // 4
2
3
4
5
# Accessing elements
Set the value by the index:
array[index] = val;
Get the value by the index:
val = array[index];
Indexes of an array have numbers from 0 to length – 1 inclusive.
Let's see an example.
int[] numbers = new int[3]; // numbers: [0, 0, 0]
numbers[0] = 1; // numbers: [1, 0, 0]
numbers[1] = 2; // numbers: [1, 2, 0]
numbers[2] = numbers[0] + numbers[1]; // numbers: [1, 2, 3]
2
3
4
提示
If we try to access a non-existing element by an index then a runtime exception occurs.
ArrayIndexOutOfBoundsException
# The utility class Arrays
If you need to process arrays, you can import and use standard methods grouped in the utility class Arrays
.
- convert an array to string using
Arrays.toString(array)
and then print it:
byte[] famousNumbers = { 0, 1, 2, 4, 8, 16, 32, 64 };
String arrayAsString = Arrays.toString(famousNumbers); // [0, 1, 2, 4, 8, 16, 32, 64]
System.out.println(arrayAsString);
2
3
- sorting a whole array or a part of it using
Arrays.sort(array)
:
long[] bigNumbers = { 200000000L, 400000000L, 100000000L, 300000000L }; // it's unsorted
Arrays.sort(bigNumbers); // sorting whole array
System.out.println(Arrays.toString(bigNumbers)); // [100000000, 200000000, 300000000, 400000000]
2
3
4
5
- comparing arrays: two arrays are equal if they contain the same elements in the same order:
int[] numbers1 = { 1, 2, 5, 8 };
int[] numbers2 = { 1, 2, 5 };
int[] numbers3 = { 1, 2, 5, 8 };
System.out.println(Arrays.equals(numbers1, numbers2)); // it prints "false"
System.out.println(Arrays.equals(numbers1, numbers3)); // it prints "true"
2
3
4
5
6
- filling a whole array or a part of it by some values:
int size = 10;
char[] characters = new char[size];
// It takes an array, start index, end index (exclusive) and the value for filling the array
Arrays.fill(characters, 0, size / 2, 'A');
Arrays.fill(characters, size / 2, size, 'B');
System.out.println(Arrays.toString(characters)); // it prints [A, A, A, A, A, B, B, B, B, B]
2
3
4
5
6
7
8
Of course, the Arrays
class contains a lot of other useful methods, including array copying, searching in arrays, and so on. For details see here (opens new window).