When I relearned Java from the basics, I felt that there were many points that I had not understood so far, so I have summarized the learning contents so that I can look back on them later.
Those that handle values of the same data type together. It is used as follows.
Array
//Variable declaration
int[] nums;
//Instantiation
nums = new int[10];
//Substitution
nums[0] = 10;
It is also possible to initialize multiple devices at the same time.
Array(Simultaneous initialization)
//Variable declaration
int[] nums = {10, 20, 30};
The number of elements in the array can be obtained as follows.
Array(size)
int length = nums.length;
java also supports multidimensional arrays. The initialization is as follows.
Multidimensional array
//Variable declaration
int[][] nums;
//Secure area
nums = new int[2][3];
//Substitution
nums[0][1] = 10;
//length
// nums.length == 2; -> true
// nums.[0].length == 3; -> true
//Multidimensional arrays of different sizes are also possible
int nums2;
nums2 = {
{1,2},
{1,2,3},
{1}
};
ArrayList Something like a resizable array.
ArrayList
import java.util.ArrayList;
/*Omission*/
//Variable declaration
ArrayList<String> strList;
//Instantiation(In the following cases, the number of elements in the initial state is 3. You do not have to specify the size)
strList = new ArrayList<String>(3);
//add to
strList.add("ABC");
//Get
strList.get(0);
//Length Since the number of stored elements is one, 1 is returned.
strList.size();
//Initialization using generics
ArrayList strList2 = new ArrayList();
//Compiles, but an error occurs when the types are different
strList2.add("ABC");
strList2.add(1);//← Error
//When using the diamond operator
ArrayList<String> strList3 = new ArrayList<>();
//When using List
List<String> strList4 = new ArrayList<>();
Recommended Posts