学Java第四十四天——可变参数、Collections工具类
一、可变参数
一个函数的参数的个数是可以变化的,因为无论多少个参数,都可以用(int ...参数名)来承接,这样的话,函数拿到的就是一个数组。
数组也可以用增强for循环
格式:
函数名(数据类型 ... 参数名)

代码:
package com.Map_exe;public class changeParam {public static void main(String[] args) {//可变参数System.out.println(getSum(1,2,3,4,5));}public static int getSum(int ...args){System.out.println(args);int sum=0;for (int arg : args) {sum+=arg;}return sum;}
}
注意细节:
- 在方法的形参中最多只能写一个可变参数,可变参数:理解为一个大胖子,有多少吃多少
- 在方法的形参中,如果出了可变参数以外,还有其他的形参,那么可变参数要写在最后 ,例如(int,a,int...b)
总结:

二、Collections


代码:
package com.Map_exe;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class Collections_exe {public static void main(String[] args) {ArrayList<String> list=new ArrayList<>();//addAll()Collections.addAll(list,"abc","hfh","gdgd","hgfh");System.out.println(list);//shuffle()Collections.shuffle(list);System.out.println(list);System.out.println("----------sout默认规则-----------");//sort() Integer、string类型的数据已经写好了Comparable接口的compareTo方法,默认按照升序,ascii表,//但是如果排序自定义类型的对象,就要重写compareTo方法了,要自己制定规则ArrayList<Integer> l1=new ArrayList<>();Collections.addAll(l1,12,4,5,9,3,8);Collections.sort(l1);System.out.println(l1);System.out.println("---------sort指定规则-------------");Collections.sort(l1, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o2-o1;}});System.out.println(l1);System.out.println("-----------binarySearch------------");//需要元素升序,降序不行System.out.println(Collections.binarySearch(l1, 8));//错误System.out.println(Collections.binarySearch(l1, 12));//❌Collections.sort(l1);System.out.println(Collections.binarySearch(l1, 9));System.out.println(Collections.binarySearch(l1, 12));//✔System.out.println("------------copy--------------");//把list3的元素拷贝到list4中ArrayList<Integer> list3=new ArrayList<>();ArrayList<Integer> list4=new ArrayList<>();Collections.addAll(list3,1,2,3,4,5);Collections.addAll(list4,1,0,5,0,0,0,0);Collections.copy(list4,list3);System.out.println(list3);System.out.println(list4);System.out.println("-----------fill--------");//把集合中现有数据都改为指定数据Collections.fill(list3,100);System.out.println(list3);System.out.println("------------max/min------------");System.out.println(Collections.max(list4));System.out.println("----------max/min指定规则----------");//string默认规则是abcdefg……,想改为按字符串长度排序从小到大,并找到那个最大值ArrayList<String> ll=new ArrayList<>();Collections.addAll(ll,"avf","a","hfhfh");System.out.println(Collections.max(ll, new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return o1.length()-o2.length();}}));System.out.println("----------swap-----------");System.out.println(ll);Collections.swap(ll,0,2);System.out.println(ll);}
}
输出:
[abc, hfh, gdgd, hgfh]
[hfh, gdgd, hgfh, abc]
----------sout默认规则-----------
[3, 4, 5, 8, 9, 12]
---------sort指定规则-------------
[12, 9, 8, 5, 4, 3]
-----------binarySearch------------
2
-7
4
5
------------copy--------------
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 0, 0]
-----------fill--------
[100, 100, 100, 100, 100]
------------max/min------------
5
----------max/min指定规则----------
hfhfh
----------swap-----------
[avf, a, hfhfh]
[hfhfh, a, avf]三、综合练习
练习一

package com;import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;public class Collection_m_test {public static void main(String[] args) {//班里有n个学生,包含姓名、年龄、性别// 实现随机点名//1、定义集合ArrayList<String> list=new ArrayList<>();//2、添加数据Collections.addAll(list,"王春","李湘","谢娜","张杰","郭晓明");//3、随机点名Random r=new Random();int index=r.nextInt(list.size());System.out.println(list.get(index));//另一种方法Collections.shuffle(list);System.out.println(list.get(0));}
}
练习二

先创建一个数组,里面包含7个1,3个0,随机抽取该数组中的一个元素,可以达到题目中的概率
抽取1则去男生里面点名,0则去女生里面点名
package com.collections_m_exe;import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;public class test2 {public static void main(String[] args) {ArrayList<Integer> l=new ArrayList<>();Collections.addAll(l,1,1,1,1,1,1,1);Collections.addAll(l,0,0,0);Random r=new Random();int index=r.nextInt(l.size());int number=l.get(index);System.out.println(number);ArrayList<String> boy=new ArrayList<>();ArrayList<String> girl=new ArrayList<>();Collections.addAll(boy,"李晨","鹿晗","陈赫","郑凯","王宝强");Collections.addAll(girl,"baby","迪丽热巴","杨颖");if(number==1){//选男生int i = r.nextInt(boy.size());System.out.println(boy.get(i));}else {//选女生int i = r.nextInt(girl.size());System.out.println(girl.get(i));}}
}
练习三

package com.collections_m_exe;import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;public class test3 {public static void main(String[] args) {ArrayList<String> list=new ArrayList<>();Collections.addAll(list,"李晨","小帅","凌晨","宋小宝","张玲和","巴布亚","卢卡斯");ArrayList<String> ll=new ArrayList<>();for (int i = 1; i <=10; i++) {//外循环,表示点名的轮数System.out.println("============这是第"+i+"轮点名================");Random r=new Random();int count=list.size();for (int j = 0; j < count; j++) { //内循环,表示每一轮点名的过程int index=r.nextInt(list.size());String name = list.remove(index);ll.add(name);System.out.println(name);}list.addAll(ll);ll.clear();}}
}练习四

package com.collections_m_exe;import java.util.*;public class test4 {public static void main(String[] args) {HashMap<String, ArrayList<String>> hm=new HashMap<>();ArrayList<String> l1=new ArrayList<>();Collections.addAll(l1,"南京市","扬州市","苏州市","无锡市","常州市");ArrayList<String> l2=new ArrayList<>();Collections.addAll(l2,"武汉市","孝感市","宜昌市","鄂州市","十堰市");ArrayList<String> l3=new ArrayList<>();Collections.addAll(l3,"石家庄市","唐山市","邢台市","保定市","张家口市");hm.put("江苏省",l1);hm.put("湖北省",l2);hm.put("河北省",l3);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);}}
}
