Java Array Definition (One-dimensional and Multidimensional Arrays)
One-Dimensional Arrays
As in all programming languages, the array structure in Java is an important data structure in the last place. Briefly describing the structure of the array:
An array is a data structure that stores a large number of data, usually of the same type, in the computer memory under the same name. The following is a logical view of an index named x:
4 | 2 | 7 | 22 | 11 | 3 |
0 | 1 | 2 | 3 | 4 | 5 |
The index has a name and an index that indicates how many elements in the array are indexed. For example, for the array x above, the expression x represents the element number 3 in this directory, which is 22. The index of the index is always specified in square brackets in the Java language.
- The index of the first element of an array in the Java language is always 0; and the index of the last element is equal to 1 minus the number of places left in the sequence.
- The type, name, and maximum number of elements of the index are specified in a declaration statement:
2 |
Type ArrayName[ ] = new Type [ number of elements ]; |
or equivalent
2 |
Type [ ] ArrayName = new Type [ number of elements ]; |
For example, for the above x array,
2 |
int [ ] x= new int [6]; |
The x array is 6 elements and the elements are of type int.
What is required for the array?
We need it in practice when it is necessary to keep a data set of the same type entirely in memory. For example, applications such as sorting of data, calculation of some statistical information of a data set (standard deviation, etc.) are such applications.
Example: Here are two different Java programs that do the same thing. Both programs collect the total of 5 numbers entered on the computer.
Demo
- NOTE: The Scanner class is used to assign a variable from a stream. The System.in stream is used to input user’s console data.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner inpt = new Scanner(System.in); int number[] = new int[6]; int count, i; count= 0; for (i = 1; i <= 5; i++) { System.out.print("enter a number: "); number[i] = inpt.nextInt(); count= count + number[i]; } System.out.println("sum of numbers entered: " + count); System.out.println("3rd element of number array: " + number[3]); } } |
Array Initial Value Assignment
As with many computer languages, it is possible to assign an initial value in the Java language when defining the sequence index. For example;
2 |
int x ={5,-1,2,4,11}; |
The following values are assigned to the x sequence: Example: Initial Value Assignment
2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { int [] number={5,2,4,3,6}; double count = 0; int i; for(i=0; i<=4; i++) { count=count+number[i]; } System.out.println("Count: "+count); } } |
Here we will have the following output:
Another method in array definition
After defining the array size as a variable, it is also possible to use this variable when defining the array. It can define a 5-element array with the following commands, but we can not assign the first value to the array:
2 3 4 |
int ARRAY_LENGTH=5; int [] number; number=new int[ARRAY_LENGTH]; |
Multidimensional Arrays
The most common use for multi-dimensional arrays is for two-dimensional arrays.
Let us present the following data:
brand | january | february | march |
---|---|---|---|
Fiat | 700 | 600 | 650 |
Renault | 900 | 800 | 700 |
VW | 300 | 400 | 350 |
Opel | 500 | 450 | 470 |
Ford | 600 | 500 | 480 |
In the Java language, we can store the tabular information in a structure called a two dimensional array structure in computer memory. This natural type of mathematical matrix structures in Java is also a two dimensional array structure.
In Java, we can define this table, which consists of 5 rows and 3 columns, as follows:
2 |
int sales = new int [5,3]; |
Here we use an expression in the program to refer to an element of a two-dimensional array called sales. Here, the name of the sales directory, i is the row index, and j is the index of the column. The row index of the satis sequence starts at 0 and continues until 4. Column indices are between 0 and 2. Here’s how the sales line is stored in memory in Java:
Accordingly, the sales [0,0] element is 700, the sales [2,1] element is 400, and the sales [4,2] element is 480.
The number of elements in a two-dimensional array called sat = 5×3 = 15.
To read two dimensional array elements
We can use a program such as the following to input elements of the two-dimensional array from the keyboard during execution of the Java program. In the following example program, 3 rows and 2 columns of sales are entered from the keyboard and then the table is transferred to the screen. Now let’s examine this sample program:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner enter = new Scanner(System.in); int[][] salesTable = new int[3][2]; int i, j; /* user sales table * get the info*/ System.out.print("sales [0,0]="); salesTable[0][0] = enter.nextInt(); System.out.print("sales [0,1]="); salesTable[0][1] = enter.nextInt(); System.out.print("sales [1,0]="); salesTable[1][0] = enter.nextInt(); System.out.print("sales [1,1]="); salesTable[1][1] = enter.nextInt(); System.out.print("sales [2,0]="); salesTable[2][0] = enter.nextInt(); System.out.print("sales [2,1]="); salesTable[2][1] = enter.nextInt(); /* sales chart * elements on the screen */ System.out.println("sales table"); for (i = 0; i <= 2; i++) { for (j = 0; j <= 1; j++) { System.out.print(salesTable[i][j] + " "); } System.out.println(); } } } |
Using the length function
With the length function, it is possible to find the number of elements in each dimension of the two-dimensional array.
For Example:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class Main { public static void main(String[] args) { int [][]salesTable = new int[][]{ {700,600,650}, {900,800,700}, {300,400,350}, {500,450,470}, {600,500,480} }; int i,j; /*sales chart * printing of elements on screen*/ System.out.println("Satis tablosu"); /*the bounds of the cycles () length () we determine with functions*/ for(i=0; i<salesTable.length; i++) { for(j=0; j<salesTable[0].length; j++) { System.out.print(salesTable[i][j]+" " ); } System.out.println(); } } } |
Matrix Multiplication
The following Java program,
Cmn = Amk Bkn
matrix multiplication
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public class Main { public static void main(String[] args) { int[][] aMatrix=new int [][]{{2,1},{-1,4},{5,3}}; int[][] bMatrix=new int [][]{{3,2,1,-1},{4,-2,1,2}}; int[][] cMatrix=new int [3][4]; int i,j,k,t; /*a and b matrix write*/ System.out.println("A matrix"); for(i=0; i<=2; i++) { for(j=0; j<=1; j++) { System.out.print(aMatrix[i][j]+"\t"); } System.out.println(); } System.out.println("B matrix"); for(i=0; i<=1; i++){ for(j=0;j<=3;j++) { System.out.print(bMatrix[i][j]+"\t"); } System.out.println(); } /* Calculation of matrix * c, which is the product of a and its matrix */ for(k=0; k<=3; k++) { for(i=0; i<=2; i++) { cMatrix[i][k]=0; for(j=0; j<=1; j++) { cMatrix[i][k]=cMatrix[i][k]+ aMatrix[i][j]*bMatrix[j][k]; } } } System.out.println("C matrix"); for(i=0; i<=2; i++) { for(j=0;j<=3;j++) System.out.print(cMatrix[i][j]+"\t"); System.out.println(); } } } |
Output:
2 3 4 5 6 7 8 9 10 11 12 |
A matrix 2 1 -1 4 5 3 B matrix 3 2 1 -1 4 -2 1 2 C matrix 10 2 3 0 13 -10 3 9 27 4 8 1 |
EXAMPLE: Develop a Java program that automatically generates and prints the following matrix.
( 2! – 1 ) / 3 | ( 4! + 2 ) / 5 | ( 6! – 3 ) / 7 |
( 8! + 4 ) / 9 | ( 10! – 5 ) / 11 | ( 12! + 6 ) / 13 |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public class Main { public static void main(String[] args) { double matrix[][]=new double[2][3]; int i,j,p,u; int k; char mark; p=-1; k=2; u=1; for(i=0; i<=1; i++) { for(j=0; j<=2; j++) { matrix[i][j]=(factorial(k)+p*u)/(k+1.0); if(p<0) { mark='-'; } else { mark='+'; } System.out.println("[" +i+ "][" +j+ "] = (" +k+ "!" +mark+u+ ") / " +(k+1)+ " = " +matrix[i][j]+ "\t"); k=k+2; p=-p; u++; } } } public static double factorial(double number){ double fakt=1.0; int i; for(i=1; i<=number; i++) { fakt=fakt*i; } return fakt; } } |
Output:
if you have question do not forget to write from the chat button next to it or from the comment
Good response in return of this query with firm arguments and
describing the whole thing about that.