欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实训篇:第十四天

程序员文章站 2022-03-16 21:43:54
...

6.数学相关类

Math:java.lang.Math

Math.PI

Math.E

static double abs(double a):返回a的绝对值

随机数的生成(假随机):

static double random()方法:返回0.0 <= x < 1.0的x,一般希望生成特定范围内的一个随机数

比如:

//生成1~10的随机数
int num1 = (int)((Math.random() * 10) + 1);
//在上面的基础上生成1~4的随机数
int num2 = num1 % 4 + 1;
//或
int num3 = (int)((Math.random() * 4) + 1);

Random(假随机):java.util.Random,专门用来生成随机数的

Random rd = new Random();

int no = rd.nextInt(10);//生成0~9的随机数
int no1 = rd.nextInt(10) + 1;//生成1-10的随机数

7.集合框架

产生原因:

数组的缺陷:长度固定,修改长度需要new一个新的数组对象

一开始就分配了的空间可能会被浪费和消耗内存

->动态改变长度的容器->集合->集合框架

Collection ---接口(只能被类实现或被其他接口继承)

       |---List---接口

                 |---ArrayList---基于数组,常用于查询

                 |---Vector---基于数组

                 |---LinkedList---基于链表,常用于插入删除

       |---Set---接口---元素不能重复

                 |---HashSet

       |---Map---接口---key-value键值对   属性名=属性值

                 |---HashMap

ArrayList:

元素有序、且可重复的集合,集合中的每个元素都有其对应位置

创建:

ArrayList() ---初始化长度为10,不够后再以某个倍数扩充长度

package com.hpe.test1;

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

public class Test1 {
    public static void main(String[] args) {
//        ArrayList list = new ArrayList();//可以存储任意类型
        ArrayList<String> list = new ArrayList<String>();//<>是泛型,约束存储的类型

        //添加
        list.add("hello");
        list.add("world");//返回true添加成功,返回false添加失败
        System.out.println(list);

        //添加到特殊位置
        list.add(0, "good");//上面方法的重载方法
        System.out.println(list);

        //返回list元素的个数
        System.out.println(list.size());

        //添加集合,默认添加到末尾
        ArrayList<String> list1 = new ArrayList<String>();
        list1.add("1111");
        list1.add("2222");
        list1.add("3333");
        list.addAll(list1);
        System.out.println(list);

        //清空
        list.clear();
        System.out.println(list);

        list.add("hello");
        list.add("world");
        list.add("ok");
        list.add("good");

        //获取特定位置的元素,get(int index)方法
        System.out.println(list.get(0));
        System.out.println(list.get(list.size() - 1));
        System.out.println(list);

        System.out.println("----------------------------------");

        //遍历ArrayList
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        for (String item: list) {
            System.out.println(item);
        }

        //通过迭代器遍历
        //获取迭代器 java.util.Iterator
        //hasNext()判断还有没有元素可以迭代
        //next()获取被迭代到的元素
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String str = it.next();
            System.out.println(str);
        }
        System.out.println("-------------------------------------------");
        //判断集合中是否包含某个元素
        System.out.println(list.contains("hello"));//包含返回true
        System.out.println(list.contains("abcd"));//不包含返回false
        //contains(Object o)的判断逻辑:  (o==null ? e==null : o.equals(e))
        //传的是Obect或Object的子类,调用的equals()方法是调用的子类的equals()方法,所以传Object才判断地址是否相等,
        //传其子类就是调用子类equals(),比如String就是判断值是否相等

        ArrayList<Person> persons = new ArrayList<Person>();
        Person p1 = new Person("Tom", 20);
        Person p2 = new Person("Bob", 21);
        Person p3 = new Person("John", 22);

        persons.add(p1);
        persons.add(p2);
        persons.add(p3);

        Person p4 = new Person("Tom", 20);
        System.out.println(persons.contains(p4));//结果是false,因为contains调用的是equals方法,但Person没有重写Object的equals方法,所以相当于调用了Object的equals方法,即判断地址是否相等
        //现在要求Person的name和age相等即相等,则需要重写Person的contains方法
        //重写以后上一行的输出是true

        System.out.println("----------------------------------------------");
        //删除
        System.out.println(list);
        list.remove(0);//参数可以是int index;Object o
        list.remove("hello");//也是要比较,和contains的比较方式是一样的
        System.out.println(list);
        persons.remove(p4);//重写Person的equals,则该句会把p4删除
        System.out.println(persons);

        System.out.println("------------------------------------");
        //获取索引  int indexOf(Object o),不存在返回-1
        System.out.println(list.indexOf("hello"));
        
        
    }


}

 

相关标签: 实训 实训