ArrayList常用操作
构造方法
ArrayList(); 构造空的顺序表,容量是默认变量
ArrayList(int capacity); 构造空的顺序表,容量是capacity
ArrayList(Collection c); 构造一个顺序表,把C中所有元素放到顺序表
举例
List origin=Arrays.asList(1,2,3,4,5); 创建链表
Arrays类这个方法里会帮你new对象返回回来
ArrayList list=new ArrayList<>(origin); 把上面的元素拿来创建顺序表
常用方法:
add(E element); 尾插数据
add(int index,E element); 把元素插入到index位置
remove(Object o); 删除顺序表中第一个o;
remove(int index); 删除顺序表中的下标index位置
contains(E e); 判断e在不在顺序表中(查找)
indexOf(E e); 从前数下标
lastIndexOf(E e); 从后数下标
get(int index); 返回下标元素
set(int index,E e) 更改下标元素;
java.util.Random
//伪随机
//种子固定的情况下,随机的产生是固定
//为了避免随机固定,引入时间作为种子
//Rogue-like
Random();
Random(long seed);
nextInt(int bound); [0,bound)
洗牌
java中,自定义的相等性比较
1)对于引用类型
==判断的不是对象相等,而是引用相等,两个引用指向同一个对象
2)Object类中有一个equals方法,在自定义类中覆写该方法去调用该对象的equals
LinkedList 链表 实现List/Deque
构造方法:
LinkedList();
LinkedList(Collection c);
//同ArrayList相同的不列出
void addFirst(E,e);
void addLast(E,e);