--Array type --Array --Multidimensional array --Array for syntax --Extended for syntax --Sample program for averaging int array elements
I want to use it because the abbreviation is easy, but considering garbage, I also want to remember the formal way of writing using new
. Even though there is a ** garbage collection **, if there is a new
where it is omitted, be aware of it and write the abbreviation.
//Formal writing
int[] scores; //Declaration of array variables
scores = new int[3]; //Declaration of array elements
scores[0]=10; //Array element assignment
scores[1]=20;
scores[2]=30;
//Abbreviation
int[] scores = new int[]{10,20,30};
int[] scores = {10,20,30};
//All equivalent
//Formal writing
String[][] scores = new String[2][3];
scores[0][0]="A1";
scores[0][1]="A2";
scores[0][2]="A3";
scores[1][0]="B1";
scores[1][1]="B2";
scores[1][2]="B3";
//Multidimensional array abbreviation
String[][] scores = {{"A1","A2","A3"},{"B1","B2","B3"}};
//All equivalent
Array name.length
: Now get the number of array elements and use
//Creating an array
int[] scores = {10,20,30};
//General for syntax
for (int i=0; i<scores.length; i++){
System.out.println(scores[i]);
}
//Extended for syntax
for (int value:scores){
System.out.println(values);
}
//Creating an array
int scores[][]={{10,11,12},{21,22,23}};
//General for syntax
for(int i=0; i<scores.length; i++){
for(int ii=0; ii<scores[i].length; ii++){
System.out.println(scores[i][ii]);
}
}
[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/dp/B00MIM1KFC/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1) Pp.138-166
Recommended Posts