java语法记录
字符操作
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
public class hello3 {
public static void main(String[] args) {
String str="To be or not to be is a question";
String ss[]=str.split(" ");
Arrays.sort(ss,(String o1,String o2)->o1.length()-o2.length());
Arrays.sort(ss, Comparator.comparingInt(String::length));
System.out.println(Arrays.toString(ss));
String s=String.join("\n",ss);
System.out.println(s);
ArrayList names=new ArrayList();
names.add("1"); names.add("2"); names.add("3");
System.out.println(String.join("-", names));
}
}
二维各种数组比较
import java.util.Arrays;
public class hello3 {
public static void main(String[] args) {
String s1="abc";
String s2="abc";
String s3=new String("ab");
System.out.println(s3.compareTo(s1));
Object[][] a={{20,"male"},{21,"female"},{20,"female"},{19,"male"},{20,"male"}};
Arrays.sort(a,(Object[] o1,Object o2[])->(int)o1[0]==(int)o2[0]?
((String)o1[1]).compareTo((String)o2[1]):(int)o1[0]-(int)o2[0]);
System.out.println(Arrays.deepToString(a));
}
}
StringBuffer
public class hello3 {
public static void main(String[] args) {
StringBuffer s1=new StringBuffer("");
String s2=s1.toString();
int n=8,m=3,k=0;
for(int i=0;i<n;i++)s1.append((char)('A'+i));
System.out.println(s1);
while(s1.length()>1){
k=(k+m-1)%s1.length();
s1.deleteCharAt(k);
}
System.out.println(s1);
System.out.println(s1.charAt(0)-'A'+1);
}
}
//string valueOf
public class Test {
public static void main(String args[]) {
double d = 1100.00;
boolean b = true;
long l = 1234567890;
char[] arr = {'r', 'u', 'n', 'o', 'o', 'b' };
System.out.println("返回值 : " + String.valueOf(d) );
System.out.println("返回值 : " + String.valueOf(b) );
System.out.println("返回值 : " + String.valueOf(l) );
System.out.println("返回值 : " + String.valueOf(arr) );
}
}
//
import java.util.HashMap;
public class J84 {
public static void main(String[] args) {
HashMap hp=new HashMap();
String s="Jinan University";
char ch;int i;
for(i=0;i<s.length();i++){
ch=s.charAt(i);
if(('a'<=ch)&&(ch<='z'))
hp.put((char)(ch-32), (int)hp.getOrDefault((char)(ch-32), 0)+1);;
if(('A'<=ch)&&(ch<='Z'))
hp.put(ch, (int)hp.getOrDefault(ch, 0)+1);
}
System.out.println(hp);
}
}