Now that I have to use lists in Java, I'll review the data structures. The reference site is as follows. -[Data structures that you should definitely remember if you use Java-arrays-list maps](http: // ...)
An array is a data structure for handling multiple data together. In order to use an array, it is necessary to declare the number of data to be stored at the time of generation. It is impossible to change the size later.
python
Data type[]Array name= new Data type[Array length]; //Array declaration
When storing data in an array, it is necessary to specify the array name and index number. Try to store data in int type numbers.
python
int[] numbers = new int[3];
numbers[0] = 5;
numbers[1] = 200;
numbers[2] = 320;
This writing method is cumbersome and can be omitted.
int[] numbers = {5, 200, 320};
When no value is assigned to the array, the value is automatically assigned. (For int type, 0 is assigned)
When retrieving data from an array, use the array name and index number in the same way as when storing it.
for(int i = 0; i < numbers.length; i++){
System.out.println(numbers[i]);
}
Like an array, a list is a data structure for handling multiple data. However, unlike arrays, you don't have to specify the length of the list. That is, the length of the list is variable. When working with lists, you need to import the package at the beginning of the source file.
import java.util.*;
To declare a list, specify the type of data to store and the list name.
List<Data type>List name= new ArrayList<Data type>();
When storing data in the list, use the add method of the list.
List name.add(data);
To get the data from the list, specify the index number in the get method of the list and get it.
List name.get(index number);
To replace the data in the list, specify the index number and the data to be added in the set method of the list.
List name.set(index number,Data to add);
Use the for statement to do the same for the list. The length of the list can be found by using the size method of the list.
for(int i = 0; i <List name.size(); i++){
//Processing content
.
.
}
A map is a data structure that stores the value corresponding to the key. When storing data in the map, associate it with the key. Use the associated key when retrieving data from the map. As with lists, if you want to use a map, you need to import the package into your source file.
import java.util.*;
Map<key data type,data type of value>Map name=
new HashMap<key data type,data type of value>();
python
Map name.put(key, value);
python
Map name.get(key);
Perform the same processing on the data in the map using the extended for statement. The keySet method of the map returns all the keys in the map.
for(data type of key key:Map name.keySet() ) {
Data type data=Map name.get(key);
//processing
}
Recommended Posts