Learning Java. I will make a note so that I can look back on it for review. It's almost my study memo. Please do not excessive expectations.
An array is like a set of variables.
Arrays can store multiple values , whereas variables can only handle one value.
Each value in the array is called a element .
--Array variable definition
When dealing with arrays, assign the array to an array type variable.
The array type must be specified as "array type with int type element" and "array type with String type element".
Arrays with int type elements are int [] ,
An array with String type elements is described as String [] .
--How to make an array
An array can be created by writing values in {}, separated by commas (,). Be careful not to confuse [] with {}. Note that a semicolon (;) is also required at the end of the array
How to make an array
//Array with int type elements
int[] numbers = {"3", "6", "9"};
//Array with elements of type String
String[] names = {"Aki", "John", "Bob"};
--Index number
The elements of the array are assigned numbers such as "0, 1, 2 ..." in order from the front.
Note that this is called the index number and the index number starts at 0.
--Getting elements
Each element of the array can be obtained by using the array name [index number].
How to make an array
String[] names = {"Aki", "John", "Bob"};
//Output element with index number 2
System.out.println(names[2]);
Output result
Bob
--Overwrite array elements
It is possible to overwrite an element by assigning a value to a specific element.
How to make an array
String[] names = {"Aki", "John", "Bob"};
System.out.println(names[1]); //①
//Overwrite array elements
names[1] = "Taro";
System.out.println(names[1]); //②
Output result
① John
② Taro
--Array and for statement
In order to output all the elements of the array, you can easily list them by using the for statement.
How to make an array
String[] names = {"Aki", "John", "Bob"};
for(int i=0; i<0; i+=1){
System.out.println("my name is," + names[i] + "is."); //i is "0",1,Repeated only during "2"
}
Output result
My name is Aki.
My name is John.
My name is Bob.
Arrays have a feature called length that counts the number of elements.
length is used by connecting with dots (.) Like " array.length "
.
By using length, the conditional expression "i <3" in the for statement can be rewritten.
You don't have to worry about the number of elements in the array.
How to make an array
String[] names = {"Aki", "John", "Bob"};
for(int i=0; i<names.length; i+=1){
System.out.println("my name is," + names[i] + "is.");
}
--For statement for array
The for statement has a special syntax (extended for statement) for arrays. If you use this, you can write the for statement more simply.
How to write
for(Data type variable name:Array name){
//Iterative processing content;
}
How to make an array
String[] names = {"Aki", "John", "Bob"};
for(String name: names){
System.out.println("my name is," + name + "is.");
}
Output result
My name is Aki.
My name is John.
My name is Bob.
[Java ~ Variable definition, type conversion ~] Study memo [Java ~ False Value ~] Study Memo 2 [Java ~ Conditional branching / Iterative processing ~] Study memo 3
Recommended Posts