• JAVA自学笔记15


    JAVA自学笔记15

    @例题1:共有5个学生,请把五个学生的信息存储到数组中,并遍历数组,并获取每个学生的信息

    Students[] students=new Student[5];
    Student s1=new Student("Jack");
    Students[0]=s1;//数组里储存的是一个一个的对象

    1、集合类
    1)面向对象语言对事物都是以对象的形式,所以为了方便 对多个对象进行操作,java提供了集合类
    2)数组虽然也可以存储对象,但长度必须是固定的,集合的长度是可变的。数组中可以存储基本数据类型,集合只能存储对象
    3)集合类的特点:只用于存储对象,长度可变,可以存储不同类型的对象
    4)集合的继承体系图解:
    这里写图片描述

    2、接口Collection
    1)Collection层次结构中的根接口。Collection表示一组对象,它们也被称为collection的元素
    2)功能:
    ①添加:
    boolean add(Object obj)
    boolean addAll(Collection c)//添加一个集合的元素
    ②删除:
    void clear();//移除集合中所有元素
    boolean remove(Object o)//移除一个元素
    boolean removeAll(Collection c)//移除一个集合的元素
    ③判断:
    boolean contain(Object obj);//判断集合中是否包含指定的元素
    boolean containAll(Collection c);//判断集合中是否包含指定的集合元素
    boolean isEmpty();//判断集合的数据元素是否为空
    ④获取:
    Iteractor iterartor();//返回在此collection中的所有元素
    ⑤交集:
    boolean retainAll(collection c);两个集合都有的元素
    ⑥长度:
    int size();//元素的个数
    数组、集合均无length方法,字符串有length方法
    ⑦转换
    Object[] toArray();//返回包含此collection中所有元素的数组
    T[] toArray();//返回包含此collection中所有元素的数组,返回数组的运行时类型与指定数组的运行时类型相同

    //使用子类对象ArrayList
    Collection c=new ArrayList(); 
    System.out.println("C:"+c);//已重写,输出为[]
    
    System.out.println(c.add("hello"));//true,以下省略输出语句
    c.remove("hello")//true,若对象中没有需要删除的元素,则返回false
    c.retain("hello")//false
    c.isEmpty()//true
    c.size;//0
    
    Collection c1=new ArrayList(); 
    c1.add("abc1");
    c1.add("abc2");
    Collection c2=new ArrayList(); 
    c2.add("abc3");
    c2.add("abc4");
    c2.add("abc2");
    
    c1.addAll(c2);//true,[abc1,abc2,abc2,abc3,abc4]
    c1.remove(c2);//只要有一个元素被移除了,就返回true,134
    c1.containsAll(c2);//只有包含c2中的所有元素才返回true
    c1.retainAll(c2);//true,c1存储两者交集,c2不变。返回值表示的是c1是否发生过改变

    2)集合的遍历

    Collection c=new ArrayList(); 
    c1.add("abc1");
    c1.add("abc2");
    Object[] objs=c.toArray();//即可遍历
    for(int x=0;x<objs.length;x++){
    System.out.println(objs[x]);
    String s=(String) objs[x];//向下转型以调用String类下的方法
    System.out.println(s.length);//输出每个元素的长度
    }
    
    //迭代器遍历(集合的专用遍历方式)
    Collection c=new ArrayList(); 
    c1.add("abc1");
    c1.add("abc2");
    
    Iterator it=c.iterator();//将返回子类对象,多态
    while(it.hasNext()){//判断是否有下一个元素
    String s=(String)it.next()//返回abc1,获取元素并移动到下一个位置
    System.out.println(s);
    }
    

    //例题2:用集合存储5个学生对象,并利用迭代器进行遍历
    注意:自己的类名不要和API当中当前使用的类名相同

    Collection c=new ArrayList(); 
    
    //创建学生对象
    String s1=new Student("Jack","33");
    String s2=new Student("cici","23");
    c.add(s1);
    c.add(s2);
    Iterator it=c.iterator();//将返回子类对象,多态
    while(it.hasNext()){//判断是否有下一个元素
    String s=(String)it.next()//返回abc1,获取元素并移动到下一个位置
    System.out.println(s);//注意:每调用一次next就移动一次
    }

    3)迭代器:
    ①是遍历集合的一种方式,是依赖于集合而存在的
    图解:这里写图片描述

    这里写图片描述

    @例题List存储字符串并遍历

    //创建集合对:
    List list=new ArrayList();
    //创建字符串并添加
    c1.add("abc1");
    c1.add("abc2");
    
    Interator it=list.iterator();
    while(it.hasNext()){
    String s=(String)it.next();
    }

    2、List集合
    1)有序的collection序列。此接口的用户可以对列表中每一个元素的插入位置进行精确的控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。与set不同,列表通常允许重复的元素

    3、增强for
    1)for循环的一种,JDK新特性,用于简化数组和Collection集合的遍历,用于迭代器
    2)格式:
    for(元素数据类型 变量名:数组或者Collection集合){
    //使用变量即可,该变量就是元素
    }
    3)弊端:增强for的目标不能为空,需要先进行判断

    int[] arr={1,2,3,4,5};
    for(int x:arr)
    {System.out.println(x);}
    
    String[] StrArray={"dd","ee","ff "};
    for(String s:strArray){
    System.out.println(s);
    }
    
    ArrayList<String>array=new ArrayList<String>;
    array.add("HELLO");
    array.add("ww");
    array.add("ed");
    for(String s:array){
    System.out.println(s); 
    }
    //存储自定义对象并遍历
    //创建对象
    Student s("aa",33);
    Student s1("bb",3);
    Student s2("cc",55);
    
    //添加到集合中
    array.add(s);
    array.add(s1);
    array.add(s2);
    
    //迭代器
    Iterator<Student> it=array.iterator();
    while(it.hasNext()){
    Student s=it.next();
    
    }
    
    //普通for
    for(int x=0lx<array.size();x++){
    Student s=array.get(x);
    System.out,println(s.getName());
    }
    
    for(Student s:array){
    System.out,println(s.getName);
    }
    //LinkedList/Vector/Collection/List请类比

    3、静态导入
    1)格式:import static 包名…类名。方法名;

    import static java.lang.Math.abs;
    import static java.lang.Math.max;
    
    abs(-2);
    max(2,3);

    2)可以直接导入到方法的级别
    方法必须是静态的,若有多个同名的静态方法,必须加前缀。但一般没什么用处

    4可变参数
    1)定义方法时不知道要定义多少个参数,java提供了可变参数
    2)格式:
    修饰符 返回值类型 方法名(数据类型…变量名){}

    public static int sum(int...a){
    int s=0;
    for(int x:a){
    s+=x;
    }
    return s;
    }

    注意:这里的变量是一个数组,如果一个方法有多个可变参数,且有多个参数。那么,可变参数肯定是最后一个
    选中表达式-右键-refactor-extract method
    3)Arrays工具类中的一个方法:
    asList方法
    public staticListasList(T…a)//把数组转换为集合,虽然可以把数组转为集合,但数组的长度不能改变

    //字符串转换为数组
    String strArray={"d","dd","ddd"};
    
    List<String> list=Arrays.asList(strArray);
    for(String s:list){
    System.out.println(s);
    }
    
    List<String> list=Arrays.asList("d","dd","ddd");
    for(String s:list){
    System.out.println(s);d dd ddd
    }
    //

    @例题5:集合嵌套存储与遍历
    这里写图片描述

    
    //创造大集合
    ArrayList<<ArrayList<Student>>> bigArrayList=new ArrayList<<ArrayList<Student>>();
    
    //创建第一个集合
    ArrayList<Student>firstList=new ArrayList<Student>();
    Student s1=new Student("jenny",23);
    Student s2=new Student("jack",73);
    
    //添加到对象中
    firstList.add(s1);
    firstList.add(s2);
    
    //把第一个班级存到大集合中
    bigArrayList.add(firstList)
    //其余类比
    
    //遍历各个元素:
    for(ArrayList<Student>array):bigArrayList{
    for(Student s:array){
    System.out.println(s.getname)
    }
    }

    @例题7:
    获取10个1-20之间的随机数,要求不能重复

    //创建产生随机数的对象
    //创建应存储随机数的集合
    //定义一个统计变量从0开始
    //判断统计变量是否小于10。是则产生一个随机数,判断该随机数在集合中是否存在,若不存在就继续添加,如果存在则跳过。如果已满10个,则跳过
    //遍历集合
    
    Random r=new Random();
    
    ArrayList<integer>array=new ArrayList<Integer>();
    
    int count=0;
    whie(count<10){
    int number=r,nextInt(20)+1;
    if(array.contains(number)!){
    array.add(number);
    count++;
    
    for(Integer i:array){
    System,out,println(i);
    }
    }
    }

    @例题8:键盘录入多个数据输出最大值

    //创建键盘录入对象
    Scanner sc=new Scanner(Sytem.in);
    //键盘录入多个数据,以0未结束
    ArrayList<Integer>array=new ArrayList<Integer>();
    while(true){
    if(number!=0){
    array.add(number);
    }else break;
    }
    //把集合转为数组
    Integer[] i=new Inteher(array.size);
    Integer[] ii=array.toArray(i);
    
    //对数组排序
    Array,sort(i);
    
    //获取该数组中最大索引的值
    System.out.println(i[i.lenngth-1]);
  • 相关阅读:
    新思路,坚持创新;好想法,坚持执行
    新的一年,新的梦想
    新的一年,新的梦想
    大秦帝国-《治秦九论》
    大秦帝国-《治秦九论》
    大学生应当趁早谋划未来(二)--给表弟的建议
    大学生应当趁早谋划未来(二)--给表弟的建议
    解读OpenRTB(实时竞价)生态系统
    Java实现蓝桥杯VIP 算法训练 阶乘末尾
    Java实现 蓝桥杯VIP 算法训练 sign函数
  • 原文地址:https://www.cnblogs.com/Tanqurey/p/10485324.html
Copyright © 2020-2023  润新知