-[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.
--ʻArrayListis a
collection class that implements the
List interface. ―― ʻArrayList
can be treated like an array as the name ʻArray` suggests.
--The array
has a fixed number of elements that can be stored, but the ʻArrayList has a fixed number of elements
.
--ʻArrayListcannot contain
primitive types (int, boolean, etc.)`.
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() |
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;
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.
[File (F)]-> [New (N)]-> [Java Project]
.
Test1
for the project name and click the Done
button.
[File (F)] → [New (N)] → [Class]
.
Test1
for the package and name and click the Done
button.
Test1.java
has been created.
Enter Test1
in the package and Hello
in the name in the same procedure as in 6.3, and click the Finish
button.
Test1.java
and Hello.java
are created.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. ↓ ↓
-[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