首页 > 其他分享 >集合

集合

时间:2023-03-15 10:22:05浏览次数:25  
标签:name System collection println 集合 public out

集合

概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能。

和数组的区别:

  1. 数组长度固定,集合长度不固定
  2. 数组可以存储基本类型和引用类型,集合只能存储引用类型。

位置:java.util.*

特点:代表一组任意类型的对象,无序,无下标,不能重复。

collection方法

add();

remove();

Iterator();-----> hasNext(); next(); remove();

contains();

package com.zhang.oop.Coll;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        //增加元素
        collection.add("zy");
        collection.add("ch");
        collection.add("rt");
        System.out.println(collection);
        //删除元素
        collection.remove("ch");
        System.out.println(collection);
        //遍历元素
        for (Object o : collection) {
            System.out.println(o);
        }
        //用迭代器遍历元素
        Iterator it = collection.iterator();
        while (it.hasNext()){   //hasNext();  有没有下一个元素
            Object object = it.next();  //next(); 获取下一个元素
            System.out.println(object);   
            //it.remove();    不能使用collection的删除,会报异常
        }
        System.out.println(collection.size());
        //判断是否存在这个元素
        System.out.println(collection.contains("zy"));
    }
}

collection 操作学生信息

package com.zhang.oop.Coll;
public class Students {
    private String name;
    private  int age;
    public Students(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;
    }
}
package com.zhang.oop.Coll;
import com.zhang.oop.Obct.Student;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Application {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        Student s1 = new Student("张三",22);
        Student s2 = new Student("李四",33);
        Student s3 = new Student("王五",43);
        //增加信息
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println(collection);
        //删除信息
        collection.remove(s1);
        System.out.println(collection);
        //遍历信息
        for (Object o : collection) {
            System.out.println(o);
        }
        //用迭代器遍历信息
        Iterator it = collection.iterator();
        while (it.hasNext()){
            Object object = it.next();
            System.out.println(object);
        }
        //判断是否存在这个信息
        System.out.println(collection.contains(s2));
        //判断是否为空
        System.out.println(collection.isEmpty());
    }
}

标签:name,System,collection,println,集合,public,out
From: https://www.cnblogs.com/rockz/p/17217548.html

相关文章

  • 集合的遍历
    List的遍历ArrayList<String>list=newArrayList<>();===1forEach循环遍历=== list.forEach(newConsumer<String>(){@Overridepublicvoida......
  • MongoDb集合改名
    publicbooleanmongoDbCollectionNameUpdate(StringoldName,StringnewName){/连接MongoDB数据库/MongoClientmongoClient=MongoClients.create(mongourl);MongoDa......
  • 【Python】数据结构:集合
    1.集合Python中的集合与数学上的集合是一致的,不允许有重复元素,而且可以进行交集、并集、差集等运算。2.创建集合#字面量方式set1={1,2,3,3,3,2}print(set1)......
  • 金蝶AAS-V10安装过程问题集合
    启动指令\ApusicAS\aas\bin目录下./startserv启动(前台启动)\ApusicAS\aas\bin目录下./asadminstart-domain(后台启动)端口设置1、路径/ApusicAS/aas/domains/m......
  • Java三大集合类 - List
    ListSetMap一、List几个小问题:1、接口可以被继承吗?(可以)2、接口可以被多个类实现吗?(可以)3、以下两种写法有什么区别?//Listlist1=newList();是错误的因为List()是......
  • Python列表、元组、字典和集合的用法
    1.列表标志符号是[],元素可以修改、删除和新增1.1提取元素(索引从0开始计算)testList=['A','B','C',1,'D']print(testList[1])#打印索引区间[1,4)print(testList[0:3]......
  • 集合
    集合1.Java集合框架概述1.集合框架与数组的对比及概述/***一、集合的框架**1.集合、数组都是对多个数据进行存储操作的结构,简称Java容器。*说明;此时的存......
  • Java基础知识点(集合、ArrayList集合、基本数据类型对应的包装类及
    1.为什么要有集合?集合它可以自动扩容。2.集合存储数据类型的特点:不能直接存基本数据类型,需要将其变为包装类再存入,可以存引用数据类型。二:集合和数组的对比长度:数组的长度固......
  • 007 springboot集合mybatis-plus,使用其中的代码生成器
    代码生成器步骤一:在pom.xml中添加相应的依赖<!--代码生成器--><dependency><groupId>com.baomidou</groupId><artifactId>......
  • Android 集合数据在Sharedpreferences中的增删改查
    Android集合数据在Sharedpreferences中的增删改查Sharedpreferences作为一个轻量化的Android本地存储方式相信很多人都为其不能存集合而烦恼所以呢,我封了两个简易的方法希......