Javaでのコレクションのメソッドと反復器
17746 ワード
しゅうごう
コレクションcollectionインタフェースのメソッド
集合の基本メソッド
追加
強制変換
addAllメソッド
containsAllメソッド
removeAllメソッド
retainAll
反復器
巡回集合
強制型変換
:
1. ( )
2.
:
1.
: ( )
2.
コレクションcollectionインタフェースのメソッド
集合の基本メソッド
@SuppressWarnings({ "rawtypes", "unchecked" })
//
Collection collection = new ArrayList();
//
// ?
//ArrayList add
// ? ---
//
boolean b1 = collection.add("a");
boolean b2 = collection.add("b");
boolean b3 = collection.add("c");
//
//
boolean b4 = collection.add(10);
boolean b5 = collection.add(true);
boolean b6 = collection.add('s');
//
System.out.println(collection.toString());
// System.out.println(b1);
// System.out.println(b2);
// System.out.println(b3);
// System.out.println(b4);
// System.out.println(b5);
// System.out.println(b6);
//
System.out.println(collection.size());
//
boolean b7 = collection.contains("b");
System.out.println(b7);
//
boolean b8 = collection.remove(true);
System.out.println(b8);
//
System.out.println(collection);
//
boolean b9 = collection.isEmpty();
System.out.println(b9);
//
collection.clear();
System.out.println(collection);
追加
public static void fun2() {
Collection collection = new ArrayList();
// a b c d
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
System.out.println(collection);
//
Object[] array = collection.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println(Arrays.toString(array));
}
強制変換
:
Student
set/get
public static void fun3() {
/*
*
* 3
*
*/
Collection collection = new ArrayList();
collection.add(new Student("sxm", 15));
collection.add(new Student("ygs", 16));
collection.add(new Student("xsd", 17));
// ( )
Object[] array = collection.toArray();
//
for (int i = 0; i < array.length; i++) {
//array[i] Object
// Student
//
Student student = (Student)array[i];
System.out.println(student.getName());
}
}
addAllメソッド
Collection collection = new ArrayList();
collection.add("ygs");
collection.add("sxm");
collection.add("dp");
//addAll
Collection collection2 = new ArrayList();
collection2.add("y");
collection2.add("s");
collection2.add("d");
collection2.addAll(collection);
// collection2 collection
//collection2.add(collection);
// [y, s, d, ygs, sxm, dp, [ygs, sxm, dp]]
System.out.println(collection);
System.out.println(collection2);
containsAllメソッド
Collection collection = new ArrayList();
collection.add("ygs");
collection.add("sxm");
collection.add("dp");
//containsAll true
Collection collection2 = new ArrayList();
collection2.add("ygs");
collection2.add("sxm");
collection2.add("dp");
boolean containsAll = collection2.containsAll(collection);
System.out.println(containsAll);
removeAllメソッド
Collection collection = new ArrayList();
collection.add("ygs");
collection.add("sxm");
collection.add("dp");
//removeAll
//
//
//
Collection collection2 = new ArrayList();
collection2.add("ygs");
collection2.add("sxm");
collection2.add("dp");
collection2.add("wl");
collection2.removeAll(collection);
System.out.println(collection2);
retainAll
Collection collection = new ArrayList();
collection.add("ygs");
collection.add("sxm");
collection.add("dp");
//retainAll
// collection2 ( )
// collection collection2 collection2
// collection2 true
// false
Collection collection2 = new ArrayList();
collection2.add("ygs");
collection2.add("sxm");
collection2.add("dp");
collection2.add("wl");
boolean retainAll = collection2.retainAll(collection);
System.out.println(collection2);
System.out.println(retainAll);
}
反復器
public static void fun1() {
//
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
//
Iterator iterator = collection.iterator();
//
boolean isHasNext = iterator.hasNext();
System.out.println(isHasNext);
//
Object next = iterator.next();
System.out.println(next);
}
巡回集合
public static void fun2() {
//
Collection collection = new ArrayList();
//
// next
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
Iterator iterator = collection.iterator();
//
while (iterator.hasNext()) {
// : next()
System.out.println(iterator.next());
}
}
強制型変換
public static void fun3() {
/*
*
* 3
*
*/
Collection collection = new ArrayList();
collection.add(new Student("sxm",18));
collection.add(new Student("ygs",19));
collection.add(new Student("dp",20));
//
Iterator iterator = collection.iterator();
//
while (iterator.hasNext()) {
// ( )
Object next = iterator.next();
// Student ( )
Student student = (Student)next;
//
System.out.println(student.getName());
}
}
Day.15