您的位置:首页 > 其它

集合框架Collection集合的基本功能及ArrayList类的使用

2017-05-09 19:03 471 查看

集合框架(Collection集合的基本功能测试)

public interface Collection<E> extends Iterable<E> 
Collection 层次结构 中的根接口。
Collection 表示一组对象,这些对象也称为 collection 的元素。
一些 collection 允许有重复的元素,而另一些则不允许。
一些 collection 是有序的,而另一些则是无序的。
JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现。
从以下版本开始: 1.2 。1.5之前<E>没有泛型的时候是<object o>


boolean add(E e)
确保此 collection 包含指定的元素(可选操作)。
如果此 collection 由于调用而发生更改,则返回 true。
(如果此 collection 不允许有重复元素,并且已经包含了指定的元素,则返回 false。)


ArrayList类

每个 ArrayList 实例都有一个容量。
该容量是指用来存储列表元素的数组的大小。
它总是至少等于列表的大小。
随着向 ArrayList 中不断添加元素,其容量也自动增长。
并未指定增长策略的细节,因为这不只是添加元素会带来分摊固定时间开销那样简单。
List 接口的大小可变数组的实现。
实现了所有可选列表操作,并允许包括 null 在内的所有元素。
除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。
该容量是指用来存储列表元素的数组的大小。
它总是至少等于列表的大小。
随着向 ArrayList 中不断添加元素,其容量也自动增长。
并未指定增长策略的细节,因为这不只是添加元素会带来分摊固定时间开销那样简单。


import java.util.ArrayList;
import java.util.Collection;
import bean.Student;
@SuppressWarnings({ "rawtypes", "unchecked" })
//保持原始类型,不检查。标记,Ctrl 左键上移
public class c {
public static void main(String[] args) {
Collection c = new ArrayList();//父类引用指向子类对象,编译看左边,运行看右边
boolean b1 = c.add("abc");
boolean b2 = c.add(true);//自动装箱new Boolean(true);
boolean b3 = c.add(100);
boolean b4 = c.add(new Student("张三",23));
boolean b5 = c.add("abc");

System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
//看Arrylist()类的add()方法,无论如何都返回true,他可以添加重复元素
/*add方法如果是List集合一直都返回true,因为List集合中是可以存储重复元素的
如果是Set集合当存储重复元素的时候,就会返回false*/

System.out.println(c.toString());
//他或他的父类重写了toString方法

Collection co = new ArrayList();
co.add("a");
co.add("b");
co.add("c");
co.add("d");
System.out.println(co);//[a, b, c, d]
co.remove("b");//删除指定元素
System.out.println(co);//[a, c, d]

System.out.println(co.contains("a"));//判断是否包含true
System.out.println(co.size());//获取元素的个数
System.out.println(co.isEmpty());
co.clear();//清空集合
System.out.println(co);//[]

}

}

ArrayList集合的遍历

* 其实就是依次获取集合中的每一个元素。
案例
* 把集合转成数组,可以实现集合的遍历
* ArrayList类方法
* public Object[] toArray()
* 	按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。 


import java.util.ArrayList;
import java.util.Collection;
import bean.Student;

public class d {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
c.add("d");
Object[] arr = c.toArray();//将集合转换成数组
System.out.println(arr);//[Ljava.lang.Object;@1db9742
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}

Collection c1 = new ArrayList();
c1.add(new Student("张三", 23)); //Object obj = new Student("张三",23);
c1.add(new Student("李四", 24));
c1.add(new Student("王五", 25));
c1.add(new Student("赵六", 26));
Object[] arr1 = c1.toArray();//将集合转换成数组
System.out.println(arr1);//[Ljava.lang.Object;@106d69c
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
//System.out.println(arr1[i].getName());
Student s = (Student)arr1[i]; //向下转型
System.out.println(s.getName() + "..." + s.getAge());
}
}

}

ArrayList基本方法测试

import java.util.ArrayList;
import java.util.Collection;
public class e {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
Collection c1 = new ArrayList();
c1.add("a");
c1.add("b");
c1.add("c");
c1.add("d");

Collection c2 = new ArrayList();//alt + shift + r改名
c2.add("a");
c2.add("b");
c2.add("f");
c2.add("e");

//c1.addAll(c2);//将c2中的每一个元素添加到c1中,[a, b, c, d, a, b, f, e]
//c1.add(c2);//将c2看成一个对象添加到c1中,[a, b, c, d, [a, b, f, e]]
//System.out.println(c1);

/*boolean b = c1.removeAll(c2);//删除的是交集 ,没有交集就是false
System.out.println(b);//true
System.out.println(c1);//[c, d]*/

boolean b = c1.containsAll(c2); //判断调用的集合是否包含传入的集合
System.out.println(b);//false
Collection c3 = new ArrayList();
c3.add("a");
c3.add("b");
boolean b1 = c1.containsAll(c3);
System.out.println(b1);//true

//取交集,如果调用的集合改变就返回true,如果调用的集合不变就返回false
boolean b3 = c1.retainAll(c2);//取交集
System.out.println(b3);//true
System.out.println(c1);//[a, b]
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐