Java gives a data structure, the array, which stores a constant-size sequential set of elements of
the same type. An array is used to collect a set of data, but it is often more useful to think of an
array as a set of variables of the same type.
Instead of said seprate variables, such as number0, number1, …, and number99, you declare one
array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to
represent seprate variables.
This tutorial discribe how to declare array variables, produce arrays, and process arrays using
indexed variables.
Said Array Variables:
To use an array in a program, you must declare a variable to footnote the array, and you must
specify the type of array the variable can footnote. Here is the syntax for said an array variable:
dataType[] arrayVar; // preferred way.
or
dataType arrayVar[]; // works but not preferred way
Note: The style dataType[] arrayVar is preferred. The style dataType arrayVar[]
comes from the C/C++ language and was take in Java to synchronize C/C++ programmers.
The following code snippets are examples of this syntax:
double[] myList;
or
double myList[];
Creating Arrays:
// preferred way(method) .
// works but not preferred way(method) .
You can produce an array by using the new operator with the following syntax
arrayVar = new dataType[arraySize];
The above statement does two things:
It produces an array using new dataType[arraySize];
It reattach the footnote of the newly produced array to the variable arrayVar.
Said an array variable, genrating an array, and assigning the footnote of the array to the
variable can be combined in one statement, as shown below:
dataType[] arrayVar = new dataType[arraySize];
otherwise you can produce arrays as follows:
dataType[] arrayVar = {ram, krishna, …, vishnu};
The array elements are satisfied through the index. Array index are 0-based; that is, they start
from 0 to arrayVar.length-1.
Example:
Following statement declares an array variable, myList, produces an array of 10 elements of
double type and reattach its footnote to myList.
double[] myList = new double[10];
Following picture represents array myList. Here, myList holds ten double values and the index are
from 0 to 9.
Filtering Arrays:
When filtering array elements, we often use either for loop or for each loop because all of the
elements in an array are of the same type and the size of the array is known.
Example:
Here is a full example of showing how to produce, institute and process arrays:
public class programingcoding
{
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
for (int i = 0; i < myList.length;
i++) {
System.out.println(myList[i] + ” “);
}
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println(“Total is ” + total);
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println(“Max is ” + max);
}
}
output
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5
public class programingcoding {
public static void main(String[] args) {
double[] myList = {25.1,98.2,58.1,6};
for (double element: myList) {
System.out.println(element);
}}}