One-dimensional arrays
1. Declaring an Array in Java
One-dimensional arrays in Java are lists of variables of the same type. To create an array, you first declare a variable of the required type.
General syntax for declaring a one-dimensional array looks like this:
type[] variableName;
The type
parameter defines the element type of the array, also known as the base type.
You can place the square brackets either before or after the variable name. However, it's more conventional to place them after the type, making it immediately clear that it's an array:
int monthDays[];
double[] monthSalaries;
2. Initializing an Array with the new Keyword
Declaring an array doesn't allocate memory. To allocate memory, use the new
keyword, followed by the array type and its size in square brackets:
variableName = new type[size];
You can also declare and initialize an array in one line:
int[] values = new int[45];
Here's an example of declaring an int
array of size 12. The line int[] monthDays = new int[12];
creates an array of 12 elements, each initialized to zero (the default value for int
). You can access individual elements by index using square brackets:
public class ArrayExample1 {
public static void main(String[] args) {
int[] monthDays = new int[12];
monthDays[0] = 31;
monthDays[1] = 28;
monthDays[2] = 31;
monthDays[3] = 30;
monthDays[4] = 31;
monthDays[5] = 30;
monthDays[6] = 31;
monthDays[7] = 31;
monthDays[8] = 30;
monthDays[9] = 31;
monthDays[10] = 30;
monthDays[11] = 31;
System.out.println("April has " + monthDays[3] + " days.");
}
}
3. Array Initialization with an Initialization Block
If you already know the values for each element, use an array initialization block. Instead of new int[12]
, use curly braces with values separated by commas. The compiler infers the array size from the number of elements:
public class ArrayExample2 {
public static void main(String[] args) {
int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("April has " + monthDays[3] + " days.");
}
}
4. Anonymous Arrays in Java
Java also supports anonymous arrays, which can be used in two scenarios.
First, suppose you initialized an array like this:
int[] testScores = {1, 2, 3, 4};
...
testScores = {4, 7, 2}; // Compilation error
You can’t reassign values using the same initializer syntax. Instead, use an anonymous array to assign new values:
testScores = new int[]{4, 7, 2};
Second, anonymous arrays can be used as method arguments. In the following example, the print
method accepts an array as a parameter. An anonymous array is passed directly to the method:
public class ArrayExample3 {
public static void main(String[] args) {
int[] testScores = {1, 2, 3, 4};
for (int element : testScores) {
System.out.print(element + " ");
}
System.out.println();
testScores = new int[]{4, 7, 2};
for (int element : testScores) {
System.out.print(element + " ");
}
System.out.println();
print(new int[]{4, 6, 2, 3});
}
public static void print(int[] array) {
for (int element : array) {
System.out.print(element + " ");
}
}
}

Please log in or register to have a possibility to add comment.