java基础学习笔记 第10天

第1章 数组冒泡排序和Arrays工具类

1.1 数组的冒泡排序

冒泡排序:就是相邻的两个元素进行两两比较,把元素值大的元素依次向后排.

1.1.1 数组排序之冒泡排序原理图解

在这里插入图片描述

1.1.2 数组排序之冒泡排序代码实现

1.1.2.1 案例代码一:
package day10;
/*冒泡排序*/
public class BubbleSort {
    public static void main(String[] args) {
        //定义一个数组
        int[] arr = {24, 69, 80, 57, 13};
        /*
         //第一次比较
        //arr.length-1 是为了防止索引越界
        //arr.length-1-0为了减少比较的次数
        for (int x = 0; x < arr.length-1-0; x++) {
               //ArrayIndexOutOfBoundsException
            if (arr[x] > arr[x + 1]) {
                //交换数据
                int temp = arr[x];
                arr[x] = arr[x + 1];
                arr[x + 1] = temp;
            }
        }
        //调用方法
        System.out.println("第一次比较完毕:");
        arrayPrint(arr);

        //第二次比较
        //arr.length-1 是为了防止索引越界
        //arr.length-1-1为了减少比较的次数
        for (int x = 0; x < arr.length-1-1; x++) {
            //ArrayIndexOutOfBoundsException
            if (arr[x] > arr[x + 1]) {
                //交换数据
                int temp = arr[x];
                arr[x] = arr[x + 1];
                arr[x + 1] = temp;
            }
        }
        //调用方法
        System.out.println("第二次比较完毕:");
        arrayPrint(arr);


        //第三次比较
        //arr.length-1 是为了防止索引越界
        //arr.length-1-1为了减少比较的次数
        for (int x = 0; x < arr.length-1-2; x++) {
            //ArrayIndexOutOfBoundsException
            if (arr[x] > arr[x + 1]) {
                //交换数据
                int temp = arr[x];
                arr[x] = arr[x + 1];
                arr[x + 1] = temp;
            }
        }
        //调用方法
        System.out.println("第三次比较完毕:");
        arrayPrint(arr);

        //第四次比较
        //arr.length-1 是为了防止索引越界
        //arr.length-1-1为了减少比较的次数
        for (int x = 0; x < arr.length-1-3; x++) {
            //ArrayIndexOutOfBoundsException
            if (arr[x] > arr[x + 1]) {
                //交换数据
                int temp = arr[x];
                arr[x] = arr[x + 1];
                arr[x + 1] = temp;
            }
        }
        //调用方法
        System.out.println("第四次比较完毕:");
        arrayPrint(arr);

         */
        //用循环改进
        /*
        for(int y=0;y<4;y++){
            for (int x = 0; x < arr.length-1-y; x++) {
                if (arr[x] > arr[x + 1]) {
                    //交换数据
                    int temp = arr[x];
                    arr[x] = arr[x + 1];
                    arr[x + 1] = temp;
                }
            }
        }

         */
        /*
        //循环做的次数不能写固定的值,用arr.length-1改进
        for(int y = 0;y<arr.length-1;y++){
            for (int x = 0; x < arr.length-1-y; x++) {
                if (arr[x] > arr[x + 1]) {
                    //交换数据
                    int temp = arr[x];
                    arr[x] = arr[x + 1];
                    arr[x + 1] = temp;
                }
            }
        }
        arrayPrint(arr);

         */
        //如果有多个数组排序,每个数组写这样的一段代码,太麻烦,怎么办呢?
        //用方法改进
       bubbleSort(arr);
        System.out.println("排序后:");
        arrayPrint(arr);
    }

    /*
     * 两个明确:
     *      返回值类型:void
     *      参数列表:int[]arr
     *
     * */
    public static void bubbleSort(int[] arr) {
        for (int x = 0; x < arr.length - 1; x++) {
            for (int y = 0; y < arr.length - 1 - x; y++) {
                if (arr[y] > arr[y + 1]) {
                    int temp = arr[y];
                    arr[y] = arr[y + 1];
                    arr[y + 1] = temp;
                }
            }
        }

    }

    /* 数组的遍历*/
    public static void arrayPrint(int[] arr) {
        System.out.print("[");
        for(int x = 0;x<arr.length;x++){
            if(x == arr.length-1){
                System.out.print(arr[x]);
            }else{
                System.out.print(arr[x]+", ");
            }
        }
        System.out.print("]");

    }

}

1.2 Arrays工具类

1.2.1 Arrays类的概述和使用

Arrays:提供了对数组操作的各种方法。
public static String toString(int[] a):把数组转成字符串
public static void sort(int[] a):对数组进行升序排序

1.2.1.1 案例代码二
package day10;

import java.util.Arrays;

/*
* Arrays:提供了对数组操作的各种方法
*
*  public static String toString(int[] a):把数组转成字符串
*  public static void sort(int[] a):对数组进行升序排序
*
*
*
* */
public class myArrays {
    public static void main(String[] args) {
        //定义一个数组
        int[] arr = {24, 69, 80, 57, 13};
        //public static String toString(int[] a):把数组转成字符串
        System.out.println("排序前:"+ Arrays.toString(arr));

        //public static void sort(int[] a):对数组进行升序排序
        Arrays.sort(arr);
        System.out.println("排序后:"+ Arrays.toString(arr));

    }

}

1.2.2 Arrays类中构造方法的问题

Arrays类中真的没有构造方法吗?
- 一个类中没有构造方法,系统将提供一个无参构造方法。
而我们在帮助文档中没有看到Arrays类的构造方法,这是为什么呢?
- Arrays类中有构造方法,只不过构造方法被private修饰,外界是无法使用的。因为外界无法使用,所以帮助文档中就看不到。
通过查看源码,我们找到了如下的内容:
private Arrays() {}
Arrays类的这种设计是常用的工具类的设计思想:
- 构造方法私有。
- 成员都用static修饰。
Math,Collections等

第2章 包装类

2.1 基本类型包装类的概述

需求:我要判断一个数据是否在int范围内?
要想判断一个数据是否在int范围内,首先我们得知道int范围,在前面我们讲解基本数据类型的时候说过了:
-2147483648 到 2147483647
为了对基本数据类型进行更多更方便的操作,Java就针对每一种基本数据类型提供了一个对应的引用类型。
基本类型包装类:
- Byte byte
- Short short
- Integer int
- Long long
- Float float
- Double double
- Character char
- Boolean boolean
基本数据类型包装类最常见的用法就是用于和字符串之间进行相互转换。

2.2 Integer类的概述和构造方法

Integer:Integer类在对象中包装了一个基本类型 int 的值。
构造方法:
Integer(int value)
Integer(String s)
注意:这个字符串必须由数字字符组成

2.2.1 案例代码三

package day10;
/*
* Integer :Integer类在对象中包装了一个基本类型 int 的值
* 构造方法:
*     Integer(int value)
*
*     Integer(String s)
       注意:这个字符串必须有数字字符组成
 *
* */
public class IntegerTest01 {
    public static void main(String[] args) {
        //Integer(int value)
        int value = 100;
        Integer i = new Integer(value);
        System.out.println(i);

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



        //Integer(String s)
        String s = "100";
        //NumberFormatException: 数据格式化异常
        //String s = "abc";
        Integer ii = new Integer(s);
        System.out.println(ii);
    }
}

2.3 int类型和String类型的相互转换

int类型和String类型的相互转换
int – String
String类中:public static String valueOf(int i)

String – int
Integer类中:public static int parseInt(String s)

2.3.1 案例代码四

package day10;
/*
* int类型和String类型相互转换
* int --- String
*   String类中:public static String valueOf(int i)
*
* String --- int
*   Integer类中:public static int parseInt(String s)
*
* */
public class IntegerTest02 {
    public static void main(String[] args) {
        //int --- String
        int number = 100;
        //方式1
        String s1 = "" + number;
        System.out.println(s1);

        //方式2
        //public static String valueOf(int i)
        String s2 = String.valueOf(number);
        System.out.println(s2);

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

        //String --- int
        String s3 = "100";
        //方式1
        //String -- Integer -- int
        Integer i = new Integer(s3);
        //public int intValue()
        int x = i.intValue();
        System.out.println(x);

        //方式2
        //public static int parseInt(String s)
        int y = Integer.parseInt(s3);
        System.out.println(y);
    }
}

2.4 Integer的练习之把字符串中的数据排序

需求:
我有如下一个字符串:”91 27 46 38 50”
请写代码实现最终输出结果是:”27 38 46 50 91”
提示:这里需要参考String类中的方法
public String[] split(String regex)

2.4.1 案例代码五

package day10;

import java.util.Arrays;

/*
*   需求:
*   我有如下一个字符串:”91 27 46 38 50”
*  	请写代码实现最终输出结果是:”27 38 46 50 91”
*  	提示:这里需要参考String类中的方法
*  	public String[] split(String regex)
*
*  分析:
*     1.定义一个字符串对象
*     2.把字符串中的数据存储到int类型的数组中
*     3.对int数组进行排序
*     4.把排序后的数组中的元素进行拼接得到一个字符串
*     5.输出字符串
*/
public class IntegerTest03 {
    public static void main(String[] args) {
       //定义一个字符串对象
        String s = "91 27 46 38 50";
        //public String[] split(String regex)
        String[] strArray = s.split(" ");
        /*
        for(int x=0;x<strArray.length;x++){
            System.out.println(strArray[x]);
        }

         */
        //定义一个int类型的数组
        int[]arr = new int [strArray.length];
        for(int x= 0;x<strArray.length;x++){
             arr[x] = Integer.parseInt(strArray[x]);
        }
        //对int数组进行排序
        Arrays.sort(arr);

        //把排序后的数组中的元素进行拼接得到一个字符串
        StringBuilder sb = new StringBuilder();
        for(int x =0;x<arr.length;x++){
            if(x==arr.length-1){
                sb.append(arr[x]);

            }else{
                sb.append(arr[x]).append(" ");
            }
        }
        String result = sb.toString();

        //输出字符串
        System.out.println("result:" + result);

    }
}

2.5 JDK5的新特性自动装箱和拆箱

JDK5新特性:
自动装箱:把基本数据类型转换为对应的包装类类型
public static Integer valueOf(int i)
自动拆箱:把包装类类型转换为对应的基本数据类型
public int intValue()
Java程序的运行:
编写java文件 – 编译生成class文件 – 执行
注意:在使用包装类类型的新特性的时候,如果做操作,最好先判断是否为null。
开发中的原则:
只要是对象,在使用前就必须进行不为null的判断。

2.5.1 案例代码六:

package day10;
/*
* JDK5提供的新特性:
* 自动装箱:把基本数据类型转换为对应的包装类类型
* public static Integer valueOf(int i)
* 自动拆箱:把包装类类型转换为对应的基本数据类型
* public int intValue()
*
* java程序的运行
*       编写java文件 -- 编译生成class文件 --- 执行
*
*注意:在使用包装类类型的时候,如果做操作最好先判断是否为null
*
* 开发中的原则:
*        只要是对象,在使用前就必须进行不为null的判断
* */
public class IntegerDemo {
    public static void main(String[] args) {
        //创建一个包装类类型的对象
        Integer i = new Integer(100);
        Integer ii = 100;//自动装箱
        //public static Integer valueOf(int i)
        ii +=200; //ii = ii + 200 自动拆箱,自动装箱
        //public int intValue()
        System.out.println(ii);
     /*
     *
     	Integer ii = Integer.valueOf(100);
		ii = Integer.valueOf(ii.intValue() + 200);
		System.out.println(ii);

     * */

        Integer iii = null;
        if (iii != null) {
            iii += 300;//NullPointerException
            System.out.println(iii);

        }

    }
}

第3章 Date类和SimpleDateFormat类

3.1 Date类的概述和构造方法

Date:Date表示特定的瞬间,精确到毫秒。
构造方法:
Date():根据当前时间创建的日期对象
Date(long date):根据给定的毫秒值创建对象,从1970 年 1 月 1 日 00:00:00

3.1.1 案例代码七:

package day10;

import java.util.Date;

/*
*  Date:Date表示特定的时间,精确到毫秒
*
* 构造方法:
*        Date() 根据当前时间创建的日期对象
*
*        Date(long date):根据给定的毫秒值创建对象,1970 年 1 月 1 日 00:00:00
 *
*
* */
public class DateTest {
    public static void main(String[] args) {
         //Date()
        Date d = new Date();
        System.out.println(d);

        //Date(long date)
        long date = 1000*60*60;
        Date dd = new Date(date);
        System.out.println(dd);

    }
}

3.2 Date类的成员方法getTime()和setTime()

成员方法:
public long getTime():获取的是毫秒值。从1970年1月1日 00:00:00开始的。
public void setTime(long time):设置时间,给的是毫秒值。
从Date得到一个毫秒值:
getTime()
从一个毫秒值得到一个Date对象
构造方法
setTime(long time)

3.2.1 案例代码八:

package day10;

import java.util.Date;

/*
*  public long getTime():获取的是毫秒值。从1970年1月1日 00:00:00开始的。
*  public void setTime(long time):设置时间,给的是毫秒值。
*
* 从Date得到一个毫秒值
*
*       getTime():
*
* 从一个毫秒值得到一个Date对象
*
*		构造方法
*		setTime(long time)
*
*
* */
public class DateTest02 {
    public static void main(String[] args) {
        //创建对象
        Date d = new Date();
        //public long getTime()
        System.out.println(d.getTime());


        //public void setTime(long time)
        d.setTime(1000*60*60);
        System.out.println(d.getTime());
    }

}

3.3 SimpleDateFormat类的概述和使用

SimpleDateFormat:是一个以与语言环境有关的方式来格式化和解析日期的具体类。
它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)
格式化(日期 -> 文本): Date – String
public final String format(Date date)
解析(文本 -> 日期): String – Date
public Date parse(String source)
package com.itheima_01;

3.3.1 案例代码九:

package day10;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
* SimpleDateFormat:是一个以与语言环境有关的方式来格式化和解析日期的具体类。
*                   它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)
*
*格式化(日期 -> 文本):Date -- String
*   public final String format(Date date)
*
*
*解析(文本 -> 日期):String -- Date
*   public Date parse(String source)
*
* */
public class SimpleDateFormatTest {
    public static void main(String[] args) throws ParseException {
        /*
        //从Date -- String
        Date d = new Date();
       //SimpleDateFormat() //用默认模式
        //SimpleDateFormat sdf = new SimpleDateFormat();
        //SimpleDateFormat(String pattern):用给定的模式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        //现在虽然实现了把日期格式化成一个字符串,但是不是我们想要的格式
        //我们想要的格式是:xxxx年xx月xx日 xx:xx:xx

        String s = sdf.format(d);
        System.out.println(s);

         */

        //从String --- Date
        String str = "2025-07-23 14:57:33";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        把一个字符串解析为日期的时候,请注意模式字符串和给定的日期字符串的格式要匹配
        Date d = sdf.parse(str);
        System.out.println(d);
    }
}

3.4 Date的练习之日期工具类的定义和使用

3.4.1 制作工具类DateUtil

3.4.1.1 案例代码十:
package day10;
/*
* 工具类:
*
* 构造方法私有
* 成员方法静态
*
*
*/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    private DateUtil(){}

    /*
    * 把日期转换为制定格式的字符串
    * 两个明确:
    *      返回值类型:String
    *      参数列表: Date date , String format
    *
    * */
    public static String dateToString(Date date, String format){

        SimpleDateFormat sdf = new SimpleDateFormat(format);
             String s = sdf.format(date);
             return s;
    }

   /*
   * 把指定格式的字符串解析为日期
   * 两个明确:
   *    返回值类型:Date
   *    参数列表: String s ,String format
   *
   *
   * */
   public static Date StringToDate(String s, String format) throws ParseException {
       SimpleDateFormat sdf = new SimpleDateFormat(format);
       Date d = sdf.parse(s);
       return d;
   }

}

3.4.2 测试工具类

3.4.2.1 案例代码十一:
package day10;

import java.text.ParseException;
import java.util.Date;

public class DateUtilTest {
    public static void main(String[] args) throws ParseException {
        //创建日期对象
        Date d = new Date();

        String s = DateUtil.dateToString(d,"yyyy年MM月dd日 HH:mm:ss");
        System.out.println(s);

        String s2 = DateUtil.dateToString(d,"yyyy年MM月dd日");
        System.out.println(s2);

        String s3 = DateUtil.dateToString(d,"HH:mm:ss");
        System.out.println(s3);

        String str = "2020-07-23 15:45:23";
        Date d1 = DateUtil.StringToDate(str,"yyyy-MM-dd HH:mm:ss");
        System.out.println(d1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值