hasNextXxx() 判断是否还有下一个输入项,其中Xxx可以是Int,Double等。如果需要判断是否包含下一个字符串,则可以省略Xxx
nextXxx() 获取下一个输入项。Xxx的含义和上个方法中的Xxx相同,默认情况下,Scanner使用空格,回车等作为分隔符
输入整数加字符的方法
第一种:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
第二种:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么。 推荐
常见对象(String类的构造方法)
* A:常见构造方法
* public String():空构造
* public String(byte[] bytes):把字节数组转成字符串
* public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
* public String(char[] value):把字符数组转成字符串
* public String(char[] value,int index,int count):把字符数组的一部分转成字符串
* public String(String original):把字符串常量值转成字符串
常见对象(String类的常见面试题)(掌握)
* 1.判断定义为String类型的s1和s2是否相等
* String s1 = "abc";
* String s2 = "abc";
* System.out.println(s1 == s2); //true
* System.out.println(s1.equals(s2)); //true
* 2.下面这句话在内存中创建了几个对象?
* String s1 = new String("abc"); 2个,堆有个副本,常量区有个原本
* 3.判断定义为String类型的s1和s2是否相等
* String s1 = new String("abc"); //返回堆中副本的地址
* String s2 = "abc"; //记录的是常量区的地址值
* System.out.println(s1 == s2); //false
* System.out.println(s1.equals(s2)); //true
* 4.判断定义为String类型的s1和s2是否相等
* String s1 = "a" + "b" + "c"; //在编译时就变成了abc,把abc赋值给s1,有常量优化机制
* String s2 = "abc";
* System.out.println(s1 == s2); //true
* System.out.println(s1.equals(s2)); //true
* 5.判断定义为String类型的s1和s2是否相等
* String s1 = "ab";
* String s2 = "abc";
* String s3 = s1 + "c";
* System.out.println(s3 == s2); //false
* System.out.println(s3.equals(s2)); //true
String类的判断功能
* boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* boolean isEmpty():判断字符串是否为空。
String类的获取功能
* int length():获取字符串的长度。
* char charAt(int index):获取指定索引位置的字符
* int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
* int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
* int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
* lastIndexOf //从后往前找
* String substring(int start):从指定位置开始截取字符串,默认到末尾。
* String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
String的转换功能:
* byte[] getBytes():把字符串转换为字节数组。
* char[] toCharArray():把字符串转换为字符数组。
* static String valueOf(char[] chs):把字符数组转成字符串。
* static String valueOf(int i):把int类型的数据转成字符串。
* 注意:String类的valueOf方法可以把任意类型的数据转成字符串
* String toLowerCase():把字符串转成小写。(了解)
* String toUpperCase():把字符串转成大写。
* String concat(String str):把字符串拼接。
A:String的替换功能及案例演示
* String replace(char old,char new)
* String replace(String old,String new)
* B:String的去除字符串两空格及案例演示
* String trim()
* C:String的按字典顺序比较两个字符串及案例演示
* int compareTo(String str)(暂时不用掌握)
* int compareToIgnoreCase(String str)(了解)
StringBuffer的构造方法: 同步,安全,速度慢 容器
public StringBuffer():无参构造方法
public StringBuffer(int capacity):指定容量的字符串缓冲区对象
public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
StringBuffer的方法:
public int capacity():返回当前容量 //理论值
public int length():返回长度 //实际值
添加功能:
public StringBuffer append(String str): 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区中,并返回缓冲区本身
public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身
public StringBuffer replace(int start,int end,String str):替换
public StringBuffer reverse():反转
public String substring(int start):
public String substring(int start,int end): 返回值为String类型
StringBuffer 和 String 的相互转换 :
A:String -- StringBuffer
a:通过构造方法
b:通过append()方法
B:StringBuffer -- String
a:通过构造方法
b:通过toString()方法
c:通过subString(0,length);
StringBuffer和StringBuild的区别:
StringBuild:不同步,线程不安全。速度快,
冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) { //外循环只需要比较arr.length-1次就可以了
for (int j = 0; j < arr.length - 1 - i; j++) { //-1为了防止索引越界,-i为了提高效率
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j+1] = temp;
}
}
}
选择排序
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) { //只需要比较arr.length-1次
for (int j = i + 1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
二分查找
public static int getIndex(int[] arr, int value) {
int min = 0;
int max = arr.length - 1;
int mid = (min + max) / 2;
while(arr[mid] != value) { //当中间值不等于要找的值,就开始循环查找
if(arr[mid] < value) { //当中间值小于了要找的值
min = mid + 1; //最小的索引改变
}else if (arr[mid] > value){ //当中间值大于了要找的值
max = mid - 1; //最大的索引改变
}
mid = (min + max) / 2; //无论最大还是最小改变,中间索引都会随之改变
if(min > max) { //如果最小索引大于了最大索引,就没有查找的可能性了
return -1; //返回-1
}
}
return mid;
}
Arrays类的排序跟查找:
* public static String toString(int[] a)
* public static void sort(int[] a)
int[] a ={1,3,5,8,2,9,7}
Arrays.sort(a)
* public static int binarySearch(int[] a,int key)
基本类型和包装类的对应
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
* * A:int -- String
* a:和""进行拼接
* b:public static String valueOf(int i)
* c:int -- Integer -- String(Integer类的toString方法())
* d:public static String toString(int i)(Integer类的静态方法)
* B:String -- int
* a:String -- Integer -- int
* public static int parseInt(String s)
基本数据类型包装类有八种,其中七种都有parseXxx的方法,可以将这七种的字符串表现形式转换成基本数据类型
String s = "123"
int a = Integer.parseInt(s)
* * A:JDK5的新特性
* 自动装箱:把基本类型转换为包装类类型
* 自动拆箱:把包装类类型转换为基本类型
* B:案例演示
* JDK5的新特性自动装箱和拆箱
* Integer ii = 100;
* ii += 200;
* C:注意事项
* 在使用时,Integer x = null;代码就会出现NullPointerException。
* 建议先判断是否为null,然后再使用。