A:是针对数组进行操作的工具类。包括排序和查找等功能。
B:要掌握的方法(自己补齐方法)
把数组转成字符串:public static String toString(int[]a)
排序: public static void sort(int[]a)
二分查找: public static int binarySearch(int[]a,int key)
import java.util.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) 二分查找
*/
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));
}
}
(4)Arrays工具类的源码解析
public static String toString(int[] a)
public static void sort(int[] a) 底层是快速排序,知道就可以了。有空看,有问题再问我
public static int binarySearch(int[] a,int key)
开发原则:
只要是对象,我们就要判断该对象是否为null。
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:" + Arrays.toString(arr));
public static String toString(int[] a) {
//a -- arr -- { 24, 69, 80, 57, 13 }
if (a == null)
return "null"; //说明数组对象不存在
int iMax = a.length - 1; //iMax=4;
if (iMax == -1)
return "[]"; //说明数组存在,但是没有元素。
StringBuilder b = new StringBuilder();
b.append('['); //"["
for (int i = 0; ; i++) {
b.append(a[i]); //"[24, 69, 80, 57, 13"
if (i == iMax)
//"[24, 69, 80, 57, 13]"
return b.append(']').toString();
b.append(", "); //"[24, 69, 80, 57, "
}
}
-----------------------------------------------------
int[] arr = {13, 24, 57, 69, 80};
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
public static int binarySearch(int[] a, int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//key -- 577
return binarySearch0(a, 0, a.length, key);
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//fromIndex -- 0
//toIndex -- 5
//key -- 577
int low = fromIndex; //low=0
int high = toIndex - 1; //high=4
while (low <= high) {
int mid = (low + high) >>> 1; //mid=2,mid=3,mid=4
int midVal = a[mid]; //midVal=57,midVal=69,midVal=80
if (midVal < key)
low = mid + 1; //low=3,low=4,low=5
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
(5)把字符串中的字符进行排序
举例:
"edacbgf"
得到结果
"abcdefg"
package cn.itcast_03;
/*
* 把字符串中的字符进行排序。
* 举例:"dacgebf"
* 结果:"abcdefg"
*
* 分析:
* A:定义一个字符串
* B:把字符串转换为字符数组
* C:把字符数组进行排序
* D:把排序后的字符数组转成字符串
* E:输出最后的字符串
*/
public class ArrayTest {
public static void main(String[] args) {
// 定义一个字符串
String s = "dacgebf";
// 把字符串转换为字符数组
char[] chs = s.toCharArray();
// 把字符数组进行排序
bubbleSort(chs);
//把排序后的字符数组转成字符串
String result = String.valueOf(chs);
//输出最后的字符串
System.out.println("result:"+result);
}
// 冒泡排序
public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
}
3:Integer(掌握)
(1)为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
/*
* 需求1:我要求大家把100这个数据的二进制,八进制,十六进制计算出来
* 需求2:我要求大家判断一个数据是否是int范围内的。
* 首先你的知道int的范围是多大?
*
* 为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了对应的类类型。包装类类型。
* byte Byte
* short Short
* int Integer
* long Long
* float Float
* double Double
* char Character
* boolean Boolean
*
* 用于基本数据类型与字符串之间的转换。
*/
public class IntegerDemo {
public static void main(String[] args) {
// 不麻烦的就来了
// public static String toBinaryString(int i)
System.out.println(Integer.toBinaryString(100));
// public static String toOctalString(int i)
System.out.println(Integer.toOctalString(100));
// public static String toHexString(int i)
System.out.println(Integer.toHexString(100));
// public static final int MAX_VALUE
System.out.println(Integer.MAX_VALUE);
// public static final int MIN_VALUE
System.out.println(Integer.MIN_VALUE);
}
}
(2)Integer的构造方法
A:Integer i = new Integer(100);
B:Integer i = new Integer("100");
注意:这里的字符串必须是由数字字符组成
package cn.itcast_02;
/*
* Integer的构造方法:
* public Integer(int value)
* public Integer(String s)
* 注意:这个字符串必须是由数字字符组成
*/
public class IntegerDemo {
public static void main(String[] args) {
// 方式1
int i = 100;
Integer ii = new Integer(i);
System.out.println("ii:" + ii);
// 方式2
String s = "100";
// NumberFormatException
// String s = "abc";
Integer iii = new Integer(s);
System.out.println("iii:" + iii);
}
}
(3)String和int的相互转换
A:String -- int
Integer.parseInt("100");
B:int -- String
String.valueOf(100);
/*
* int类型和String类型的相互转换
*
* int -- String
* String.valueOf(number)
*
* String -- int
* Integer.parseInt(s)
*/
public class IntegerDemo {
public static void main(String[] args) {
// int -- String
int number = 100;
// 方式1
String s1 = "" + number;
System.out.println("s1:" + s1);
// 方式2
String s2 = String.valueOf(number);
System.out.println("s2:" + s2);
// 方式3
// int -- Integer -- String
Integer i = new Integer(number);
String s3 = i.toString();
System.out.println("s3:" + s3);
// 方式4
// public static String toString(int i)
String s4 = Integer.toString(number);
System.out.println("s4:" + s4);
System.out.println("-----------------");
// String -- int
String s = "100";
// 方式1
// String -- Integer -- int
Integer ii = new Integer(s);
// public int intValue()
int x = ii.intValue();
System.out.println("x:" + x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println("y:"+y);
}
}
(4)其他的功能(了解)
进制转换
/*
* 常用的基本进制转换
* 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)
* 由这个我们也看到了进制的范围:2-36
* 为什么呢?0,...9,a...z
*
* 其他进制到十进制
* public static int parseInt(String s,int radix)
*/
public class IntegerDemo {
public static void main(String[] args) {
// 十进制到二进制,八进制,十六进制
System.out.println(Integer.toBinaryString(100));
System.out.println(Integer.toOctalString(100));
System.out.println(Integer.toHexString(100));
System.out.println("-------------------------");
// 十进制到其他进制
System.out.println(Integer.toString(100, 10));
System.out.println(Integer.toString(100, 2));
System.out.println(Integer.toString(100, 8));
System.out.println(Integer.toString(100, 16));
System.out.println(Integer.toString(100, 5));
System.out.println(Integer.toString(100, 7));
System.out.println(Integer.toString(100, -7));
System.out.println(Integer.toString(100, 70));
System.out.println(Integer.toString(100, 1));
System.out.println(Integer.toString(100, 17));
System.out.println(Integer.toString(100, 32));
System.out.println(Integer.toString(100, 37));
System.out.println(Integer.toString(100, 36));
System.out.println("-------------------------");
//其他进制到十进制
System.out.println(Integer.parseInt("100", 10));
System.out.println(Integer.parseInt("100", 2));
System.out.println(Integer.parseInt("100", 8));
System.out.println(Integer.parseInt("100", 16));
System.out.println(Integer.parseInt("100", 23));
//NumberFormatException
//System.out.println(Integer.parseInt("123", 2));
}
}
(5)JDK5的新特性
自动装箱 基本类型--引用类型
自动拆箱 引用类型--基本类型
把下面的这个代码理解即可:
Integer i = 100;
i += 200;
/*
* JDK5的新特性
* 自动装箱:把基本类型转换为包装类类型 Integer类中valueof
* 自动拆箱:把包装类类型转换为基本类型 Integer类中intValue
*
* 注意一个小问题:
* 在使用时,Integer x = null;代码就会出现NullPointerException。
* 建议先判断是否为null,然后再使用。
*/
public class IntegerDemo {
public static void main(String[] args) {
// 定义了一个int类型的包装类类型变量i
// Integer i = new Integer(100);
Integer ii = 100;
ii += 200;
System.out.println("ii:" + ii);
// 通过反编译后的代码
// Integer ii = Integer.valueOf(100); //自动装箱
// ii = Integer.valueOf(ii.intValue() + 200); //自动拆箱,再自动装箱
// System.out.println((new StringBuilder("ii:")).append(ii).toString());
Integer iii = null;
// NullPointerException
if (iii != null) {
iii += 1000;
System.out.println(iii);
}
}
}
(6)面试题
-128到127之间的数据缓冲池问题
字符方法区中存在一个字节常量池
/*
* 看程序写结果
*
* 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据
*/
public class IntegerDemo {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
System.out.println("-----------");
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);
System.out.println(i3.equals(i4));
System.out.println("-----------");
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);
System.out.println(i5.equals(i6));
System.out.println("-----------");
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);
System.out.println(i7.equals(i8));
// 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
// Integer ii = Integer.valueOf(127);
}
}
4:Character(了解)
(1)Character构造方法
Character ch = new Character('a');
/*
* Character 类在对象中包装一个基本类型 char 的值
* 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然
*
* 构造方法:
* Character(char value)
*/
public class CharacterDemo {
public static void main(String[] args) {
// 创建对象
// Character ch = new Character((char) 97);
Character ch = new Character('a');
System.out.println("ch:" + ch);
}
}
(2)要掌握的方法:(自己补齐)
A:判断给定的字符是否是大写
B:判断给定的字符是否是小写
C:判断给定的字符是否是数字字符
D:把给定的字符转成大写
E:把给定的字符转成小写
/*
* 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):把给定的字符转换为小写字符
*/
public class CharacterDemo {
public static void main(String[] args) {
// public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
System.out.println("isUpperCase:" + Character.isUpperCase('A'));
System.out.println("isUpperCase:" + Character.isUpperCase('a'));
System.out.println("isUpperCase:" + Character.isUpperCase('0'));
System.out.println("-----------------------------------------");
// public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
System.out.println("isLowerCase:" + Character.isLowerCase('A'));
System.out.println("isLowerCase:" + Character.isLowerCase('a'));
System.out.println("isLowerCase:" + Character.isLowerCase('0'));
System.out.println("-----------------------------------------");
// public static boolean isDigit(char ch):判断给定的字符是否是数字字符
System.out.println("isDigit:" + Character.isDigit('A'));
System.out.println("isDigit:" + Character.isDigit('a'));
System.out.println("isDigit:" + Character.isDigit('0'));
System.out.println("-----------------------------------------");
// public static char toUpperCase(char ch):把给定的字符转换为大写字符
System.out.println("toUpperCase:" + Character.toUpperCase('A'));
System.out.println("toUpperCase:" + Character.toUpperCase('a'));
System.out.println("-----------------------------------------");
// public static char toLowerCase(char ch):把给定的字符转换为小写字符
System.out.println("toLowerCase:" + Character.toLowerCase('A'));
System.out.println("toLowerCase:" + Character.toLowerCase('a'));
}
}
(3)案例:
统计字符串中大写,小写及数字字符出现的次数
import java.util.Scanner;
/*
* 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
*
* 分析:
* 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 + "个");
}
}