GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java array - initializing, accessing, traversing arrays in Java Java arraylast modified February 23, 2024 In this article we cover arrays. An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. A scalar variable can hold only one item at a time. Arrays can hold multiple items. These items are called elements of the array. Arrays store data of the same data type. Each element can be referred to by an index. Arrays are zero based. The index of the first element is zero. Array definitionArrays are used to store data of our applications. We declare arrays to be of a certain data type. We specify their length. And we initialize arrays with data. We have several methods for working with arrays. We can modify the elements, sort them, copy them or search for them. int[] ages; String[] names; float[] weights;
We have three array declarations. The declaration consists of two parts: the
type of the array and the array name. The type of an array has a data type
that determines the types of the elements within an array ( Collections serve a similar purpose like arrays. They are more powerful than arrays. They will be described later in a separate chapter. Initializing arraysThere are several ways how we can initialize an array in Java. In the first example, an array is created and initialized in two steps. Main.java
import java.util.Arrays;
void main() {
int[] a = new int[5];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
System.out.println(Arrays.toString(a));
}
We create and initialize a numerical array. The contents of the array are printed to the console. int[] a = new int[5];
Here we create an array which can contain five elements. The statement allocates
memory for five integers. The square brackets are used for declaring an array,
the type ( a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 5; We initialize the array with some data. This is assignment initialization. The indexes are in the square brackets. Number 1 is going to be the first element of the array, number 2 is the second etc. System.out.println(Arrays.toString(a));
The $ java Main.java [1, 2, 3, 4, 5] We can declare and initialize an array in one statement. Main.java
import java.util.Arrays;
void main() {
int[] a = new int[] { 2, 4, 5, 6, 7, 3, 2 };
System.out.println(Arrays.toString(a));
}
This is a modified version of the previous program. int[] array = new int[] { 2, 4, 5, 6, 7, 3, 2 };
An array is created and initialized in one step. The elements are specified in curly brackets. We did not specify the length of the array. The compiler will do it for us. The one step creation and initialization can be further simplified by only specifying the numbers between the curly brackets. Main.java
import java.util.Arrays;
void main() {
int[] a = { 2, 4, 5, 6, 7, 3, 2 };
System.out.println(Arrays.toString(a));
}
An array of integers is created using the most simple way of array creation. int[] a = { 2, 4, 5, 6, 7, 3, 2 };
The Accessing array elementsAfter the array is created, its elements can be accessed by their index. The index is a number placed inside square brackets which follow the array name. Main.java
void main() {
String[] names = {"Jane", "Thomas", "Lucy", "David"};
System.out.println(names[0]);
System.out.println(names[1]);
System.out.println(names[2]);
System.out.println(names[3]);
}
In the example, we create an array of string names. We access each of the elements by its index and print them to the terminal. String[] names = {"Jane", "Thomas", "Lucy", "David"};
An array of strings is created. System.out.println(names[0]); System.out.println(names[1]); System.out.println(names[2]); System.out.println(names[3]);
Each of the elements of the array is printed to the console. With the
$ java Main.java Jane Thomas Lucy David Running the example we get the above output. It is possible to change the elements of an array. The elements are not immutable. Main.java
import java.util.Arrays;
void main() {
int[] vals = { 1, 2, 3 };
vals[0] *= 2;
vals[1] *= 2;
vals[2] *= 2;
System.out.println(Arrays.toString(vals));
}
We have an array of three integers. Each of the values will be multiplied by two. int[] vals = { 1, 2, 3 };
An array of three integers is created. vals[0] *= 2; vals[1] *= 2; vals[2] *= 2; Using the element access, we multiply each value in the array by two. $ java Main.java [2, 4, 6] Traversing arraysWe often need to go through all elements of an array. We show two common methods for traversing an array. Main.java
void main() {
String[] planets = { "Mercury", "Venus", "Mars", "Earth", "Jupiter",
"Saturn", "Uranus", "Neptune", "Pluto" };
for (int i = 0; i < planets.length; i++) {
System.out.println(planets[i]);
}
for (String planet : planets) {
System.out.println(planet);
}
}
An array of planet names is created. We use the for loop to print all the values. for (int i=0; i < planets.length; i++) {
System.out.println(planets[i]);
}
In this loop, we utilize the fact that we can get the number of elements from
the array object. The number of elements is stored in the for (String planet : planets) {
System.out.println(planet);
}
An enhanced for keyword can be used to make the code more compact when traversing arrays or other collections. In each cycle, the planet variable is passed the next value from the planets array. Passing arrays to methodsIn the next example, we pass an array to a method. Main.java
import java.util.Arrays;
void main() {
int[] a = { 3, 4, 5, 6, 7 };
int[] r = reverseArray(a);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(r));
}
int[] reverseArray(int[] b) {
int[] c = new int[b.length];
for (int i = b.length - 1, j = 0; i >= 0; i--, j++) {
c[j] = b[i];
}
return c;
}
The example reorders the elements of an array. For this task, a
int[] reverseArray(int[] b) {
The int[] c = new int[b.length]; Inside the body of the method, a new array is created; it will contain the newly ordered elements. for (int i = b.length - 1, j = 0; i >= 0; i--, j++) {
c[j] = b[i];
}
In this for loop, we fill the new array with the elements of the copied array. The elements are reversed. return c; The newly formed array is returned back to the caller. System.out.println(Arrays.toString(a)); System.out.println(Arrays.toString(r)); We print the elements of the original and the reversed array. $ java Main.java [3, 4, 5, 6, 7] [7, 6, 5, 4, 3] Multidimensional arraysSo far we have been working with one-dimensional arrays. In Java, we can create multidimensional arrays. A multidimensional array is an array of arrays. In such an array, the elements are themselves arrays. In multidimensional arrays, we use two or more sets of brackets. Main.java
void main() {
int[][] twodim = new int[][] { { 1, 2, 3 }, { 1, 2, 3 } };
int d1 = twodim.length;
int d2 = twodim[1].length;
for (int i = 0; i < d1; i++) {
for (int j = 0; j < d2; j++) {
System.out.println(twodim[i][j]);
}
}
}
In this example, we create a two-dimensional array of integers. int[][] twodim = new int[][] { { 1, 2, 3 }, { 1, 2, 3 } };
Two pairs of square brackets are used to declare a two-dimensional array. Inside the curly brackets, we have additional two pairs of curly brackets. They represent two inner arrays. int d1 = twodim.length; int d2 = twodim[1].length; We determine the length of the outer array that holds other two arrays and the second inner array. for (int i = 0; i < d1; i++) {
for (int j = 0; j < d2; j++) {
System.out.println(twodim[i][j]);
}
}
Two for loops are used to print all the six values from the two-dimensional
array. The first index of the $ java Main.java 1 2 3 1 2 3 In a similar fashion, we create a three-dimensional array of integers. Main.java
void main() {
int[][][] n3 = {
{ { 12, 2, 8 }, { 0, 2, 1 } },
{ { 14, 5, 2 }, { 0, 5, 4 } },
{ { 3, 26, 9 }, { 8, 7, 1 } },
{ { 4, 11, 2 }, { 0, 9, 6 } }
};
int d1 = n3.length;
int d2 = n3[0].length;
int d3 = n3[0][0].length;
for (int i = 0; i < d1; i++) {
for (int j = 0; j < d2; j++) {
for (int k = 0; k < d3; k++) {
System.out.print(n3[i][j][k] + " ");
}
}
}
}
A variable that holds a three-dimensional array is declared with three pairs of square brackets. The values are place inside three pairs of curly brackets. int[][][] n3 = {
{ { 12, 2, 8 }, { 0, 2, 1 } },
{ { 14, 5, 2 }, { 0, 5, 4 } },
{ { 3, 26, 9 }, { 8, 7, 1 } },
{ { 4, 11, 2 }, { 0, 9, 6 } }
};
Three-dimensional array int d1 = n3.length; int d2 = n3[0].length; int d3 = n3[0][0].length; We get the length of all three dimensions. for (int i = 0; i < d1; i++) {
for (int j = 0; j < d2; j++) {
for (int k = 0; k < d3; k++) {
System.out.print(n3[i][j][k] + " ");
}
}
}
We need three for loops to traverse a three dimensional array. $ java Main.java 12 2 8 0 2 1 14 5 2 0 5 4 3 26 9 8 7 1 4 11 2 0 9 6 Irregular arraysArrays that have elements of the same size are called rectangular arrays. It is possible to create irregular arrays where the arrays have a different size. In C# such arrays are called jagged arrays. Main.java
void main() {
int[][] ir = new int[][] {
{ 1, 2 },
{ 1, 2, 3 },
{ 1, 2, 3, 4 }
};
for (int[] a : ir) {
for (int e : a) {
System.out.print(e + " ");
}
}
}
This is an example of an irregular array. int[][] ir = new int[][] {
{ 1, 2 },
{ 1, 2, 3 },
{ 1, 2, 3, 4 }
};
This is a declaration and initialization of an irregular array. The three inner arrays have 2, 3, and 4 elements. for (int[] a : ir) {
for (int e : a) {
System.out.print(e + " ");
}
}
The enhanced for loop is used to go through all the elements of the array. $ java Main.java 1 2 1 2 3 1 2 3 4 Array methods
The Main.java
import java.util.Arrays;
void main() {
int[] a = { 5, 2, 4, 3, 1 };
Arrays.sort(a);
System.out.println(Arrays.toString(a));
Arrays.fill(a, 8);
System.out.println(Arrays.toString(a));
int[] b = Arrays.copyOf(a, 5);
if (Arrays.equals(a, b)) {
System.out.println("Arrays a, b are equal");
} else {
System.out.println("Arrays a, b are not equal");
}
}
In the code example, we present five methods of the import java.util.Arrays;
We will use the shorthand notation for the int[] a = { 5, 2, 4, 3, 1 };
We have an array of five integers. Arrays.sort(a);
The System.out.println(Arrays.toString(a));
The Arrays.fill(a, 8);
The int[] b = Arrays.copyOf(a, 5);
The if (Arrays.equals(a, b)) {
System.out.println("Arrays a, b are equal");
} else {
System.out.println("Arrays a, b are not equal");
}
The $ java Main.java [1, 2, 3, 4, 5] [8, 8, 8, 8, 8] Arrays a, b are equal Comparing arrays
There are two methods for comparing arrays. The Main.java
import java.util.Arrays;
void main() {
int[] a = { 1, 1, 2, 1, 1 };
int[] b = { 0, 0, 3, 0, 0 };
int[][] c = {
{ 1, 1, 2, 1, 1 },
{ 0, 0, 3, 0, 0 }
};
int[][] d = {
a,
b
};
System.out.print("equals() method: ");
if (Arrays.equals(c, d)) {
System.out.println("Arrays c, d are equal");
} else {
System.out.println("Arrays c, d are not equal");
}
System.out.print("deepEquals() method: ");
if (Arrays.deepEquals(c, d)) {
System.out.println("Arrays c, d are equal");
} else {
System.out.println("Arrays c, d are not equal");
}
}
The example explains the difference between the two methods. int[] a = { 1, 1, 2, 1, 1 };
int[] b = { 0, 0, 3, 0, 0 };
We have two arrays of integers. int[][] c = {
{ 1, 1, 2, 1, 1 },
{ 0, 0, 3, 0, 0 }
};
The c array has two inner arrays. The elements of the inner arrays are equal
to the int[][] d = {
a,
b
};
The System.out.print("equals() method: ");
if (Arrays.equals(c, d)) {
System.out.println("Arrays c, d are equal");
} else {
System.out.println("Arrays c, d are not equal");
}
System.out.print("deepEquals() method: ");
if (Arrays.deepEquals(c, d)) {
System.out.println("Arrays c, d are equal");
} else {
System.out.println("Arrays c, d are not equal");
}
Now the $ java Main.java equals() method: Arrays c, d are not equal deepEquals() method: Arrays c, d are equal Searching arrays
The Main.java
import java.util.Arrays;
void main() {
String[] planets = { "Mercury", "Venus", "Mars", "Earth", "Jupiter",
"Saturn", "Uranus", "Neptune", "Pluto" };
Arrays.sort(planets);
String p = "Earth";
int r = Arrays.binarySearch(planets, p);
String msg = "";
if (r >= 0) {
msg = STR."\{p} was found at position \{r} of the sorted array";
} else {
msg = STR."\{p} was not found";
}
System.out.println(msg);
}
In the example, we search for the "Earth" string in an array of planets. Arrays.sort(planets); Since the algorithm only works on sorted arrays, we must sort the array first. String p = "Earth"; We will be searching for the "Earth" element. int r = Arrays.binarySearch(planets, p);
The if (r >= 0) {
msg = STR."\{p} was found at position \{r} of the sorted array";
} else {
msg = STR."\{p} was not found";
}
Depending on the returned value, we create a message. $ java Main.java Earth was found at position 0 of the sorted array Download imageIn the next example, we show how to download an image. Main.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
void main() throws IOException, URISyntaxException {
String imageUrl = "https://something.com/favicon.ico";
String destinationFile = "favicon.ico";
var url = new URI(imageUrl).toURL();
try (var is = url.openStream();
var fos = new FileOutputStream(destinationFile)) {
byte[] buf = new byte[1024];
int noOfBytes;
while ((noOfBytes = is.read(buf)) != -1) {
fos.write(buf, 0, noOfBytes);
}
}
}
The example downloads a small byte[] buf = new byte[1024];
An image is an array of bytes. We create an empty array of while ((noOfBytes = is.read(buf)) != -1) {
fos.write(buf, 0, noOfBytes);
}
We read the binary data and write it to the file. SourceIn this article we worked with arrays. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |