ArrayList常用操作
构造方法
ArrayList(); 构造空的顺序表,容量是默认变量
ArrayList(int capacity); 构造空的顺序表,容量是capacity
ArrayList(Collection c); 构造一个顺序表,把C中所有元素放到顺序表
举例
List<Integer> origin=Arrays.asList(1,2,3,4,5); 创建链表
Arrays类这个方法里会帮你new对象返回回来
ArrayList<Integer> 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发那个发
迭代器:Iterator
boolean hasNext();
E next();
void remove();
Iterable(interface)使用了Iterator(interface)
继承 继承
Collection(interface) ListIterator(interface)
继承
List(interface)
实现
ArrayList(class)
LinkedList 链表 实现List/Deque
构造方法:
LinkedList();
LinkedList(Collection c);
//同ArrayList相同的不列出
void addFirst(E,e);
void addLast(E,e);
空的话
抛异常 返回null
E getFirst(); vs E peekFirst(); //取而不删
E getLast(); vs E peekLast(); E peek();
E pollFirst(); //取并且删
E pollLast(); E poll();
void push(E e); E pop();
静态域可以通过方法,对象名(它属于每个对象),类名来访问,一般通过静态方法来访问静态域
1.泛型的知识
1.泛型分为泛型方法和泛型类
2.学会使用泛型类<>]
3.泛型是支持嵌套的
2.包装类
基本数据类型也变成引用数据类型
8个包装类/ int<=> Integer char<=>Character
1) 自动装箱 2)自动拆箱
int i; Integer ii=i; (Integer)i;
Integer ii; int i=ii;
重点:无论是泛型还是自动拆装箱,都是编译期间的工作,不涉及运行期间。