黑马程序员----Java基础-----集合框架(一)

来源:互联网 发布:wind资讯终端 淘宝 编辑:程序博客网 时间:2024/04/29 09:12
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

                      集合框架(一)

一. 对象数组

 

我有3个学生,请把这个3个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。

 学生:Student
 成员变量:name,age
 构造方法:无参,带参
 成员方法:getXxx()/setXxx()

 

代码实现:

 

package cn.itcast_day0804;import cn.itcast_day0803.Student;public class objectarray {public static void main(String[] args) {Student[] arr = new Student[3];Student s1 = new Student("Peter",18);Student s2 = new Student("Bob",18);Student s3 = new Student("Allen",18);arr[0] = s1;arr[1] = s2;arr[2] = s3;for (int x = 0; x < arr.length; x++){//Student s = arr[x];System.out.println(arr[x].getName()+"-------"+arr[x].getAge());}}}

 

二. Collection集合

1. 集合

面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象进行操作,便有了集合

2.数组和集合的区别
      (1): 长度区别:
            数组的长度是固定的而集合的长度是可变的
        (2): 存储数据类型的区别:
            数组可以存储基本数据类型 也可以存储引用数据类型而集合只能存储引用数据类型
        (3): 内容区别:
            数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素

 

3.Collection集合的功能

 

a:添加功能
            boolean add(Object obj):添加一个元素
            boolean addAll(Collection c):添加一个集合的元素

b:删除功能
            void clear():移除所有元素
            boolean remove(Object o):移除一个元素
            boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)

c:判断功能
            boolean contains(Object o):判断集合中是否包含指定的元素    
            boolean containsAll(Collection c):判断集合中是否包含指定的集合元素
            boolean isEmpty():判断集合是否为空
d:获取功能
              Iterator<E> iterator()
e:长度功能
            int size():元素的个数

f:交集功能
            boolean retainAll(Collection c):两个集合都有的元素

 g:把集合转换为数组
            Object[] toArray()

 

代码实现 

 

package cn.itcast_day0804;import java.util.ArrayList;import java.util.Collection;public class Test2 {public static void main(String[] args) {Collection c1= new ArrayList();Collection c2 = new ArrayList();String s1 = "hello";String s2 = "Java";String s3 = "World";String s4 = "itcast";//boolean add(Object obj):添加一个元素c1.add(s1);c1.add(s2);c1.add(s3);c2.add(s1);c2.add(s3);c2.add(s4);System.out.println(c1);            System.out.println(c1.contains(s3));System.out.println(c1.containsAll(c2));System.out.println(c1.size());System.out.println(c1.retainAll(c2));System.out.println(c1.isEmpty());System.out.println(c1.remove(s2));System.out.println(c1.removeAll(c2));System.out.println(c1);}}

} 打印结果:

[hello, Java, World]

3

true

true

false

false

false

true

[]

 

4. 集合的遍历之集合转数组遍历

 

利用Collection中的Object toArray()方法将集合转为数组进行遍历

 

代码实现:

 

package cn.itcast_day0804;import java.util.ArrayList;import java.util.Collection;import cn.itcast_day0803.Student;public class Test3 {public static void main(String[] args) {Collection c = new ArrayList();Student s1 = new Student("Peter",18);Student s2 = new Student("Allen",18);Student s3 = new Student("Bob",18);c.add(s1);c.add(s2);c.add(s3);Object[] obj = c.toArray();for(int x = 0 ; x < obj.length ; x++){Student s = (Student)obj[x];System.out.println(s.getName()+"---"+s.getAge());}}}

5. Collection存储自定义对象并用迭代器遍历

 

代码实现:

 

/*

 * Collection存储自定义对象并用迭代器遍历

 * */

package cn.itcast.day0805;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import cn.itcast_day0803.Student;public class Test1 {public static void main(String[] args) {Collection c = new ArrayList();Student s1 = new Student("Allen",18);Student s2 = new Student("Peter",18);Student s3 = new Student("Bob",18);c.add(s1);c.add(s2);c.add(s3);Iterator it = c.iterator();while(it.hasNext()){Student s = (Student)it.next();System.out.println(s.getName()+"--------"+s.getAge());}}}

 

6. Collection集合存储字符串并用迭代器遍历

 

代码实现

package cn.itcast.day0805;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Tset2 {public static void main(String[] args) {Collection c = new ArrayList();c.add("西施");c.add("貂蝉");c.add("昭君");Iterator it  = c.iterator();while(it.hasNext()){System.out.println(it.next());}}}

三. List

 

1.List 集合的特点:     元素有序,并且每一个元素都存在一个索引.元素可以重复.

 

2.List集合的特有功能概述
   void add(int index,E element):        在指定索引处添加元素
   E remove(int index):                移除指定索引处的元素
   E get(int index):                    获取指定索引处的元素
   E set(int index,E element):            更改指定索引处的元素

 

代码实现

package cn.itcast.day0805;import java.util.ArrayList;import java.util.List;public class Test3 {public static void main(String[] args) {List list = new ArrayList();list.add("hello") ;list.add("world") ;list.add("java") ;System.out.println(list);list.add(0, "itcast");System.out.println(list.remove(3));System.out.println(list.get(1));System.out.println(list.set(1,"blackhorse"));System.out.println(list);}}

打印结果: [hello, world, java]

java

hello

hello

[itcast, blackhorse, world]

 

3. List集合存储学生对象并遍历。通过迭代器  size()get()方法 结合使用遍历

 

代码实现:

 

package cn.itcast.day0805;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import cn.itcast_day0803.Student;public class Test4 {public static void main(String[] args) {List list = new ArrayList();Student s1 = new Student("Allen",18);Student s2 = new Student("Peter",18);Student s3 = new Student("Bob",18);list.add(s1);list.add(s2);list.add(s3);Iterator it = list.iterator();while(it.hasNext()){Student s = (Student)it.next();System.out.println(s.getName()+"----"+s.getAge());}System.out.println("---------");for(int x = 0 ; x < list.size() ; x++){Student st = (Student)list.get(x);System.out.println(st.getName()+"----"+st.getAge());}}}

 

4. List的三个子类的特点
        ArrayList:
            底层数据结构是数组,查询快,增删慢。
            线程不安全,效率高。
        Vector:
            底层数据结构是数组,查询快,增删慢。
            线程安全,效率低。
        LinkedList:
            底层数据结构是链表,查询慢,增删快。
            线程不安全,效率高。

 

四 LinkedlistVector

1. Vector的特有功能

 

Vector 类可以实现可增长的对象数组 , Vector 是同步的

        public void addElement(E obj)
        public E elementAt(int index)
        public Enumeration elements()(一种特殊的遍历方式,和迭代器差不多)

 

代码实现

package cn.itcast_day080502;import java.util.Enumeration;import java.util.Vector;import cn.itcast_day0803.Student;public class Test1 {public static void main(String[] args) {Vector vec= new Vector();Student s1 = new Student("Peter",18);Student s2 = new Student("Allen",18);Student s3 = new Student("Bob",18);vec.addElement(s1);vec.addElement(s2);vec.addElement(s3);vec.addElement(vec.elementAt(0));Enumeration en = vec.elements();while(en.hasMoreElements()){Student s = (Student)en.nextElement();System.out.println(s.getName()+"----"+s.getAge());}}}

打印结果 Peter----18

Allen----18

Bob----18

Peter----18

 

2. Linkedlist 的特有功能

 

LinkedList类概述:  List 接口的链接列表实现 此实现不是同步的
LinkedList类特有功能
        public void addFirst(E e)addLast(E e)
        public E getFirst()getLast()
        public E removeFirst()public E removeLast()

 

代码实现

package cn.itcast_day080502;import java.util.LinkedList;public class Test2 {public static void main(String[] args) {LinkedList ll = new LinkedList();ll.add("hello");ll.add("world");ll.add("java");ll.addFirst("Hi");ll.addLast("Oh");System.out.println(ll.getFirst()+"-----"+ll.getLast());ll.removeFirst();ll.removeLast();System.out.println(ll);}}

五 去除ArrayList 中重复自定义对象或者重复字符串

 

1. ArrayList去除集合中字符串的重复值(字符串的内容相同)

思路(建立一个新的集合,把每一个元素装进去,有重复的就不装,没有就装)

 

代码实现

2. ArrayList 取出自定义对象中的重复对象,注意Student类要重写equals()方法

 

package cn.itcast_day080503;import java.util.ArrayList;import cn.itcast_day0803.Student;public class Test1 {public static void main(String[] args) {ArrayList oldList = new ArrayList();Student s1 = new Student("Peter",18);Student s2 = new Student("Allen",18);Student s3 = new Student("Bob",18);Student s4 = new Student("Bob",18);Student s5 = new Student("Bob",18);Student s6 = new Student("Allen",18);oldList.add(s1);oldList.add(s2);oldList.add(s3);oldList.add(s4);oldList.add(s5);oldList.add(s6);ArrayList newList = new ArrayList();for(int x = 0 ; x < oldList.size() ; x++){Student s = (Student)oldList.get(x);if( ! newList.contains(s)){newList.add(s);System.out.println(s.getName()+"----"+s.getAge());//Student 类得重写Object 中的equals()方法,不然打印的是比较地址值}}}}

打印结果

Peter----18

Allen----18

Bob----18

 

 

 

 

id培训、iOS培训、.Net培训</a>、期待与您交流! -------
0 0