ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java 集合一

2021-12-05 21:34:03  阅读:126  来源: 互联网

标签:java System collection println Student 集合 public out


集合概念

概念:对象的容器,实现了对对象常用的操作,类似数组功能。

和数组的区别:

  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());

  }
}

 

标签:java,System,collection,println,Student,集合,public,out
来源: https://www.cnblogs.com/SummeRr/p/15647211.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有