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