Course Content
Java Basics
Java Basics
What is Array?
Arrays
Storing information exclusively in variables isn't always convenient, as it would consume a significant amount of stack memory and require a substantial number of code lines.
The solution to this problem is arrays.
Let's explore the basic syntax for declaring an array in Java:
Main
dataType[] arrayName = new dataType[capacity];
We can use any data type when declaring an array, including int
, float
, char
, and more. You can even use your custom classes in array declarations. Learning how to create your own classes and their objects will be covered in a separate Java Extended course.
It is mandatory to specify the size of the array in square brackets ([]
). Let's consider an example of creating an array of type int
with a size of 5:
Main
package com.example; public class Main { public static void main(String[] args) { int[] intArray = new int[5]; // declaring a new int array with length 5 System.out.println(intArray.length); // printing the size of our array } }
As you may have noticed, we used something unfamiliar in conjunction with the console output. This thing is called a array's property.
Arrays have their own properties.
Properties are called using dot (.
) notation on the object with them. In our example, the intArray
array has properties, and we are using one of them.
The .length
propertie returns the size (length) of the array as an int
value. This can also be written differently:
Main
package com.example; public class Main { public static void main(String[] args) { int[] intArray = new int[5]; // declaring a new int array with length 5 int length = intArray.length; // declaring a new int with the value of the array`s size System.out.println(length); // printing the size of our array } }
Now, let's examine three fundamental methods and properties for working with arrays:
array.length;
- Returns the size of the array;Arrays.sort(array);
- Sorts the elements of the array in ascending order;Arrays.fill(array, value);
- Sets all elements of the array to the specified value.
Note
array
is simply an example name for the array. You can name the array as you like, depending on the task requirements or your preferences.
How to Access Specific Data in an Array?
Arrays in Java are indexed. Each array element is situated in its cell and assigned a unique ID. The numbering of elements within the array begins at 0. Suppose we have an array of numbers from zero to nine, so it has 10 elements in it.
Let's examine a diagram that illustrates how the elements are organized within the array:
Note
It's worth highlighting the formula N-1, where N represents the element's number in the array, and N-1 denotes its position in the array. This method allows us to calculate the element's position in the array effortlessly. This concept is known as zero-based indexing.
Practice is always better than just theory, so let's explore an example of adding and removing elements from an array using indices:
Main
package com.example; public class Main { public static void main(String[] args) { int[] intArray = new int[5]; // declaring a new int array with length 5 //filling up our array with numbers from 1 to 5 intArray[0] = 1; intArray[1] = 2; intArray[2] = 3; intArray[3] = 4; intArray[4] = 5; System.out.println(intArray[3]); // printing the fourth element of our array } }
We have populated our array with elements from one to five by specifying each element with an index. However, this approach lacks elegance and requires many lines of code. Let's explore an alternative method of declaring an array that comes pre-filled:
Main
package com.example; public class Main { public static void main(String[] args) { int[] intArray = {1, 2, 3, 4, 5}; // declaring a new pre-filled int array with length 5 System.out.println(intArray[0]); // printing the first element of our array } }
We created an array identical to the one we made earlier, but this time, it required fewer lines of code and appeared more elegant.
It's important to note that when using this array notation, the elements must be enclosed within curly braces ({}
) and separated by commas.
Note
Notice that when declaring an array in this manner, we don't specify its size explicitly. The compiler automatically determines the required length of the array based on the provided elements and populates it accordingly.
Everything was clear?