接口作用
实现该接口,就说明支持随机访问方式遍历
例子
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
if (list instanceof RandomAccess) {
//随机访问
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} else {
//迭代器访问
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}