目 录:
数组
集合类
Collection
List
ArrayList & LinkedList
Set
数组
定义
int a[] = {1,2,3};
int a[] = new int[]{1,2,3};
int a[][];
a = new int[2][];
a[1] = new int[2];
a[0] = new int[3];
方法 | 说明 |
---|---|
fill(arr, data); | 填充 |
fill(arr, from, to, data); | 填充 |
arr2 = Arrays.copyOf(arr, length); | 拷贝。import java.util.Arrays |
arr2 = Arrays.copyOfRange(arr, from, to); | 拷贝。import java.util.Arrays |
Arrays.binarySearch(arr, from, to, value); | 查询。import java.util.Arrays |
Arrays.sort(arr) | 排序。import java.util.Arrays |
集合类
Collection
方法 | 说明 |
---|---|
add(E, e) | 将制定对象添加到集合中 |
remove(Object o) | 将指定对象从该集合中删除 |
isEmpty() | 返回boolean,判断当前集合是否非空 |
iterator() | 返回该Collection的迭代器 |
size() | 返回int,元素个数 |
List
方法 | 说明 |
---|---|
get(int index) | 获取指定位置元素 |
set(int index, Object o) | 修改指定位置元素 |
ArrayList & LinkedList
方法 | return | 说明 |
---|---|---|
add(E e) | boolean | 添加e |
add(int index, E e) | void | 插入e 到index |
addAll(Collection<? extends E> c) | boolean | 添加集合c |
clear() | 清空 | |
remove(int index) | list[index] | 移除index 处值 |
removeAll(Collection<?> c) | boolean | 移除集合c |
set(int index, E e) | e0 | 修改index 处值为e ,返回修改前值e0 |
get(int index) | e | 获取index 处值 |
size() | Long | 返回元素个数 |
isEmpty() | boolean | 判断是否为空 |
contains(Object o) | boolean | 判断是否包含o |
containAll(Collection<?> c) | boolean | 判断是否包含集合c |
toArray() | Object[] | 转换为数组 |
iterator() | Iterator | 返回可迭代对象 |
Set
- TreeSet 方法
方法 | 说明 |
---|---|
first() | 返回此Set当前第一个值 |
last() | 返回此Set当前最后一个值 |
comparator() | 返回此Set的比较器。如自然顺序,return null |
headSet(E end) | 返回一新集合。包含此Set E 之前的元素 |
subSet(E from, E to) | 返回一新集合。包含此Set元素 :[from , to ) |
tailSet(E from) | 返回一新集合。包含此Set from 及之后元素 |
TreeSet
类必须实现Comparable
接口compareTo(Object o)
方法
Map
方法 | 说明 |
---|---|
put(K key, V value) | 向集合中添加指定键值对 |
get(Object key) | 返回key 对应的值,没有则返回null |
keySet() | 返回所有key 对象的Set 集合 |
values() | 返回所有value 对象的Collection 集合 |
contiansKey(Object key) | 判断是否包含指定key 的映射关系 |
contiansValue(Object value) | 判断是否有key 映射到指定value |
- HashMap 类实现的Map集合添加和删除映射关系效率更高。
- 可通过HashMap类创建Map集合,需要顺序输出时,再创建一个相同映射关系的TreeMap
Map<String, String> map = new HashMap<>();
...
TreeMap<String, String> treemap = new TreeMap<>();
treemap.putAll(map);
Iterator<String> keys = treemap.keySet().Iterator();
...