This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
** Array (ʻarray`) ** has the function of storing values of the same type together. To prepare the array, you need to follow the steps below.
Array preparation example
int[] arrayOfScore;
arrayOfScore = new int[40];
//Declaration of an array element that can store 40 int type values
//Or
int[] arrayOfScore = new int[40];
You can use the new
operator to prepare an array container in memory. This new
is handled again when it is a class
or instance
.
In the example, 40 is specified, but the number in [] is a subscript (ʻindex) and is the serial number of the array prepared in the array variable. This ʻindex
,
** Array ʻindex in Java starts at 0 ** Therefore, the maximum number is "number of elements-1". In the case of the example, ** from ** ʻarrayOfScore [0]
to ʻarrayOfScore [39] is prepared, so even if you make a mistake, do not call it assuming that ʻarrayOfScore [40]
exists.
Also, the array can be initialized with one line.
Array initialization
int[] arrayOfScore = {50, 62, 71, 94, 16, 8,};
//Now arrayOfScore[0] = 50;(Omitted below) is stored.
//The above equation is equal to the following equation.
int[] arrayOfScore = new arrayOfScore[6];
arrayOfScore[0] = 50;
arrayOfScore[1] = 62;
arrayOfScore[2] = 71;
arrayOfScore[3] = 94;
arrayOfScore[4] = 16;
arrayOfScore[5] = 8;
Regarding the question, "Because it is a variable, it can be assigned as a variable." Certainly it can be substituted, but there is one caveat. If you prepare another variable and assign an array variable to it, you will be "** referencing the same array from two variables **", so if you change the contents of one, the contents of the other will also be Looks strange. Keep in mind that we don't have another variable with the same value, we just ** refer to one source array **.
Java can create ** multidimensional arrays ** as arrays that span more than two dimensions, making the elements of the array into arrays. It's easy to understand if you imagine an Excel table.
Preparation of 2D array
int[][] eightClassesScoreArray;
= new eightClassesScoreArray[40][8]
eightClassesScoreArray[0][0] = 65;
//(Omitted below)
//You may do this
int[][] eightClassesScoreArray = {/*(abridgement)*/},{0,1,2,3,4 ,5 ,6 ,7}
Since the number of each element does not need to be the same, the above 40: 8 array is also possible, and even if the number of dimensions increases to 3D and 4D and the number of elements is different, it can exist as a multidimensional array.
arrayOmikuzi
//(abridgement)
int arrayOmikuziPattern[][] = new int[][5];
//For the time being, 5 stages have been decided from Daikichi.
//I want to see only the movement, so it's okay to leave the elements empty.
for(int checker = 0;checker <5;checker++)
{
System.out.println("Your fortune" + "【Unimplemented】" + "Came out. );
System.out.println("The result on the Omikuji side is" + (arrayOmikuziPattern[][checker]+1) + "It's your turn.");
}
Compile error when declaring a two-dimensional array. ** It seems that the number of elements in the first dimension should not be omitted when declaring a multidimensional array **.
I often hear that arrays are used for 3D data. I would like to take the time to understand the sequence again, as it may be directly linked to my future work.
I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.
Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)
Recommended Posts