Last active
August 29, 2015 14:03
-
-
Save typosone/476c7d6bb15a700a6010 to your computer and use it in GitHub Desktop.
Java講義サンプル (コレクション)
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; | |
public class ArrayListTest1 { | |
public static void main(String...args) { | |
ArrayList<String> list = new ArrayList<String>(); | |
list.add("Alice"); | |
list.add("Bob"); | |
list.add("Chris"); | |
// さらに追加 | |
list.add("Diana"); | |
list.add("Elmo"); | |
for (int i = 0; i < list.size(); i++) { | |
System.out.println(list.get(i)); | |
} | |
} | |
} |
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.Iterator; | |
public class ArrayListTest2 { | |
public static void main(String...args) { | |
ArrayList<String> list = new ArrayList<String>(); | |
list.add("Alice"); | |
list.add("Bob"); | |
list.add("Chris"); | |
list.add("Diana"); | |
list.add("Elmo"); | |
Iterator<String> it = list.iterator(); | |
while (it.hasNext()) { | |
String name = it.next(); | |
System.out.println(name); | |
} | |
} | |
} |
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; | |
public class ArrayListTest3 { | |
public static void main(String...args) { | |
ArrayList<String> list = new ArrayList<String>(); | |
list.add("Alice"); | |
list.add("Bob"); | |
list.add("Chris"); | |
list.add("Diana"); | |
list.add("Elmo"); | |
for (String name : list) { | |
System.out.println(name); | |
} | |
} | |
} |
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; | |
public class ArrayListTest5 { | |
public static void main(String...args) { | |
// intのコレクション | |
ArrayList<Integer> list = new ArrayList<Integer>(); | |
list.add(12); | |
list.add(34); | |
list.add(56); | |
list.add(78); | |
list.add(90); | |
for (int n : list) { | |
System.out.println(n); | |
} | |
} | |
} |
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; | |
public class ArrayListTest6 { | |
public static void main(String...args) { | |
ArrayList<String> list = new ArrayList<String>(); | |
list.add("Alice"); | |
list.add("Bob"); | |
list.add("Chris"); | |
list.add("Diana"); | |
list.add("Elmo"); | |
// 削除前に要素を表示 | |
System.out.println("削除前"); | |
for (int i = 0; i < list.size(); i++) { | |
System.out.println(i + ": " + list.get(i)); | |
} | |
System.out.println(); | |
// Aliceは含まれているか? | |
if (list.contains("Alice")) { | |
System.out.println("listにAliceは含まれています。"); | |
} else { | |
System.out.println("listにAliceは含まれていません。"); | |
} | |
// いくつか削除 | |
list.remove("Alice"); | |
list.remove("Bob"); | |
list.remove("Elmo"); | |
// 削除後に要素を表示 | |
System.out.println("削除後"); | |
for (int i = 0; i < list.size(); i++) { | |
System.out.println(i + ": " + list.get(i)); | |
} | |
System.out.println(); | |
// Aliceは含まれているか? | |
if (list.contains("Alice")) { | |
System.out.println("listにAliceは含まれています。"); | |
} else { | |
System.out.println("listにAliceは含まれていません。"); | |
} | |
} | |
} |
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.Map; | |
import java.util.HashMap; | |
public class HashMapTest1 { | |
public static void main(String...args) { | |
Map<String, Integer> map = new HashMap<String, Integer>(); | |
map.put("Alice", 100); | |
map.put("Bob", 57); | |
map.put("Chris", 85); | |
map.put("Diana", 85); | |
map.put("Elmo", 92); | |
// エントリに基づいてあれ | |
for (Map.Entry<String, Integer> entry : map.entrySet()) { | |
System.out.println(entry.getKey() + " => " + entry.getValue()); | |
} | |
System.out.println(); | |
// キーに基づいてのアレ | |
for (String name : map.keySet()) { | |
System.out.println(name); | |
} | |
System.out.println(); | |
// 値に基づいてのアレ | |
for (int value : map.values()) { | |
System.out.println(value); | |
} | |
System.out.println(); | |
// Bob の値を得る | |
System.out.println(map.get("Bob")); | |
// Fred の値を得る | |
System.out.println(map.get("Fred")); | |
} | |
} |
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.Set; | |
import java.util.HashSet; | |
public class HashSetTest1 { | |
public static void main(String...args) { | |
Set<String> set = new HashSet<String>(); | |
set.add("Alice"); | |
set.add("Bob"); | |
set.add("Chris"); | |
set.add("Diana"); | |
set.add("Elmo"); | |
set.add("Chris"); | |
for (String name : set) { | |
System.out.println(name); | |
} | |
if (set.contains("Alice")) { | |
System.out.println("set に Alice は含まれています"); | |
} else { | |
System.out.println("set に Alice は含まれていません"); | |
} | |
} | |
} |
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; | |
public class LinkedListTest1 { | |
public static void main(String...args) { | |
LinkedList<String> list = new LinkedList<String>(); | |
list.add("Bob"); | |
list.add("Chris"); | |
list.add("Diana"); | |
list.add("Elmo"); | |
System.out.println(list); | |
list.addFirst("Alice"); | |
System.out.println(list); | |
} | |
} |
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.Queue; | |
public class QueueTest1 { | |
public static void main(String...args) { | |
Queue<String> queue = new LinkedList<String>(); | |
queue.offer("Alice"); | |
System.out.println("offer 後の queue = " + queue); | |
queue.offer("Bob"); | |
System.out.println("offer 後の queue = " + queue); | |
queue.offer("Chris"); | |
System.out.println("offer 後の queue = " + queue); | |
queue.offer("Diana"); | |
System.out.println("offer 後の queue = " + queue); | |
while (queue.peek() != null) { | |
String name = queue.poll(); | |
System.out.println("pollの戻り値 = " + name); | |
System.out.println("poll 後の queue = " + queue); | |
} | |
} | |
} |
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.NoSuchElementException; | |
public class StackTest1 { | |
public static void main(String...args) { | |
LinkedList<String> stack = new LinkedList<String>(); | |
stack.addFirst("Alice"); | |
System.out.println("addFirst 後の stack = " + stack); | |
stack.addFirst("Bob"); | |
System.out.println("addFirst 後の stack = " + stack); | |
stack.addFirst("Chris"); | |
System.out.println("addFirst 後の stack = " + stack); | |
stack.addFirst("Elmo"); | |
System.out.println("addFirst 後の stack = " + stack); | |
try { | |
while (true) { | |
//要素の抽出と削除 | |
String name = stack.removeFirst(); | |
System.out.println("removeFirst の戻り値 = " + name); | |
System.out.println("removeFirst 後の stack = " + stack); | |
} | |
} catch (NoSuchElementException e) { | |
System.out.println("stack は空になりました"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment