Java basic learning content 2 (array / ArrayList)

What about this article?

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.

Array

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

Java basic learning content 2 (array / ArrayList)
Java basic learning content 7 (exception)
Java basic learning content 5 (modifier)
Java Basic Learning Content 8 (Java API)
Java basic learning content 4 (repetition)
Java basic learning content 3 (operator / ternary operator)
Java basic learning content 9 (lambda expression)
Java basic learning content 1 (literals, variables, constants)
Java learning memo (basic)
Java Basic Learning Content 6 (Inheritance / Abstract Class / Interface)
[Java] Convert ArrayList to array
[Beginner] Java basic "array" description
[Java] array
Java array
Java array
Java learning memo (creating an array)
java (array)
Java array
[Java] Array
Java array
[Java] Difference between array and ArrayList
java array
[Java] Array
[Java] Convert array to ArrayList * Caution
java array variable
For JAVA learning (2018-03-16-01)
Java learning day 5
Java basic grammar
Java basic grammar
[Java] Array notes
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Java basic grammar
Java exercises [Basic]
java learning day 2
java learning day 1
Ruby learning points (basic)
Java learning 2 (learning calculation method)
java learning (conditional expression)
Java learning memo (method)
Java Learning (1)-Hello World
[Java] Data type ①-Basic type
Java basic date manipulation
Java basic naming conventions
[Java ~ Array ~] Study memo 4
Java learning memo (interface)
Java learning memo (inheritance)
[Java] Basic method notes
[Java] What is ArrayList?
Java basic data types
Basic Java OOps concepts
[Java] ArrayList → Should I specify the size in array conversion?
Java beginners briefly summarized the behavior of Array and ArrayList
Learning Java framework # 1 (Mac version)
[Java] List type / Array type conversion
Basic Authentication with Java 11 HttpClient
Java basic syntax + α trap
JAVA learning history interface inheritance
Java learning memo (data type)