Java Tutorials
Java Methods
Java Classes
Java File Handling
Arrays are used to store multiple values for a single variable, instead of declaring different values for each value.
To declare the same members, define a variable type with square brackets:
String[] cars;
We have now announced the variables that hold the string of strings. To add numbers to it, we can use the same physical components - put values in a comma-separated list, within folded parentheses:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
To create whole numbers, you can type:
int[] myNum = {10, 20, 30, 40};
You are accessing an element of the same members by referring to the reference number.
This statement covers the value of the first element in cars:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
Note: Index lists start with the 0: [0] first item. [1] second element, etc.
To change the value of a specific element, see the reference number:
cars[0] = "Opel";
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
To find out how many features of the same member, use the length
feature:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4
You can enter between elements of the same loop with a for
loop, and use the length
feature to specify how many loops should work.
The following example removes all elements from the array of cars:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
There is also a "for-one" loop, which is specially used to combine elements into identical members:
for (type variable : arrayname) {
...
}
The following example removes all features from the array of cars, using the "for-each" loop:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
System.out.println(i);
}
The example above can be read as follows: for each String
element (pronounced as i in index) on cars, print the value of i.
If you compare the loop with each loop, you will see that each method is easy to write, does not require a counter (using a length structure), and is very readable.
A multidimensional array is a group of identical members.
To create identical two-dimensional members, add the same individual members within their set of curly brackets:
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers is now a program with two identical members as their components.
To access myNumbers array features, specify two indicators: one for the program, and one for the component within that list. This example reaches the third (2) element in the second (1) program of my numbers:
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
We can also use a for loop
inside other for loop
to get a loop to get the features of a two-dimensional system (we still have to point to two directions):
public class Main {
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}
}
}