数组
数组的特点
1、一旦初始化后,其长度就确定了。
2、一旦定义好数组后,其元素的类型也就确定了。我们就只能操作指定类型的数据了。—>比如:String[] arr ; int[] arr1;
数组的缺点
1、一旦建立,长度就不可更改
2、数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便。
3、获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用。
4、数组存储数据的特点:有序、可重复,对于无序、不可重复的需求,不能满足。
集合框架
| --- Collection接口:单列集合,用来存储一个一个的对象
| --- List接口:存储有序的、可重复的数据 --> "动态"数组
| --- ArrayList、LinkedList、Vector
| --- Set接口:存储无序的、不可重复的数据 --> 高中讲的"集合"
| --- HashSet、LinkHashSet、TreeSet
| --- Map接口:双列集合,用来存储一对(key - value)一对的数据 --> 高中函数 y = f(x) value = f(key)
| --- 、Linked、TreeMap、Hashtable、Properties
Collection接口
常见方法
//Collection接口类的创建
Collection collection = new ArrayList();
//add(object obj);
collection.add(obj);
//size();
collection.size();
//addAll(Collection addedcollection);
collection.addAll(collection1);
//clear()
collection.clear();
//isEmpty()
collection.isEmpty();
//contains(Object obj);
//判断集合中是否存在obj
collection.contains(obj);
//此方法判断自定义类需要重写equals()方法
//containsAll(Collection collection1);
collection.containsAll(collection1);
//remove();
//remove有按序号删除和按关键字删除
//按关键字删除
collection.remove(obj);
//按序号删除
collection.remove(new Integer(num));
//removeAll(collection collection1);
collection.removeAll(collection1);
//retainAll(collection collection1):修改collection类两集合的交集
collection.retainAll(collection1);
//equals(collection collection1);
boolean boo = collection.equals(collection1);
//hashCode();
collection.hashCode();
//toArray();
Object[] objects = collection.toArray();
//Arrays.asList();
List integers1 = Arrays.asList(new Object[]{});
//iterator();
Iterator iterator = collection.iterator();
//iterator的遍历
while(iterator.hasNext()){
System.out.println(iterator.next());
}
foreach循环
for(object obj:coll){
System.out.println(obj);
}
List接口
List接口概述:
ArrayList :主要实现类,线程不安全的,效率高,底层是应用Object[]存储
linkedList :底层使用双向链表存储,对于频繁的删除、插入操作效率高。
Vector : 古老实现类,线程安全,效率低,底层是应用Object[]存储
三者的异同:
相同点:三个类都实现了List接口,存储数据的特点相同:存储有序的、可重复的数据
不同点:ArrayList作为主要实现类、Vector是List接口的古老实现类、LinkedList
ArraysList底层分析
ArrayList arrayList = new ArrayList();
arrayList.add(123);
//第一次调用add()时,底层才创建了长度为10的数组,并将数据123添加到数组中
//当长度超过最大长度时,则扩容到1.5倍。
LinkedList底层分析
//LinkedList底层是由双向列表组成
//LinkedList内部声明了Node类型的指针,first和Last
Vector底层分析
//Vector底层有数组组成
//当容量不足时,会扩容到2倍
List常用方法
//创建
ArrayList list = new ArrayList();
//添加
list.add(obj);
//按位插入
list.add(index,obj);
//addAll(list);
List<Integer> list1 = Arrays.asList(1, 2, 3);
list.addAll(list1);
System.out.println(list);
//get();
Object obj = list.get(0);
//indexOf(obj):obj首次出现的位置
int index = list.indexOf(obj);
//lastIndexOf(obj);
int lastIndexOf = list.lastIndexOf(obj);
//删除
list.remove(index);
//修改
list.set(index,replaceElem);
//返回子串:左闭右开
List list2 = list.subList(index1,index2);
Set接口
Set接口
HashSet:作为Set接口的主要实现类,线程不安全的,可以存储null值
LinkedHashSet:作为HashSet的子类:遍历其内部数据时,可以按照添加的顺序遍历
TreeSet:可以按照添加对象的指定属性,进行排序。
HashSet底层add()方法的实现,初始化数组长度16
add()中先考虑hash值,经过散列函数后,获取索引值
当索引位置有元素时,比较hash值,如果hash值不同,则用链表的形式连接到这个位置的后面(链表法)
如果hash一样,再用equals方法比较
jdk7:新元素头插法
jdk8:新元素尾插法
HashSet和LinkedHashSet
//无序的、不可重复的
Set set = new HashSet();
//LinkedHashSet在存储上是无序的,但是在遍历时与插入顺序相同。
Set set = new LinkedHashSet();
TreeSet
//TreeSet可以使用定制排序
Comparator com = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof User && o2 instanceof User) {
User user1 = (User) o1;
User user2 = (User) o2;
return -user1.name.compareTo(user2.name);
} else {
throw new RuntimeException("输入错误");
}
}
};
Set set = new TreeSet(com);//定制排序
Map接口
HashMap
//会自动排序,可以存储null值
Map map = new HashMap();
//添加
map.put(key,value);
map.putAll(map2)
//删除
Object removeValue = map.remove(key);
//返回keySet数组
Set set = map.keySet();
//返回values数组
Collection values = map.values();
//返回entry数组
Set set = map.entrySet();
LinkedHashMap
//顺序存储,可以存储null值
LinkedHashMap map = new LinkedHashMap();
//添加
map.put(obj)
//get
object returnValue = map.get(index);
//replace
Object replace = map.replace(index,replaceNum);
TreeMap
//会排序,但不能存储null值
TreeMap map = new TreeMap();
//向TreeMap中添加key-value,要求key必须是由一个类创建的对象
//因为要按照key进行排序:自然排序 、定制排序
TreeMap map = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof User && o2 instanceof User) {
User u1 = (User) o1;
User u2 = (User) o2;
return Integer.compare(u1.getAge(), u2.getAge());
}
throw new RuntimeException("输入的类型不匹配!");
}
});
Hashtable
//会排序,但不能存储null值
Propertise table = new Properties();
Collection工具类
//reverse:反转数组
Collections.reverse(list);//将list反转
//shuffle:随机排序
Collections.shuffle(lsit);
//sort
Collections.sort(list);
//sort(list,Comparator):定制排序
Collections.sort(list,new Comparator(){.....});
Collection.min(list);
Collections.min(list,Comparator);
Collections.max(list);
Collections.min(list,Comparator);
//frequency(list,object);返回指定集合中指定元素出现的次数
System.out.println(Collections.frequency(list, obj));
//copy(dest,list):将list复制到dest中
List dest = Arrays.asList(new Object[list.size()]);
Collections.copy(dest,list);
System.out.println(dest);
//synchronizedXxxx()方法;返回的list1是一个线程安全的List
List list1 = Collections.synchronizedList(list);
System.out.println(list1);

被折叠的 条评论
为什么被折叠?



