java 集合一 Collection


集合概念

概念:對象的容器,實現了對對象常用的操作,類似數組功能。

和數組的區別:

  1. 數組長度固定,集合長度不固定

  2. 數組可以存儲基本類型和引用類型,集合只能存儲引用類型

Collection體系

Collection為該體系的根接口,代表一組對象,成為“集合”;

  1. List接口的特點:有序,有下標、元素可重復

    • ArrayList

    • LinkedList

    • Vector

  2. Set接口的特點:無序,無下標、元素不可重復

    • HashSet

    • SortedSet

      • TreeSet

Collection的使用

  1. 添加與刪除元素

public class Demo1 {
   public static void main(String[] args) {
       //創建集合
       Collection collection = new ArrayList();

       //添加元素
       collection.add("蘋果");
       collection.add("西瓜");
       collection.add("榴蓮");
       System.out.println("元素個數:"+collection.size());
       System.out.println(collection);
       
//刪除元素
       collection.remove("榴蓮");
       System.out.println(collection);
       //清空
       collection.clear();
  }
}
  1. 遍歷元素與判斷

public class Demo1 {
   public static void main(String[] args) {
       //創建集合
       Collection collection = new ArrayList();

       //遍歷元素[重點]
       //1、使用增強for
       for (Object object : collection) {
           System.out.println(object);
      }
       //2、使用迭代器(專門用於遍歷集合的一種方式)
       //hasNext();有無下一個元素
       //next();獲取下一個元素
       //remove();刪除當前元素
       Iterator it = collection.iterator();
       while (it.hasNext()){
           String s = (String) it.next();
           System.out.println(s);
           //迭代過程中不能使用刪除方法刪除Collection中的元素(不能並發修改)
           //it.remove();//可以刪除迭代器對象的元素
      }

       //判斷
       System.out.println(collection.contains("西瓜"));
       System.out.println(collection.isEmpty());
  }
}
  1. 對於對象的操作

//學生類
public class Student {
   private String name;
   private int age;

   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 + ']';
  }
}

public class Demo1 {
   public static void main(String[] args) {
       //新建
       Collection collection = new ArrayList();
       Student s1 = new Student("張三",20);
       Student s2 = new Student("李四",18);
       Student s3 = new Student("王二",22);

       //添加學生
       collection.add(s1);
       collection.add(s2);
       collection.add(s3);
       System.out.println("元素個數:"+collection.size());
       System.out.println(collection.toString());

       //刪除
//       collection.remove(s1);
//       collection.clear();
//       System.out.println("刪除之后:"+collection.size());

       //遍歷
       for (Object o : collection) {
           Student s = (Student) o;
           System.out.println(s.toString());
      }
       System.out.println("------------");
       Iterator it = collection.iterator();
       while (it.hasNext()){
           Student s = (Student) it.next();
           System.out.println(s.toString());
      }

       //判斷
       System.out.println(collection.contains(s1));
       System.out.println(collection.isEmpty());

  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM