zoukankan      html  css  js  c++  java
  • RandomAccess接口是空的,那它是用来做什么的呢?

    RandomAccess

    用来当标记的,是一种标记接口,接口的非典型用法

    意思是,随机访问任意下标元素都比较快

    用处,当要实现某些算法时,会判断当前类是否实现了RandomAccess接口

    会根据结果选择不同的算法

    例如:

    作者:Accelerator
    链接:https://www.zhihu.com/question/50909512/answer/123257522
    来源:知乎
    著作权归作者所有,转载请联系作者获得授权。
    
    public static void shuffle(List<?> list, Random rnd) {
            int size = list.size();
            if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {              
            for (int i=size; i>1; i--) swap(list, i-1, rnd.nextInt(i)); } else { Object arr[] = list.toArray(); // Shuffle array for (int i=size; i>1; i--) swap(arr, i-1, rnd.nextInt(i)); // Dump array back into list ListIterator it = list.listIterator(); for (int i=0; i<arr.length; i++) { it.next(); it.set(arr[i]); } } }
  • 相关阅读:
    结构体比较
    不定长参数列表用法
    接口
    字符串数据类型
    *和**的打包和解包
    python类常用装饰器
    继承的实现
    map用法
    包的导入和init函数
    协程
  • 原文地址:https://www.cnblogs.com/imeiling/p/6409011.html
Copyright © 2011-2022 走看看