Three steps are required to create an array.
As an image
Such a place.
To declare an array variable, write as follows.
Array element data type[]Variable name of array;
The data type of an array element is the data type of the element to be assigned in the array. An integer is int [], a decimal is double [], and a string is String [].
The variable name of the array is basically the English plural that represents the contents of the array. Scores if the contents of the array are the numbers of the test scores, colors if the string of the color name, etc.
Example: Define an array of integers numbers
//Since it is an integer, the data type is int, after the data type[]Define with
int[] numbers;
To assign an element, write as follows.
Variable name of array=Data type of new element[Number of elements];
Create an array element for the variable declared earlier.
The data type of the element is the same as the data type when the variable is declared. The number of elements is the number of values you want to put in the array.
Example: Assign three elements to the array numbers defined earlier
//Since numbers is an array of integers, the data type is int
//Since there are three elements, the number of elements is[3]
numbers = new int[3];
To assign a value to an array, write as follows.
Variable name of array[index number] =Value you want to assign;
Example: Substitute the integers 10, 20, 30 for numbers
//Note that the index number starts from 0
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
After creating the elements of the array, if the value is not entered, the value is automatically assigned according to the data type when compiling. (0 for int, null for String, etc.)
If you create an array by following the above 3 steps, it will be as follows.
Example: Create an array of integers, assign numbers 10, 20, 30
//Declare a variable
int[] numbers;
//Create element
numbers = new int[3];
//Assign value
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
However, these can also be described together as follows.
Array element data type[]Variable name of array= {Value 1,Value 2,Value 3, ...};
//Example: Make an array of integers numbers and integer 10, 20,Substitute 30
int[] numbers = {10, 20, 30};
Recommended Posts