本文主要是介绍Collections工具类及其案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package exercise;public class Demo1 {public static void main(String[] args) {//可变参数//方法形参的个数是可以发生变化的//格式:属性类型...名字//int...argsint sum = getSum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);System.out.println(sum);}//底层:可变参数底层就是一个数组public static int getSum(int... number) {int sum = 0;for (int i : number) {sum += i;}return sum;}
}
细节:
1.在方法的形参中最多只能写一个可变参数
2.在方法的形参中,如果除了可变参数以外,还有其他的形参,那么可变参数要写在最后
package exercise;import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;public class Demo3 {public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();Collections.addAll(list, "张三", "李四", "王五");/*方法一:Random r = new Random();int i = r.nextInt(list.size());System.out.println(list.get(i));*///方法二:Collections.shuffle(list);String s = list.get(0);System.out.println(s);}
}
package exercise;import java.util.ArrayList;
import java.util.Collections;public class Demo4 {public static void main(String[] args) {/*1.创建一个整数数组(1:男生 0:女生),用来判断概率2.分别用两个集合来存储男生和女生3.将上述集合打乱4.用整数数组得到的结果来输出 男生\女集合打乱后的第0个元素*/ArrayList<Integer> list = new ArrayList<>();Collections.addAll(list, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);Collections.shuffle(list);System.out.println(list.get(0));ArrayList<String> boyList = new ArrayList<>();ArrayList<String> girlList = new ArrayList<>();Collections.addAll(boyList, "男1", "男2", "男3");Collections.addAll(girlList, "女1", "女2", "女3");Collections.shuffle(boyList);Collections.shuffle(girlList);if (list.get(0) == 1) {System.out.println(boyList.get(0));} else {System.out.println(girlList.get(0));}}
}
shift+F6:批量改名
package exercise;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class Demo5 {public static void main(String[] args) {ArrayList<String> list1 = new ArrayList<>();Collections.addAll(list1, "张三", "李四", "王五", "赵六");ArrayList<String> list2 = new ArrayList<>();for (int i = 0; i < 10; i++) {System.out.println("============第" + i + "轮================");Collections.shuffle(list1);int count = list1.size();for (int j = 0; j < count; j++) {String remove = list1.remove(0);list2.add(remove);System.out.println(remove);}list1.addAll(list2);list2.clear();}}
}
package exercise;import java.util.*;public class Demo6 {public static void main(String[] args) {HashMap<String, ArrayList<String>> hm = new HashMap<>();ArrayList<String> list1 = new ArrayList<>();Collections.addAll(list1, "桂林", "柳州", "南宁");ArrayList<String> list2 = new ArrayList<>();Collections.addAll(list2, "苏州", "无锡", "南京");ArrayList<String> list3 = new ArrayList<>();Collections.addAll(list3, "哈尔滨", "大庆", "齐齐哈尔");hm.put("广西", list1);hm.put("江苏", list2);hm.put("黑龙江", list3);Set<Map.Entry<String, ArrayList<String>>> entries = hm.entrySet();for (Map.Entry<String, ArrayList<String>> entry : entries) {String key = entry.getKey();ArrayList<String> value = entry.getValue();StringJoiner sj = new StringJoiner(",", "", "");for (String s : value) {sj.add(s);}System.out.println(key + " = " + sj);}}
}
这篇关于Collections工具类及其案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!