黑马程序员-------API中常用类和JDK5新特性

第一讲 API中常用类
1.Arrays类
Arrays:针对数组进行操作的工具类。比如说排序和查找。
1:public static String toString(int[] a) 把数组转成字符串
  2:public static void sort(int[] a) 对数组进行排序
  3:public static int binarySearch(int[] a,int key) 二分查找
讲到排序,首先讲到数组中排序常用的两种方法
a:冒泡排序:
   相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处
  冒泡排序图解



冒泡排序实现:
public class ArrayDemo {
	public static void main(String[] args) {
		// 定义一个数组
		int[] arr = { 24, 69, 80, 57, 13 };
		System.out.println("排序前:");
		printArray(arr);

		/*
		// 第一次比较
		// arr.length - 1是为了防止数据越界
		// arr.length - 1 - 0是为了减少比较的次数
		for (int x = 0; x < arr.length - 1 - 0; x++) {
			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++) {
			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++) {
			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++) {
			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;
		// }
		// }
		// }

		/*
		// 由于我们知道比较的次数是数组长度-1次,所以改进最终版程序
		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;
				}
			}
		}
		System.out.println("排序后:");
		printArray(arr);
		*/
		
		//由于我可能有多个数组要排序,所以我要写成方法
		bubbleSort(arr);
		System.out.println("排序后:");
		printArray(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 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("]");
	}
}
b:选择排序:
   从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
选择排序图解

选择排序算法实现:
public class ArrayDemo {
	public static void main(String[] args) {
		// 定义一个数组
		int[] arr = { 24, 69, 80, 57, 13 };
		System.out.println("排序前:");
		printArray(arr);

		/*
		// 第一次
		int x = 0;
		for (int y = x + 1; y < arr.length; y++) {
			if (arr[y] < arr[x]) {
				int temp = arr[x];
				arr[x] = arr[y];
				arr[y] = temp;
			}
		}
		System.out.println("第一次比较后:");
		printArray(arr);

		// 第二次
		x = 1;
		for (int y = x + 1; y < arr.length; y++) {
			if (arr[y] < arr[x]) {
				int temp = arr[x];
				arr[x] = arr[y];
				arr[y] = temp;
			}
		}
		System.out.println("第二次比较后:");
		printArray(arr);

		// 第三次
		x = 2;
		for (int y = x + 1; y < arr.length; y++) {
			if (arr[y] < arr[x]) {
				int temp = arr[x];
				arr[x] = arr[y];
				arr[y] = temp;
			}
		}
		System.out.println("第三次比较后:");
		printArray(arr);

		// 第四次
		x = 3;
		for (int y = x + 1; y < arr.length; y++) {
			if (arr[y] < arr[x]) {
				int temp = arr[x];
				arr[x] = arr[y];
				arr[y] = temp;
			}
		}
		System.out.println("第四次比较后:");
		printArray(arr);
		*/
		
		/*
		//通过观察发现代码的重复度太高,所以用循环改进
		for(int x=0; x<arr.length-1; x++){
			for(int y=x+1; y<arr.length; y++){
				if(arr[y] <arr[x]){
					int temp = arr[x];
					arr[x] = arr[y];
					 arr[y] = temp;
				}
			}
		}
		System.out.println("排序后:");
		printArray(arr);
		*/
		
		//用方法改进
		selectSort(arr);
		System.out.println("排序后:");
		printArray(arr);

	}
	
	public static void selectSort(int[] arr){
		for(int x=0; x<arr.length-1; x++){
			for(int y=x+1; y<arr.length; y++){
				if(arr[y] <arr[x]){
					int temp = arr[x];
					arr[x] = arr[y];
					 arr[y] = 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类实现数组操作如下:
public class ArraysDemo {
	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));

		// [13, 24, 57, 69, 80]
		// public static int binarySearch(int[] a,int key) 二分查找
		System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57));
		System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
	}
}


2. Integer类
 Integer类其实就是int的引用类型
Integer类在对象中包装了一个基本类型int 的值
该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法
Integer的构造方法
 public Integer(int value)
 public Integer(String s)
   注意:这个字符串必须是由数字字符组成

  int类型和String类型的相互转换
  
  int -- String
   String.valueOf(number)
  
  String -- int
   Integer.parseInt(s)

常用的基本进制转换
 public static String toBinaryString(int i)
 public static String toOctalString(int i)
 public static String toHexString(int i)
 
 十进制到其他进制
 public static String toString(int i,int radix)

 其他进制到十进制
 public static int parseInt(String s,int radix)


3. Character类
Character类是char的引用类型
Character类在内部包装了一个基本类型char的值

构造方法:
   Character(char value)

其他方法:
  public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
  public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
  public static boolean isDigit(char ch):判断给定的字符是否是数字字符
  public static char toUpperCase(char ch):把给定的字符转换为大写字符
  public static char toLowerCase(char ch):把给定的字符转换为小写字符
Character类代码示例:
/*
 * 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
 * 
 * 分析:
 * 		A:定义三个统计变量。
 * 			int bigCont=0;
 * 			int smalCount=0;
 * 			int numberCount=0;
 * 		B:键盘录入一个字符串。
 * 		C:把字符串转换为字符数组。
 * 		D:遍历字符数组获取到每一个字符
 * 		E:判断该字符是
 * 			大写	bigCount++;
 * 			小写	smalCount++;
 * 			数字	numberCount++;
 * 		F:输出结果即可
 */
public class CharacterTest {
	public static void main(String[] args) {
		// 定义三个统计变量。
		int bigCount = 0;
		int smallCount = 0;
		int numberCount = 0;

		// 键盘录入一个字符串。
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个字符串:");
		String line = sc.nextLine();

		// 把字符串转换为字符数组。
		char[] chs = line.toCharArray();

		// 历字符数组获取到每一个字符
		for (int x = 0; x < chs.length; x++) {
			char ch = chs[x];

			// 判断该字符
			if (Character.isUpperCase(ch)) {
				bigCount++;
			} else if (Character.isLowerCase(ch)) {
				smallCount++;
			} else if (Character.isDigit(ch)) {
				numberCount++;
			}
		}

		// 输出结果即可
		System.out.println("大写字母:" + bigCount + "个");
		System.out.println("小写字母:" + smallCount + "个");
		System.out.println("数字字符:" + numberCount + "个");
	}
}


4.Math类
Math类
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。里面的方法和变量全部都是静态的。
  成员变量:
   public static final double PI
   public static final double E
  成员方法:
   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):最大值 (min自学)
  public static double pow(double a,double b):a的b次幂
  public static double random():随机数 [0.0,1.0)
  public static int round(float a) 四舍五入
  public static double sqrt(double a):正平方根


5. Random类
 Random:产生随机数的类

构造方法:
public Random():没有给种子,用的是默认种子,是当前时间的毫秒值
public Random(long seed):给出指定的种子

给定种子后,每次得到的随机数是相同的。

 成员方法:
public int nextInt():返回的是int范围内的随机数
public int nextInt(int n):返回的是[0,n)范围的内随机数


6.BigInteger类
 BigInteger:可以让超过Integer范围内的数据进行运算
 
 构造方法:
 BigInteger(String val) 
 public BigInteger add(BigInteger val):加
 public BigInteger subtract(BigInteger val):减
 public BigInteger multiply(BigInteger val):乘
 public BigInteger divide(BigInteger val):除
 public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组


7.BigDecimal类
为什么会出现BigDecimal类
 float和的存储数据和整数存储数据不一样,它是带有有效数字位。
 
 由于在运算的时候,float类型和double很容易丢失精度。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
 
 BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。

 构造方法:
public BigDecimal(String val)
 其他方法
 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):商,几位小数,如何舍取


7.Date类

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

  构造方法:
   Date():根据当前的默认毫秒值创建日期对象
   Date(long date):根据给定的毫秒值创建日期对象

  public long getTime():获取时间,以毫秒为单位

  public void setTime(long time):设置时间

 Date--String(格式化)
   public final String format(Date date)
  
  String -- Date(解析)
   public Date parse(String source)
  
  DateForamt:可以进行日期和字符串的格式化和解析,但是由于是抽象类,所以使用具体子类SimpleDateFormat。

 SimpleDateFormat的构造方法:
SimpleDateFormat():默认模式
SimpleDateFormat(String pattern):给定的模式
这个模式字符串该如何写呢?
通过查看API,我们就找到了对应的模式
年 y
月 M
日 d
时 H
分 m
秒 s

示例如2015年8月5日 16:40:41

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


示例:计算自己来到这个世界多少天?

 分析:
  A:键盘录入你的出生的年月日
   B:把该字符串转换为一个日期
   C:通过该日期得到一个毫秒值
  D:获取当前时间的毫秒值
   E:用D-C得到一个毫秒值
   F:把E的毫秒值转换为年
   /1000/60/60/24

public class MyYearOldDemo {
	public static void main(String[] args) throws ParseException {
		// 键盘录入你的出生的年月日
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的出生年月日:");
		String line = sc.nextLine();

		// 把该字符串转换为一个日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = sdf.parse(line);

		// 通过该日期得到一个毫秒值
		long myTime = d.getTime();

		// 获取当前时间的毫秒值
		long nowTime = System.currentTimeMillis();

		// 用D-C得到一个毫秒值
		long time = nowTime - myTime;

		// 把E的毫秒值转换为年
		long day = time / 1000 / 60 / 60 / 24;

		System.out.println("你来到这个世界:" + day + "天");
	}
}



8.Calendar类
Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 
日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

 public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。

 public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。
 public final void set(int year,int month,int date):设置当前日历的年月日

代码示例:

获取任意一年的二月有多少天

 分析:
A:键盘录入任意的年份
B:设置日历对象的年月日
年就是A输入的数据
月是2
日是1
C:把时间往前推一天,就是2月的最后一天
D:获取这一天输出即可

public class CalendarTest {
	public static void main(String[] args) {
		// 键盘录入任意的年份
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = sc.nextInt();

		// 设置日历对象的年月日
		Calendar c = Calendar.getInstance();
		c.set(year, 2, 1); // 其实是这一年的3月1日
		// 把时间往前推一天,就是2月的最后一天
		c.add(Calendar.DATE, -1);

		// 获取这一天输出即可
		System.out.println(c.get(Calendar.DATE));
	}
}


第二讲 JDK5新特性

1.自动拆箱装箱

自动装箱:把基本类型转换为包装类类型
自动拆箱:把包装类类型转换为基本类型

示例:Integer---int

Integer ii = 100;----->自动装箱,Integer.value)f(100)

ii += 200;ii ------> 自动拆箱,再自动装箱  Integer.valueOf(ii.intValue() + 200); 

注意:Integer的数据直接赋值,若在-128~127之间,直接从数据缓冲池中取数据

那么同样值地址相同,若超出这个范围,地址值则不同


2.增强for循环

 增强for:是for循环的一种。
 
 格式:
for(元素数据类型 变量 : 数组或者Collection集合) {
使用变量即可,该变量就是元素
    }
   
 好处:简化了数组和集合的遍历。
 
 弊端: 增强for的目标不能为null。
 如何解决呢?对增强for的目标先进行不为null的判断,然后在使用。

示例:

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

		// 定义一个字符串数组
		String[] strArray = { "xx", "yy", "zz", "qq" };
		// 增强for
		for (String s : strArray) {
			System.out.println(s);
		}
		System.out.println("---------------");
		// 定义一个集合
		ArrayList<String> array = new ArrayList<String>();
		array.add("hello");
		array.add("world");
		array.add("java");
		// 增强for
		for (String s : array) {
			System.out.println(s);
		}
	}
}


3.可变参数

 可变参数:定义方法的时候不知道该定义多少个参数
 格式:
  修饰符 返回值类型 方法名(数据类型…  变量名){
  
   }
  
   注意:
   这里的变量其实是一个数组
   如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
可变参数的简单应用

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

		//函数调用
		result1 = sum(10, 20, 30, 30, 40);
		System.out.println("result1:" + result1);

		result2 = sum(10, 20, 30, 30, 40, 50);
		System.out.println("result2:" + result2);
	}

	//求和函数----任意个数的
	public static int sum(int... a) {

		int s = 0;
		
		for(int x : a){
			s +=x;
		}
		
		return s;
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值