[Java] Basic summary of Java not covered by Progate ~ Part 2 · List ~

list

Progate talks about arrays, but not lists. Arrays and lists are similar, but what's the difference?

Array

python


int result[] = new int[5];

result[0] = 85;
result[1] = 78;
result[2] = 92;
result[3] = 62;
result[4] = 69;

If you compare it to a container, the array result [] must first determine the size of the container. Furthermore, once the size is decided, it cannot be changed after that.

list

python


List<String> list = new ArrayList<String>();

On the other hand, the size of the list is variable, and elements can be added / deleted.

Interface

In java, an interface (like a specification) for handling lists is defined. This List interface is an interface with the roots of Collection.

Package import required

You need to ʻimrport`` java.util.List` to use List.

python


import java.util.List;

Also, since List is an interface (specification), it has no implementation itself, so It is necessary to ʻimport`` class` which implements List interface separately.

ArrayList and LinkedList

There are ʻArrayList and LinkedListinclass` that implements List interface.

Try creating an instance of the ArrayList class

python


import java.util.list;
import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    System.out.println(list);
  }
}


///Execution result
/// >[]

List<String> list = new ArrayList<>(); It declares that the variable is of type List and that the data handled by this List is of type String.

Instantiate LinkedList class

python


import java.util.list;
import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    List<String> list = new LinkedList<>();
    System.out.println(list);
  }
}


///Execution result
/// >[]

LinkedList defines some unique methods that List does not have, but they are omitted here. (Almost not in ArrayList)

For lists dealing with primitive types

List cannot directly handle primitive types such as ʻintandlong. String` is a reference type. See the table in the previous article for more information on primitive and reference types. https://qiita.com/hiroshimahiroshi/items/01de02cfe1caacd07540#3-%E5%9F%BA%E6%9C%AC%E3%83%87%E3%83%BC%E3%82%BF%E5%9E%8B%E3%81%A8%E5%8F%82%E7%85%A7%E5%9E%8B

So when dealing with primitive types such as ʻintandlong, the wrapper class ʻInteger of ʻint, Use the long wrapper class Long`.

python


    List<Integer> list = new LinkedList<>();

Primitive type? Wrapper class? I'm not sure, but for the time being, I just want to know the list first! In that case, think that something is strange if the type of List <type> starts with a lowercase letter for the time being. Let's learn about primitive types, reference types, and wrapper classes separately.

At the end

I edited the article significantly. This article is for people like me who have finished Progate and feel like understanding Java. I've only touched on it, but I think it's important basic knowledge, so let's deepen your understanding with CODEPREP.

Recommended Posts

[Java] Basic summary of Java not covered by Progate ~ Part 2 · List ~
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Summary of basic knowledge of Rails acquired by progate
Basic usage of java Optional Part 1
[Java] Personal summary of conditional statements (basic)
[Java] Personal summary of classes and methods (basic)
[Note] Java: Speed of List processing by purpose
Summary of Java support 2018
Summary of revisions (new era) support by Java version
[Java11] Stream Summary -Advantages of Stream-
Progate Java (Beginner) Review & Summary
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Object-oriented summary by beginners (Java)
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Ruby] List of basic commands
Summary of values returned by the Spliterator characteristics method #java
Summary of basic functions of ImageJ
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 7)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 3)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 9)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 6)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)
The story of not knowing the behavior of String by passing Java by reference
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
[Java] Delete the elements of List
[For beginners] Summary of java constructor
Sort a List of Java objects
[Java] Output by FormatStyle of DateTimeFormatter
List of members added in Java 9
Basic processing flow of java Stream
Java "pass by reference" problem summary
List of types added in Java 9
Summary of object-oriented programming using Java
[Basic knowledge of Java] Scope of variables
Basic structure of Java source code
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
[java] Summary of how to handle char
Summary of changes other than JEP of Java10
Summary of Docker understanding by beginners ② ~ docker-compose ~
Basic knowledge of Java development Note writing
List of download destinations for oracle java
[Java] [Maven3] Summary of how to use Maven3
[Basic knowledge of Java] About type conversion
Java Summary of frequently searched type conversions
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface