Array / list / map

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: // ...)

1. Array

1.1 Array declaration

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

1.2 Store data in an array

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)

1.3 Get data from an array

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]);
}

2. List

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.*;

2.1 List declaration

To declare a list, specify the type of data to store and the list name.

List<Data type>List name= new ArrayList<Data type>();

2.2 Store data in list

When storing data in the list, use the add method of the list.

List name.add(data);

2.3 Get data from a list

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);

2.4 Replace the data in the list

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);

2.5 Do the same for the list

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
  .
  .
}

3. Map

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.*;

3.1 Map Declaration

Map<key data type,data type of value>Map name= 
                     new HashMap<key data type,data type of value>();

3.2 Store map data

python


Map name.put(key, value);

3.3 Get map data

python


Map name.get(key);

3.4 Do the same for the data in the map

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

Array / list / map
List, Set, Map
About Java Array List
[Java] List type / Array type conversion
Array
(Array # each + Array # push) v.s. Array # map
Array
For Java beginners: List, Map, Iterator / Array ... How to convert?
Make a list map with LazyMap
[Java] Convert 1-to-N List to Map
[Java] Conversion from array to List
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map
[Java] array
[Ruby] Array
Java array
Array practice
[Java] Get List / Map elements with Iterator
java (array)
Map without using an array in java
Java array
[Java] Array
Map method
Array practice 2
Thread-safe list
JAVA (Map)
List method
Java array
Work list
12 of Array
java array
[Java] Array
Link list
[Java] From two Lists to one array list
[MyBatis] List <Map <>> Pass nesting as Mapper parameter