Last active
November 15, 2016 03:57
-
-
Save BumpeiShimada/3be3c1a29d89051b61da9c8235db2b7d to your computer and use it in GitHub Desktop.
コレクションの代表的なクラス ref: http://qiita.com/BumpeiShimada/items/8a5b52af4270d3583acd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | |
B | |
C | |
B | |
Cの位置: 2 | |
サイズ: 4 | |
2番目の位置に新たに文字列を挿入 | |
A | |
B | |
X | |
C | |
B | |
変更後のCの位置: 3 | |
変更後のサイズ: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> al = new ArrayList<String>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | |
B | |
C | |
C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | |
B | |
C | |
C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
null | |
Y | |
X | |
Z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Taro | |
tel: 999-999-9999 | |
aaa: null | |
null: null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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