「Java入門」Listを操作するサンプルコード

2017年2月8日

Javaコード:
package com.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CopyList {
//print list method
public static <T> void printList(List<T> list) {
System.out.println(“—begin—“);
for (T t : list) {
System.out.println(t);
}
System.out.println(“—end—“);
}

//print array method
public static <T> void printArray(T[] array) {
System.out.println(“—begin—“);
for (T t : array) {
System.out.println(t);
}
System.out.println(“—end—“);
}

//シリアライゼーション
public static <T> List<T> deepCopy(List<T> src) throws IOException,
ClassNotFoundException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);

ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut
.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings(“unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
}
//init
public static List checkOne() {
List<Person> srcList = new ArrayList<Person>();
Person p1 = new Person(20, “123");
Person p2 = new Person(21, “ABC");
Person p3 = new Person(22, “abc");
srcList.add(p1);
srcList.add(p2);
srcList.add(p3);
return srcList;
}
//コピートラバーサルサイクル
public static void checkTwo(List<Person> srcList) {
List<Person> destList = new ArrayList<Person>(srcList.size());
for (Person p : srcList) {
destList.add(p);
}
printList(destList);
srcList.get(0).setAge(100);
printList(destList);
}
//Listでクラスのコンストラクタを実装
public static void checkThree(List<Person> srcList) {
List<Person> destList = new ArrayList<Person>(srcList);
printList(destList);
srcList.get(0).setAge(100);
printList(destList);
}

//list.addAll()
public static void checkFour(List<Person> srcList) {
List<Person> destList = new ArrayList<Person>();
destList.addAll(srcList);
printList(destList);
srcList.get(0).setAge(100);
printList(destList);
}
//System.arraycopy()
public static void checkFive(List<Person> srcList) {
Person[] srcPersons = srcList.toArray(new Person[0]);
Person[] destPersons = new Person[srcPersons.length];
System.arraycopy(srcPersons, 0, destPersons, 0, srcPersons.length);
// destPersons=srcPersons.clone();

printArray(destPersons);
srcPersons[0].setAge(100);
printArray(destPersons);

List<Person> destList = Arrays.asList(destPersons);
printList(destList);
}

public static void checkSix(List<Person> srcList) throws IOException,
ClassNotFoundException {
List<Person> destList = deepCopy(srcList);
printList(destList);
srcList.get(0).setAge(100);
printList(destList);
}

//java.util.Collections.copy
public static void checkSeven(List<Person> srcList){
List<Person> destList = new ArrayList<Person>(srcList);
java.util.Collections.copy(destList, srcList);
printList(destList);
srcList.get(0).setAge(100);
printList(destList);
}

public static void main(String[] args) throws IOException, ClassNotFoundException {
List srcList = checkOne();
// checkTwo(srcList);
// checkThree(srcList);
// checkFour(srcList);
// checkFive(srcList);
// checkSix(srcList);
// checkSeven(srcList);
}
}

class Person implements Serializable {
private int age;
private String name;
public Person() {
};
public Person(int age, String name) {
this.age = age;
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return this.name + “–>" + this.age;
}

}

Java

Posted by arkgame