[Java] How to use List [ArrayList]

What is List?

An ordered collection that contains multiple elements. Since it is an interface, it can be used by implementing it.

What is a collection? I'm looking into that.

Collection: A mechanism for working with a set of objects. There are the following types.

--List system --ArrayList Handles arrays.

--LinkedList Handles arrays. Insertion / deletion is fast. --Vector Handles arrays. Currently not recommended due to poor performance. --Set system --HashSet A set of elements in no particular order that does not allow duplicate values. --TreeSet: A sorted element set that does not allow duplicate values. --Map system --HashMap A set of elements consisting of a key / value pair. --TreeMap A set of elements consisting of key / value pairs. Sorted by key.

This time I will use ArrayList.

Example of using ArrayList

listtest.java



package listtest;

import java.util.ArrayList;
import java.util.List;

public class Listtest {
	
	public static void main(String[] args) {

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

		list.add("Apple");
		list.add("Mandarin orange");
		list.add("melon");

		System.out.println(list);

	}
}

Execution result.


[Apple,Mandarin orange,melon]

Since ArrayList is an implementation class, you can create an instance as follows.

.


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

Actually, the contents are similar to the following. It means that the list is packed with objects of the String class.

listtest.java


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

		list.add(new String("Apple"));
		list.add(new String("Mandarin orange"));
		list.add(new String("melon"));

		System.out.println(list);

If you want to retrieve one element, use the get method. In the argument, enter the number of the element you want to retrieve from the list.

listtest.java


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

		list.add(new String("Apple"));
		list.add(new String("Mandarin orange"));
		list.add(new String("melon"));

		System.out.println(list.get(1));

Execution result.


Mandarin orange

I got the element out!

Recommended Posts

[Java] How to use List [ArrayList]
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[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
[Java] How to use LinkedHashMap class
[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
[Java] How to add data to List (add, addAll)
How to use submit method (Java Silver)
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
[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
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
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 java.util.logging
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
How to use org.immutables
How to use java.util.stream.Collector