[Easy-to-understand explanation! ] How to use ArrayList [Java]

1. Prior knowledge

-[Latest] How to build Java environment on Ubuntu

-[Even beginners can do it! ] How to create a Java environment on Windows 10 (JDK14.0.1)

-[Easy-to-understand explanation! ] How to use Java instance

-[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)

As prior knowledge, the contents of the above link are required.

2. What is ArrayList?

--ʻArrayListis acollection class that implements the List interface. ―― ʻArrayList can be treated like an array as the name ʻArray` suggests.

Difference between ArrayList and array

--The array has a fixed number of elements that can be stored, but the ʻArrayList has a fixed number of elements. --ʻArrayListcannot containprimitive types (int, boolean, etc.)`.

How to use the List method

Method Description Description example
add Add a value to the list. list.add(Element number)
addAll Add a list to the list. list1.add(list)
set Change the value in the list. list.set(Element number,Value to substitute)
get Get the value of the list. list.get(Element number)
size Get the number of elements in the list. list.size()
indexOf Get the element number of the value from the list. list.indexOf(Value to search)
subList Specify a range from the list and make a shallow copy.(* Refers to the same data as the original list.) subList(Starting element number,End element number)
contains Determine if the list contains values. list.contains(Value to search)
remove Delete the value of the specified element number from the list. list.remove(Element number)
distinct Remove duplicate values in the list. list.stream().distinct().collect(Collectors.toList())
clone Copy the list. list.clone()
clear Empty the list. list.clear()

Mainly used class

import


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

3. Basic way to write ArrayList

Test class


package package name;
public class main class name{
    public static void main(String[] args) {
        //Creating an ArrayList
        ArrayList<Reference type name>Variable name= new ArrayList<Reference type name>();
    }
}

--Basic ʻArrayList` is described as above.

4. Preparation

01.png

  1. Start Eclipse and select [File (F)]-> [New (N)]-> [Java Project]. 02.png
  2. Enter Test1 for the project name and click the Done button. 03.png
  3. Select [File (F)] → [New (N)] → [Class]. 05.png
  4. Enter Test1 for the package and name and click the Done button. 06.png
  5. Confirm that Test1.java has been created. 01.png Enter Test1 in the package and Hello in the name in the same procedure as in 6.3, and click the Finish button. 02.png
  6. Success if Test1.java and Hello.java are created.

5. Description example

Test1.java


package Test1;
import java.util.ArrayList;
import java.util.stream.Collectors;

public class Test1 {
    public static void main(String[] args) {
    	//Creating an ArrayList
    	ArrayList<Hello> hello = new ArrayList<Hello>();
    	ArrayList<Hello> subhello = new ArrayList<Hello>();

    	//Add value to ArrayList
    	hello.add(new Hello("A"));
    	hello.add(new Hello("B"));
    	hello.add(new Hello("C"));

    	//Add list to ArrayList
    	subhello.addAll(hello);

    	//Change the value of ArrayList
    	hello.set(0, new Hello("D"));

    	//Number of elements in ArrayList
    	System.out.println("Number of hello elements:"+hello.size());

    	//Matching element numbers between ArrayLists
    	System.out.println("subhello(2)Element number of hello that matches:"+hello.indexOf(subhello.get(2)));

    	//Determine if the ArrayList contains values
    	System.out.println("Does hello contain a value:"+hello.contains(subhello.get(2)));

    	//Delete the value of the specified element number from ArrayList
    	hello.remove(2);

    	//Remove duplicate values in ArrayList.
    	subhello.stream().distinct().collect(Collectors.toList());

    	//Copy the ArrayList.
    	ArrayList<Hello> clonehello = (ArrayList<Hello>) hello.clone();

    	//Display of ArrayList
    	for(Hello h : hello) {
    		h.showGreeting();
    	}
    	System.out.println();
    	for(Hello sub : subhello) {
    		sub.showGreeting();
    	}
    	System.out.println();
    	for(Hello clone : clonehello) {
    		clone.showGreeting();
    	}
    }
}

Hello.java


package Test1;
public class Hello {
	//Instance variables
	String name;

	//constructor
    public Hello(String name) {
        this.name = name;
    }
    //Display greetings
    void showGreeting() {
        System.out.println(name+"Hey,.");
    }
}

--Copy the above sentence, specify S-JIS as the character code, save the file name as Test1.java, Hello.java, and execute it. ↓ ↓ 03.png

6. Related

-[Useful to remember !!!] Easy creation of constructor and getter / setter in Eclipse -[Useful to remember !!!] Easy creation of inherited class in Eclipse -[Even beginners can do it! ] How to write Javadoc -[Easy-to-understand explanation! ] How to use Java overload -[Easy-to-understand explanation! ] How to use Java encapsulation -[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation] -[Easy-to-understand explanation! ] Type conversion of reference type in Java

Recommended Posts

[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Easy-to-understand explanation! ] How to use Java instance
[Easy-to-understand explanation! ] How to use Java polymorphism
[Easy-to-understand explanation! ] How to use Java overload
[Easy-to-understand explanation! ] How to use Java encapsulation
[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]
[Java] How to use List [ArrayList]
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Java classes, definitions, import
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
try-catch-finally exception handling How to use java
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle