您的位置:首页 > 健康 > 美食 > 网页制作软件frontpage2000属于_android studio期末大作业_百度信息流广告怎么收费_湖南网站seo公司

网页制作软件frontpage2000属于_android studio期末大作业_百度信息流广告怎么收费_湖南网站seo公司

2024/10/6 5:48:15 来源:https://blog.csdn.net/weixin_63106307/article/details/142712037  浏览:    关键词:网页制作软件frontpage2000属于_android studio期末大作业_百度信息流广告怎么收费_湖南网站seo公司
网页制作软件frontpage2000属于_android studio期末大作业_百度信息流广告怎么收费_湖南网站seo公司

目录

7. Collections工具类

7.1 常用方法

7.2 举例

7.3 练习


7. Collections工具类

        参考操作数组的工具类:Arrays,Collections 是一个操作 Set、List 和 Map 等集合的工具类。

7.1 常用方法

        Collections 中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作,还提供了对集合对象设置不可变、对集合对象实现同步控制等方法(均为static方法):

排序操作:

  • reverse(List):反转 List 中元素的顺序

  • shuffle(List):对 List 集合元素进行随机排序

  • sort(List):根据元素的自然顺序对指定 List 集合元素按升序排序

  • sort(List,Comparator):根据指定的 Comparator 产生的顺序对 List 集合元素进行排序

  • swap(List,int, int):将指定 list 集合中的 i 处元素和 j 处元素进行交换

查找

  • Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素

  • Object max(Collection,Comparator):根据 Comparator 指定的顺序,返回给定集合中的最大元素

  • Object min(Collection):根据元素的自然顺序,返回给定集合中的最小元素

  • Object min(Collection,Comparator):根据 Comparator 指定的顺序,返回给定集合中的最小元素

  • int binarySearch(List list,T key)在List集合中查找某个元素的下标,但是List的元素必须是T或T的子类对象,而且必须是可比较大小的,即支持自然排序的。而且集合也事先必须是有序的,否则结果不确定。

  • int binarySearch(List list,T key,Comparator c)在List集合中查找某个元素的下标,但是List的元素必须是T或T的子类对象,而且集合也事先必须是按照c比较器规则进行排序过的,否则结果不确定。

  • int frequency(Collection c,Object o):返回指定集合中指定元素的出现次数

复制、替换

  • void copy(List dest,List src):将src中的内容复制到dest中

  • boolean replaceAll(List list,Object oldVal,Object newVal):使用新值替换 List 对象的所有旧值

  • 提供了多个unmodifiableXxx()方法,该方法返回指定 Xxx的不可修改的视图。

添加

  • boolean addAll(Collection c,T... elements)将所有指定元素添加到指定 collection 中。

同步

  • Collections 类中提供了多个 synchronizedXxx() 方法,该方法可使将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题:

7.2 举例

import org.junit.Test;import java.text.Collator;
import java.util.*;public class TestCollections {@Testpublic void test01(){/*public static <T> boolean addAll(Collection<? super T> c,T... elements)将所有指定元素添加到指定 collection 中。Collection的集合的元素类型必须>=T类型*/Collection<Object> coll = new ArrayList<>();Collections.addAll(coll, "hello","java");Collections.addAll(coll, 1,2,3,4);Collection<String> coll2 = new ArrayList<>();Collections.addAll(coll2, "hello","java");//Collections.addAll(coll2, 1,2,3,4);//String和Integer之间没有父子类关系}@Testpublic void test02(){
/** public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)* 在coll集合中找出最大的元素,集合中的对象必须是T或T的子类对象,而且支持自然排序
*  
*  public static <T> T max(Collection<? extends T> coll,Comparator<? super T> comp)
*  在coll集合中找出最大的元素,集合中的对象必须是T或T的子类对象,按照比较器comp找出最大者
*
*/List<Man> list = new ArrayList<>();list.add(new Man("张三",23));list.add(new Man("李四",24));list.add(new Man("王五",25));/** Man max = Collections.max(list);//要求Man实现Comparable接口,或者父类实现* System.out.println(max);*/Man max = Collections.max(list, new Comparator<Man>() {@Overridepublic int compare(Man o1, Man o2) {return o2.getAge()-o2.getAge();}});System.out.println(max);}@Testpublic void test03(){/** public static void reverse(List<?> list)* 反转指定列表List中元素的顺序。*/List<String> list = new ArrayList<>();Collections.addAll(list,"hello","java","world");System.out.println(list);Collections.reverse(list);System.out.println(list);}@Testpublic void test04(){/** public static void shuffle(List<?> list) * List 集合元素进行随机排序,类似洗牌,打乱顺序*/List<String> list = new ArrayList<>();Collections.addAll(list,"hello","java","world");Collections.shuffle(list);System.out.println(list);}@Testpublic void test05() {/** public static <T extends Comparable<? super T>> void sort(List<T> list)* 根据元素的自然顺序对指定 List 集合元素按升序排序** public static <T> void sort(List<T> list,Comparator<? super T> c)* 根据指定的 Comparator 产生的顺序对 List 集合元素进行排序*/List<Man> list = new ArrayList<>();list.add(new Man("张三",23));list.add(new Man("李四",24));list.add(new Man("王五",25));Collections.sort(list);System.out.println(list);Collections.sort(list, new Comparator<Man>() {@Overridepublic int compare(Man o1, Man o2) {return Collator.getInstance(Locale.CHINA).compare(o1.getName(),o2.getName());}});System.out.println(list);}@Testpublic void test06(){/** public static void swap(List<?> list,int i,int j)* 将指定 list 集合中的 i 处元素和 j 处元素进行交换*/List<String> list = new ArrayList<>();Collections.addAll(list,"hello","java","world");Collections.swap(list,0,2);System.out.println(list);}@Testpublic void test07(){/** public static int frequency(Collection<?> c,Object o)* 返回指定集合中指定元素的出现次数*/List<String> list = new ArrayList<>();Collections.addAll(list,"hello","java","world","hello","hello");int count = Collections.frequency(list, "hello");System.out.println("count = " + count);}@Testpublic void test08(){/** public static <T> void copy(List<? super T> dest,List<? extends T> src)* 将src中的内容复制到dest中*/List<Integer> list = new ArrayList<>();for(int i=1; i<=5; i++){//1-5list.add(i);}List<Integer> list2 = new ArrayList<>();for(int i=11; i<=13; i++){//11-13list2.add(i);}Collections.copy(list, list2);System.out.println(list);List<Integer> list3 = new ArrayList<>();for(int i=11; i<=20; i++){//11-20list3.add(i);}//java.lang.IndexOutOfBoundsException: Source does not fit in dest//Collections.copy(list, list3);//System.out.println(list);}@Testpublic void test09(){/** public static <T> boolean replaceAll(List<T> list,T oldVal,T newVal)* 使用新值替换 List 对象的所有旧值*/List<String> list = new ArrayList<>();Collections.addAll(list,"hello","java","world","hello","hello");Collections.replaceAll(list, "hello","song");System.out.println(list);}
}

7.3 练习

练习1:模拟斗地主洗牌和发牌,牌没有排序。牌没有排序,注意不要忘了大小王。

提示:考查HashMap、TreeSet、ArrayList、Collections

代码示例:

package exer4;import java.util.ArrayList;
import java.util.Collections;/*** ClassName:IntelliJ IDEA* Description:*      模拟斗地主洗牌和发牌并对牌进行排序的代码实现。牌没有排序,注意不要忘了大小王。** @Author zyjstart* @Create:2024/10/5 12:05*/
public class PoKerTest {public static void main(String[] args) {// 1、组成一副扑克牌String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};String[] color = {"方片♦", "梅花♣", "红桃♥", "黑桃♠"};ArrayList poker = new ArrayList();for (int i = 0; i < color.length; i++) {for (int j = 0; j < num.length; j++) {poker.add(color[i] + " " + num[j]);}}// 添加大小王poker.add("小王");poker.add("大王");//2、洗牌,将数据打乱Collections.shuffle(poker);//3、发牌//3.1、创建3个角色和1个底牌对应的4个ArrayListArrayList tom = new ArrayList();ArrayList jerry = new ArrayList();ArrayList me = new ArrayList();ArrayList lastGards = new ArrayList();for (int i = 0; i < poker.size(); i++) {if (i >= poker.size() - 3){     //后3张给底牌lastGards.add(poker.get(i));}else if (i % 3 == 0){          // 给tomtom.add(poker.get(i));}else if (i % 3 == 1){          // 给jerryjerry.add(poker.get(i));}else if (i % 3 == 2){          // 给自己me.add(poker.get(i));}}//3.2、遍历显示4个ArrayListSystem.out.println("tom:");System.out.println(tom);System.out.println("jerry:");System.out.println(jerry);System.out.println("me:");System.out.println(me);System.out.println("lastGards:");System.out.println(lastGards);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com