Skip to content

Instantly share code, notes, and snippets.

@BumpeiShimada
Last active November 15, 2016 03:57
Show Gist options
  • Save BumpeiShimada/3be3c1a29d89051b61da9c8235db2b7d to your computer and use it in GitHub Desktop.
Save BumpeiShimada/3be3c1a29d89051b61da9c8235db2b7d to your computer and use it in GitHub Desktop.
コレクションの代表的なクラス ref: http://qiita.com/BumpeiShimada/items/8a5b52af4270d3583acd
import java.util.ArrayList;
import java.util.List;
public class ArrayListSample {
public static void main(String args[]) {
//ArrayListの生成とデータのセット
List<String> al = new ArrayList<String>();
al.add("A");
al.add("B");
al.add("C");
al.add("B");
//ArrayListを1件ずつ取り出し画面に表示する
for (String alphabet : al) {
System.out.println(alphabet);
}
// C のインデックスを表示
System.out.println("Cの位置: " + al.indexOf("C"));
// サイズを表示
System.out.println("サイズ: " + al.size());
// 2番目に新たに文字列を挿入
System.out.println();
System.out.println("2番目に新たに文字列を挿入");
al.add(2, "X");
System.out.println();
//変更後の結果を表示
for (String alphabet : al) {
System.out.println(alphabet);
}
System.out.println("変更後のCの位置: " + al.indexOf("C"));
System.out.println("変更後のサイズ: " + al.size());
}
}
A
B
C
B
Cの位置: 2
サイズ: 4
2番目の位置に新たに文字列を挿入
A
B
X
C
B
変更後のCの位置: 3
変更後のサイズ: 5
List<String> al = new ArrayList<String>();
name: Taro
tel: 999-999-9999
aaa: null
null: null
import java.util.HashMap;
public class HashMapSample {
public static void main(String args[]) {
//HashMapデータを作成
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("name", "Taro");
hm.put("address", "Tokyo");
hm.put("tel", "999-999-9999");
//get メソッドでそれぞれのキーに紐づく値を取得
System.out.println("name: " + hm.get("name"));
System.out.println("tel: " + hm.get("tel"));
System.out.println("aaa: " + hm.get("aaa"));
}
}
import java.util.HashSet;
import java.util.Iterator;
public class HashSetSample {
public static void main(String args[]) {
//HashSetデータを作成
HashSet<String> hs = new HashSet<String>();
hs.add("Z");
hs.add("Y");
hs.add("Y");
hs.add("X");
hs.add(null);
//画面表示
Iterator<String> it = hs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
import java.util.LinkedList;
import java.util.List;
public class LinkedListSample {
public static void main(String args[]) {
//LinkedListの生成とデータのセット
List<String> ll = new LinkedList<String>();
ll.add("A");
ll.add("B");
ll.add("C");
ll.add("C");
//LinkedListを1件ずつ取り出し画面に表示する
Iterator<String> it = ll.iterator();
while (it.hasNext()) {
System.out.println((String)it.next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment