java--常用API02

冒泡排序

相邻元素两两比较,大的往后放,
第一次,比较完毕后,最大值就出现在了最大索引处。
第二次比较,比较完毕后,次大值就出现在了次大索引处,
。。。
最终得到排序好的。

public static void main(String[] args) {
		//定义一个int类型的数组
		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("第一次比较完毕:");
		printArray(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("第二次比较完毕:");
		printArray(arr);
		
		//第三次比较
		//arr.length-1是为了防止索引越界
		//arr.length-1-2是为了减少比较的次数
		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("第三次比较完毕:");
		printArray(arr);
		
		//第四次比较
		//arr.length-1是为了防止索引越界
		//arr.length-1-3是为了减少比较的次数
		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("第四次比较完毕:");
		printArray(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;
				}
			}
		}
		printArray(arr);
		*/
		
		//如果我有多个数组要排序,每个数组写这样的一段代码,太麻烦,怎么办呢
		//用方法改进
		sort(arr);
		System.out.println("排序后:");
		printArray(arr);
	}
	
	/*
	 * 两个明确:
	 * 		返回值类型:void
	 * 		参数列表:int[] arr
	 */
	public static void sort(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 printArray(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.println("]");
	}
}

Arrays工具类

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

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));

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

Math.Collections等。

基本类型包装类:

	Byte	byte
	Short	short
	Integer	int
	Long	long
	Float	float
	Double	double
	Character	char
	Boolean	boolean

基本数据类型包装类最常见的用法就是用于和字符串之间进行相互转换。

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

//int->String
        int number = 100;

        String s1 = ""+ number;
        System.out.println(s1);

        //String->int
        String s = "100";
        int y = Integer.parseInt(s);
        System.out.println(y);
package IntegerDemo;

import java.util.Arrays;

/*
*  我有如下一个字符串:”91 27 46 38 50”
 * 请写代码实现最终输出结果是:”27 38 46 50 91”
 * 提示:这里需要参考String类中的方法
*  public String[] split(String regex)
*  分析:
 * 		A:定义一个字符串对象
 * 		B:把字符串中的数字数据存储到一个int类型的数组中
 * 		C:对int数组进行排序
 * 		D:把排序后的数组中的元素进行拼接得到一个字符串
 * 		E:输出字符串

 */
public class IntegerDemo {
    public static void main(String[] args) {
        String s = "91 27 46 38 50 ";

        String[] strArray = s.split(" ");

        int[] arr = new int[strArray.length];
        for (int x=0; x < arr.length; x++) {
            arr[x] = Integer.parseInt(strArray[x]);
        }
            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);

        }
    }

JDK5新特性:

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

import java.util.Date;

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

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

}

Date:Date表示特定的瞬间,精确到毫秒。

  • 构造方法:
  •  Date():根据当前时间创建的日期对象
    
  •  Date(long date):根据给定的毫秒值创建对象,从1970 年 1 月 1 日 00:00:00 
    
	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);
	}

public long getTime():获取的是毫秒值。从1970年1月1日 00:00:00开始的。

  • public void setTime(long time):设置时间,给的是毫秒值。
  • 从Date得到一个毫秒值:
  •  getTime()
    
  • 从一个毫秒值得到一个Date对象
  •  构造方法
    
  •  setTime(long time)
    
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());
	}

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

package com.itheima;

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 com.itheima;

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 = "2080-08-08 12:23:34";
		Date dd = DateUtil.stringToDate(str, "yyyy-MM-dd HH:mm:ss");
		System.out.println(dd);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值