2021-07-29第十八天math,random,collection方法,Iterator迭代器和泛型

这篇博客介绍了Java编程中的一些高级特性,包括Random类用于生成伪随机数,BigDecimal处理高精度计算,Collection的高级功能如迭代器Iterator的使用,以及泛型的概念和优势,强调了泛型如何提高程序安全性。
摘要由CSDN通过智能技术生成

第十八天

DateUtil是针对Date日期对象与String进行转换的工具类.

public class DateUtils {

    //构造方法私有化
    private DateUtils(){}

    //提供两个静态功能
    //Date---->String

    /**
     * 这个方法是针对将日期对象转换成日期文本字符串
     * @param date     需要被格式化的日期对象
     * @param pattern   需要指定的模式
     * @return  返回的日期文本字符串  结果 yyyy-MM-dd
     */
    public static String date2String(Date date,String pattern){
        //分步走
        //创建SimpleDateFormat对象
       /* SimpleDateFormat sdf = new SimpleDateFormat(pattern) ;
        String result = sdf.format(date);
        return  result ;*/

       //一步走
        return new SimpleDateFormat(pattern).format(date) ;
    }


    //String--->Date: 解析过程: parse(String source) throws ParseException

    /**
     * 这个方法针对String日期文本字符串转换成日期对象
     * @param source   需要被解析的日期文本字符串
     * @param pattern   需要解析中使用的一种模式
     * @return  返回的就是日期对象Date
     */
    public static Date string2Date(String source,String pattern) throws ParseException {

        return new SimpleDateFormat(pattern).parse(source) ;
    }
}

Math是针对数学运算的工具类,天宫了很多方法

public static int abs(int  a):绝对值方法
public static double ceil(double a):向上取整
public static double floor(double a):向下取整
public static int max(int a,int b):获取最大值
public static int min(int a,int b):获取最小值
public static double pow(double a,double b):a的b次幂
public static double random():[0.0,1.0):随机数
public static long round(double a):四舍五入
public static double sqrt(double a):开平方根

Random伪随机数生成器

构造方法
        public Random():    产生一个随机生成器对象,通过成员方法随机数每次没不一样的(推荐)
        public Random(long seed) :参数为long类型的值(随机数流:种子),每次通过成员方法获取随机数产生的随机数相同的
获取随机数的成员方法
        public int nextInt():获取的值的范围是int类型的取值范围(-231次方到231次方-1)
        public int nextInt(int n):获取的0-n之间的数据 (不包含n)
        产生随机数:
                Math类的random方法
                                    public static double random();
               Random:也能够去使用
                            无参构造方法  + 成员方法
                            public Random():+ public int nextInt(int n)

BigDecimal

小数要进行精确计算-还可以计算的同时保留小数点后的有效位数
Java提供的类: BigDecimal

构造方法
public BigDecimal(String value):数字字符串

成员方法:
     public BigDecimal add(BigDecimal augend)public BigDecimal subtract(BigDecimal subtrahend)public BigDecimal multiply(BigDecimal multiplicand)public BigDecimal divide(BigDecimal divisor):public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
         参数1:商
         参数2:小数点后保留的有效位数
         参数3:舍入模式 :四舍五入

Collection的高级功能

boolean addAll(Collection c):添加一个集合中的所有元素

boolean containsAll(Collection c):包含一个集合中的所有元素

boolean removeAll(Collection c):删除集合中的所有元素, (删除一个算删除,还是删除所有)

boolean retainAll(Collection c):A集合对B集合求交集, boolean的返回值是什么意思,交集的元素是保存在A中还是B//Collection最基本的遍历功能,不属于集合的专有遍历
Object[] toArray():将集合转换成了对象数组

Iterator

Iterator是一个Collection的迭代器,它也是集合专有的遍历方式.因为它是一个接口,所以必须创建一个子类去间接实现.

Iterator x = y.iterator();//通过y集合的ArrayList子类对象去获取其对象的迭代器.

public class CollectionTest {
    public static void main(String[] args) {

        //创建集合对象
        Collection c = new ArrayList() ; //List接口的子实现类 (重复元素)

        //添加元素
        c.add("hello") ;
        c.add("world") ;
        c.add("javaee") ;
       // System.out.println(c);

        //获取Collection的迭代器Iterator iterator()
        Iterator it = c.iterator();
        //第一次获取
        //Object next():  获取下一个可以遍历的元素
        /*if(it.hasNext()){
            Object obj = it.next();
            System.out.println(obj);
        }


        //第二次获取
        if(it.hasNext()){
            System.out.println(it.next());
        }


        //第三次获取
        if(it.hasNext()){
            System.out.println(it.next());
        }


        //第四次获取
      //  System.out.println(it.next());//此时没有元素了,要是用迭代器去获取元素,就会出现这个问题!
        //加入判断
        if(it.hasNext()){
            System.out.println(it.next());
        }*/

        //如果现在明确存储了3个元素,以后这些数据可能数据库获取的一个列表集合数据,一般while循环
        while(it.hasNext()){//判断迭代器中有下一个元素
            //才获取
            Object obj = it.next();// Object obj = new String("hello") ...
            //  System.out.println(obj+"----"+obj.length());
            //获取的元素同时,还要获取当前存储元素的长度  ---->String类型   length()
            String str = (String) obj;//向下转型
            System.out.println(str+"----"+str.length());
        }

    }
}

泛型

泛型的格式:<引用数据类>.泛型将运行时期的异常提前到了编译时期,避免了强制类型转换,从而提高了程序的安全性.

public class GenericDemo {
    public static void main(String[] args) {

        //创建Collection集合对象
        Collection<String> c = new ArrayList<String>() ; //new XXX<数据类型>: jdk7以后泛型推断


        c.add("hello") ;
        c.add("高圆圆") ;
        c.add("你好吗") ;
       // c.add(100) ;

        //获取迭代器Iteratr<E>是集合中存储的泛型是一致的
        Iterator<String> it = c.iterator();
        while(it.hasNext()){
            //获取String字符串的同时,还要获取长度
            String str = it.next();
            System.out.println(str+"---"+str.length());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值