List<String> data = new ArrayList<>();
<br>
public void run (ArrayList List <String>
in the interface type from the beginning, you can easily change to Linked List! ** **List<String> data = new LinkedList<>();
List <String>
, assign String to general-purpose type (List) and become ** List ** dedicated to string storage!Strng str = (String) data.get (0)
List <String> data = new ArrayList <> ();
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
//Anonymous class
var data = new ArrayList<String>() {
//Initialization block
{
add("neko");
add("inu");
add("tori");
}
};
System.out.println(data);//[neko, inu, tori]
}
}
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
var data = new ArrayList<String>() {
{
add("neko");
add("inu");
add("kuma");
}
};
//Process under collection in order
for (var s : data) {
System.out.println(s); //neko inu kuma
}
//When using an iterator
// var itr = data.iterator();
// while (itr.hasNext()) {
// System.out.println(itr.next());
// }
}
}
ConcurrentModificationException
.import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
var data = new ArrayList<String>() {
{
add("neko");
add("inu");
add("niwatori");
}
};
var ite = data.iterator();
while (ite.hasNext()) {
System.out.println(ite.next()); //neko inu niwatori
ite.remove();
}
System.out.println(data); //[]
}
}
//NG example:Extension for
for (var s : data) {
System.out.println(s);
data.remove(s);
}
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
var data = new ArrayList<String>() {
{
add("neko");
add("inu");
add("shimaenaga");
}
};
//Set the end to the start position
var ite = data.listIterator(data.size());
while (ite.hasPrevious()) {
System.out.println(ite.previous()); //shimaenaga inu neko
}
}
}
//NG example: Convert existing character string to list, add and delete value to converted list
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
var data = new String[] { "cat", "dog", "bird"};
var list = Arrays.asList(data);
list.set(0, "neko");
System.out.println(list); //[neko, dog, bird]
System.out.println(Arrays.toString(data)); //[neko, dog, bird]
//list.add("inu"); //UnsupportedOperationException
//list.remove(0); //UnsupportedOperationException
}
}
//OK Example: Convert existing character string to list, add and delete value to converted list
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
var data = new String[] { "cat", "dog", "bird" };
var list = new ArrayList<String>();
Collections.addAll(list, data);
list.set(0, "neko");
list.add("inu");
System.out.println(list); //[neko, dog, bird, inu]
list.remove(1);
System.out.println(list); //[neko, bird, inu]
System.out.println(Arrays.toString(data)); //[cat, dog, bird]
}
}
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
var data = new ArrayList<String>(Arrays.asList("cat", "dog", "bird"));
//Copy the contents to an array strs of the same size as the list
var strs = new String[data.size()];
data.toArray(strs);
//Add data only to the conversion source list
data.set(0, "neko");
System.out.println(Arrays.toString(strs)); //[cat, dog, bird]
//Only the conversion source list has changed
System.out.println(data); //[neko, dog, bird]
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
var data = new ArrayList<String>(Arrays.asList("cat", "dog", "bird"));
//Generate an immutable list
var udata = Collections.unmodifiableList(data);
udata.set(0, "neko"); //UnsupportedOperationException
udata.add("inu"); //UnsupportedOperationException
}
}
//Collection element contents can be changed in StringBuilder
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
var data = new ArrayList<StringBuilder>(Arrays.asList(
new StringBuilder("Hello"),
new StringBuilder("Kamesan"),
new StringBuilder("Yo")));
var udata = Collections.unmodifiableList(data);
udata.get(0).append("Kameyo");
System.out.println(udata); //[Hello,Kamesan,Yo]
}
}
//Immutable collection directly generated by the of method
import java.util.List;
public class Main {
public static void main(String[] args) {
var data = List.of("cat", "dog", "bird");
System.out.println(data);
data.set(0, "neko"); //UnsupportedOperationException
}
}
return Collections.emptyList();
var data = Collections.synchronizedList(new ArrayList<String>());
var map = new ConcurrentHashMap<String,String>();
Recommended Posts