常见对象部分案例总结
1.String类
A:字符串遍历
String s = "...";
for(int x = 0;x < s.length; x++){
char ch = s.charAt(x);
System.out.println(ch)
}
B:统计字符串中大写、小写及数字字符的个数
String s = "...";
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
for(int x = 0;x < s.length();x++){
char ch = s.charAt(x);
if('a' <= ch && ch <= 'z'){
smallCount++;
}else if('A' <= ch && ch <= 'Z'){
bigCount++;
}else if('0' <= ch && ch <= '9'){
numberCount++;
}
}
C:把字符串的首字母转成大写,其他小写
String s = "...";
//获取第一个字符
String s1 = s.substring(0,1);
//获取其他字符
String s2 = s.substring(1);
//把第一个字符装换为大写
String s3 = s1.toUpperCase();
//把其他字符装换为小写
String s4 = s2.toLowerCase();
//拼接成新字符
String s5 = s3.concat(s4);
链式化编程
String result = s.substring(0,1).toUpperCase.concat(s.substring(1).toLowerCase);
D:把int数组拼接成一个指定格式的字符串
int[] arr = {...};
String s = "";
s += "[";
for(int x = 0;x < arr.length; x++){
if(x == arr.length - 1){
s += arr[x];
s += "]";
}else{
s += arr[x];
s += ", ";
}
}
E:字符串反转
String s = "...";
char[] chs = s.toCharArray();
String result = "";
for(int x = chs.length - 1; x >= 0; x--){
result += chs[x];
}
F:统计大串中小串出现的次数
String maxString = "......";
String minString = "...";
int index = maxString.indexOf(minString);
int count = 0;
while(index != -1){
count++;
int newIndex = index + minString.length();
maxString = maxString.substring(newIndex);
index = maxString.indexOf(minString);
}
改进版:
int index = -1;
while((index = maxString.indexOf(minString)) != -1){
count++;
maxString = maxString.substring(index + minString.length());
}
2.StringBuffer类
A:String和StringBuffer相互转换
a:String --- StringBuffer
String s = "...";
方式一:使用构造方法
StringBuffer sbu = new StringBuffer(s);
方式二:使用append()方法
StringBuffer sbu2 = new StringBuffer();
sbu2.append(s);
b:StringBuffer --- String
StringBuffer sbu = new StringBuffer();
方式一:使用构造方法
String s = new String(sbu);
方式二:使用toString()方法
String s2 = sbu.toString();
B:字符串拼接
int[] arr = {......};
StringBuffer sbu = new StringBuffer();
sbu.append("[");
for(int x = 0;x < arr.length(); x++){
if(x == arr.length - 1){
sbu.append(arr[x]);
}else{
sbu.append(arr[x]).append(", ");
}
}
sbu.append("]");
C:字符串反转
String s = "...";
//创建字符串缓冲区对象
StringBuffer abu = new StringBuffer(s);
//反转
sbu.reverse();
String result = sbu.toString();
//改进版
String result = new StringBuffer(s).reverse().toString();
D:判断一个字符串是否对称
a:把字符串转换成数组比较
String s = "....";
boolean flag ;
char[] chs = s.toCharArray();
for(int start = 0,end = chs.length - 1; start <= end; start ++,end--){
if(chs[start] != chs[end]){
flag = false;
}else{
flag = true;
}
}
b:使用字符串缓冲区对象的反转功能
String s = "....";
StringBuffer sbu = new StringBuffer(s);
boolean flag = sbu.reverse().toString().equals(s);
3.Arrays类
A:冒泡排序
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;
}
}
}
}
B:选择排序
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[y];
arr[y] = arr[x];
arr[x] = temp;
}
}
}
}
C:无序数组基本查找
从头到尾一次查找
D:有序数组二分法查找
public static int getIndex(int[] arr,int value){
//定义最大索引和最小索引
int max = arr.length - 1;
int min = 0;
//计算中间索引
int mid = (min + max)/2;
//查找比较判断
while(arr[mid] != value){
if(arr[mid] > value){
max = mid - 1;
}else if(arr[mid] < value){
min = mid + 1;
}
if(min > max){
return -1;
}
mid = (min + max)/2;
}
return mid;
}
E:对字符串中的字符排序
String s = "....";
char[] chs = s.toCharArray();
Arrays.sort(chs);
String result = String.valueOf(chs);
4.Integer类
A:获取100的二进制、八进制、十六进制,以及其他进制数
String binary = Integer.toBinaryString(100);
String octal = Integer.toOctalString(100);
String hex = Integer.toHexString(100);
String other1 = Integer.toString(100,5);
String other2 = Integer.toString(100,32);
B:获取其他进制到十进制的数
int i1 = Integer.parseInt("100", 10);
int i2 = Integer.parseInt("100", 2);
int i3 = Integer.parseInt("100", 8);
int i4 = Integer.parseInt("100", 16);
int i5 = Integer.parseInt("100", 5);
C:String --- int 之间的相互转换
a:int ---String
int number = 100;
//方式一:
String s1 = "" + number;
//方式二:
String s2 = String.valueOf(number);
//方式三:
String s4 = Integer.toString(number);
b:String --- int
String s = "100";
//方式一:
int y1 = Integer.parseInt(s);
//方式二:
Integer i = new Integer(s);
int y2 = i.intValue(); |
|