* 面试题:
* 数组和集合的区别?
* 1)长度的区别:
* 数组:长度是固定的
* 集合:长度是可变的
* 2)存储类型的区别
* 数组:可以存储基本类型、也可以存储引用类型
* 集合:只能存储引用类型
* 3)存储元素的区别
* 数组:存储元素类型保持一致
* 集合:可以存储不同引用类型的元素
*
* Collection:一些 collection 允许有重复的元素,而另一些则不允许。
* 一些 collection 是有序的,
* 而另一些则是无序的。JDK 不提供此接口的任何直接
* 实现:它提供更具体的子接口(如 Set 和 List)实现
*
*
* Collection的基本功能:
* boolean add(Object e):添加任意的引用类型元素
* boolean remove(Object o):删除指定元素
* void clear():删除集合中的所有元素
*
* boolean contains(Object o):判断是否包含指定的元素
* boolean isEmpty():判断集合是否为空
*
* int size():获取集合中的元素数量
* Object[] toArray():将集合进行遍历-->将集合转换成数组遍历(传统方式)
* Iterator<Object> iterator():获取Collection集合的迭代器(集合的专有遍历方式)
@SuppressWarnings("all")
public class CollectionDemo {
public static void main(String[] args) {
Collection c=new ArrayList();
System.out.println(c);//集合重写了Object类中toString()方法
// boolean add(Object e):添加任意的引用类型元素
c.add("hello");
c.add("dsa");
//
System.out.println(c);
System.out.println("-------------------");
// boolean remove(Object o):删除指定元素
// System.out.println(c.remove("dsa"));
// System.out.println(c);
// System.out.println("-------------------");
// void clear():删除集合中的所有元素
c.clear();
System.out.println(c);
System.out.println("-------------------");
// boolean contains(Object o):判断是否包含指定的元素
System.out.println(c.contains("hello"));
System.out.println(c);
System.out.println("------------------");
System.out.println(c.isEmpty());
System.out.println("------------------");
//集合中获取长度:size()
//数组中获取长度:length属性
//字符串中获取长度:length()方法
System.out.println(c.size());
}
}