set不重复:
package main.chapter11;
import java.util.*;
/**
* p219
*
* @author
* @create 2019-04-17 上午12:09
**/
public class SimpleCollection {
public static void main(String[] args){
Collection<Integer> c = new ArrayList<Integer>();
Collection<Integer> b = new HashSet<>();
for (int i=0;i<10;i++){
b.add(i);
b.add(i);
}
System.out.println(b.size());
for (Integer s :
b) {
System.out.println(s);
}
}
}
//执行结果:0到9
Arrays.asList()的用法,1可变参数、2数组
Collections.addAll的用法
list不可add扩展
package main.chapter11;
import java.util.*;
/**
* p220
*
* @author
* @create 2019-04-17 上午12:26
**/
public class AddingGroups {
public static void main(String[] args){
Collection<Integer> collection = new ArrayList<>(Arrays.asList(1,2,3,4,5));
Integer[] moreInts = {6,7,8,9,10};
collection.addAll(Arrays.asList(moreInts));
Collections.addAll(collection,11,12,13,14,15);
Collections.addAll(collection,moreInts);
List<Integer> list = Arrays.asList(16,17,18,19);
list.set(1,100);
// list.add(101);
for (Integer i :
list) {
System.out.println(i);
}
System.out.println("=======================");
for (Integer i:
collection) {
System.out.println(i);
}
}
}
练习4
package main.chapter11;
import java.util.*;
/**
* ex4,page223
*
* @author
* @create 2019-04-17 上午12:46
**/
public class GenerateCollection {
public static String[] movies = new String[]{"11","42","33"};
public static int index = 0;
public static Collection next(Collection<String> collection){
int trueIndex = index%movies.length;
collection.add(movies[trueIndex]);
index++;
return collection;
}
public static void main(String[] args){
for (int i =0;i<10;i++){
System.out.println("ArrayList" + GenerateCollection.next(new ArrayList<String>()));
System.out.println("LinkedList" + GenerateCollection.next(new LinkedList<String>()));
System.out.println("HashSet" + GenerateCollection.next(new HashSet<String>()));
System.out.println("LinkedHashSet" + GenerateCollection.next(new LinkedHashSet<String>()));
System.out.println("TreeSet" + GenerateCollection.next(new TreeSet<String>()));
}
}
}
List各方法
package main.chapter11;
import java.util.*;
/**
* p223
*
* @author
* @create 2019-04-17 上午1:27
**/
public class ListFeatures {
public static void main(String[] args){
List<String> pets = new ArrayList<>();
pets.add("dog");
pets.add("cat");
pets.add("fish");
pets.add("duck");
pets.add("bear");
System.out.println("2" + pets);
pets.remove("dog");
System.out.println("3" + pets);
pets.add(2,"dragon");
System.out.println("4" + pets);
List<String> sub = pets.subList(2,5);
System.out.println("5" + sub);
Collections.sort(sub);
System.out.println("6" + sub);
Collections.shuffle(sub,new Random(33));
System.out.println("7" + sub);
List<String> copy = new ArrayList<>(pets);
sub = Arrays.asList(sub.get(1),sub.get(2));
System.out.println("8" + sub);
//交集
copy.retainAll(sub);
System.out.println("9" + copy);
copy = new ArrayList<>(pets);
copy.removeAll(sub);
System.out.println("10" + copy);
copy.addAll(2,sub);
System.out.println("11" + copy);
Object[] s = copy.toArray();
System.out.println("12" + s[0]);
String[] ss = copy.toArray(new String[0]);
System.out.println("13" + ss[0]);
}
}
网友评论