文章目录
1.数组和集合的区别
1.数组的大小是固定的,并且同一个数组只能是相同的数据类型
2.集合的大小是不固定的,在不知道会有多少数据的情况下可使用集合。
2.集合的三种类型:list(列表)、set(集)、map(映射)
List接口和Set接口属于Collection接口,Map接口和Collection接口并列存在(同级)。


2.1List(元素可重复性,有序性)
- ArrayList底层是一个数组,输出时需要foreach遍历,查询快,增删慢,线程安全,效率高。
- Vector底层是一个实现自动增长的对象数组,元素会自动添加到数组中,查询快,增删慢,线程安全,效率低。
- LinkedList底层是一个链表,查询慢,增删快,线程不安全,效率高。
public static void main(String[] args) {
List<Integer> arraylist=new ArrayList<>();
arraylist.add(3);
arraylist.add(22);
arraylist.add(31);
arraylist.add(345);
arraylist.add(63);
arraylist.add(3);
//ArrayList底层是一个数组,输出时需要foreach遍历,查询快,增删慢,线层安全,效率高
System.out.print("arraylist:");
for (Integer test : arraylist) {
System.out.print(test+" ");
}
System.out.println

本文介绍了Java集合框架中的数组与集合的区别,重点讲解了List、Set和Map三大类型的特性。List接口的实现如ArrayList、Vector和LinkedList各有优劣;Set接口的HashSet、LinkedHashSet和TreeSet确保元素唯一性;Map接口的HashMap、TreeMap及HashTable则关注键值对存储,强调key的唯一性。HashMap与HashTable主要区别在于线程安全性。

362

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



