Java教程

java学习之集合(一)

本文主要是介绍java学习之集合(一),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一:集合的概念

  • 对象的容器,定义了对多个对象进行操作的常用方法。可是实现数组的功能。
  • 和数组的区别:
  1. 数组长度固定,集合长度不固定;
  2. 数组能存储基本数据类型和引用数据类型,集合只能存储引用数据类型
  • 位置:java.util.*;

二:Collection体系集合

1.体系结构

在这里插入图片描述

  • Collection接口:是该体系结构的根接口,代表一组对象啊,称为集合
  • List接口的特点:有序,有下标,元素可重复;
  • Set接口的特点:无序,无下标,元素不能重复;

2.Collection父接口

  • 特点:代表一组任意类型的对象,无序,无下标,不能重复。
  • 方法
  1. boolean add(object obj):添加一个对象;
  2. boolean addAll(Collection c):将一个集合中的所有对象添加到此集合中;
  3. void clear():清空此集合中的所有对象;
  4. boolean contains(object obj):检查此集合中是否包含obj对象。
  5. boolean equals(Object obj):比较此集合是否与指定对象相等。
  6. boolean isEmpty():判断此集合是否为空。
  7. boolean remove(Object obj):在此集合中移除obj对象

3.Collection 的使用
案例1

package hai.bok.jh.coll;

import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput;

import java.util.*;

public class Text01 {
    /**
     * Collection 集合的使用
     * 1)添加元素
     * 2)删除元素
     * 3)遍历元素
     * 4)判断
     */
    public static void main(String[] args) {
        //创建集合
        Collection collection=new ArrayList();
//         * 1)添加元素
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("榴莲");
        System.out.println("元素个数:"+collection.size());
        System.out.println(collection);
//         * 2)删除元素
/*        collection.remove("榴莲");
        collection.clear();
        System.out.println("删除之后 :"+collection.size());*/

        System.out.println("====================");
//         * 3)遍历元素(重点)
        //3.1使用增强for
        for (Object object:collection) {
            System.out.println(object);
        }
        //3.2使用迭代器(迭代器专门用来遍历集合的一种方式)
        //hasNext()有没有下一个元素
        //next()下一个元素
        //remove;删除当前元素
        System.out.println("=========================");

        Iterator it=collection.iterator();
        while(it.hasNext()){
            String s=(String) it.next();
            System.out.println(s);
//            it.remove();
        }
        System.out.println("元素个数 = " + collection.size());
//         * 4)判断
        System.out.println(collection.contains("西瓜"));
        System.out.println("是否为空:   " + collection.isEmpty() );
    }
}

案例2

package hai.bok.jh.coll;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

package hai.bok.jh.coll;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Test02 {
    /**
     * Collection的使用:保存学生信息
     *
     */
    public static void main(String[] args) {
        //新建Collection对象
        Collection collection=new ArrayList();
        Student s1=new Student("张三",20);
        Student s2=new Student("张无忌",18);
        Student s3=new Student("王二",18);
        Student s4=new Student("王二",28);
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        collection.add(s4);
        System.out.println("元素个数:" + collection.size());
        System.out.println("collection = " + collection.toString());
        collection.remove(s4);
        collection.remove(s2);
        System.out.println("collection.toString() = " + collection.toString());
//        collection.clear();
        System.out.println("collection.size() = " + collection.size());
        System.out.println("===============增强for============");
        for (Object object:collection) {
                Student s=(Student)object;
            System.out.println(collection.toString());
        }
        System.out.println("============迭代器================");
        Iterator it=collection.iterator();
        while (it.hasNext()){
            Student s=(Student)it.next();
            System.out.println(s.toString() );
        }
        //4判断
        System.out.println("" + collection.contains((s1)));
        //通过new y一个对象比对,地址不一样,还是false,因为可以有重复值。
        System.out.println(collection.contains(new Student("张三",20)));

        //判断是否为空
        System.out.println("=============判断是否为空=============");
        System.out.println(collection.isEmpty());
    }
}

这篇关于java学习之集合(一)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!