java常用类总结_java——常用类的总结
packagetest;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.HashSet;importjava.util.Random;importjava.util.Set;public classStringlei {public static voidmain(String args[]) {//構建字符串
String str1 = "字符串常量";//1. 直接實例化 賦值 聲明變量
String str2= null;
str2= new String();//2.構造方法 用new
str2 = new String("實例化字符串");char[] c = new char[] { 'a', 'b', 'c' };//3.用char型數(shù)組構造
str2 = newString(c);
str2= "abcdefghijkml";//字符集//str2 = new String(bytes);
System.out.println("str2.length=" +str2.length());
System.out.println("str2=" +str2);//查找字符或字符串
int in = str2.indexOf("ee");
System.out.println("a=" + in);//相當于數(shù)組,按索引值來查找
int la = str2.lastIndexOf("l");
System.out.println("d=" +la);//獲取子字符串
str2.substring(5, 7);
String newStr= str2.substring(5, 7);
System.out.println("e=" +newStr);
str2= " ab c dds ";//去除前后空格
str2.trim();
System.out.println("去前后空格" +str2.trim());//查找和替換
str2.replace(" ", "");
System.out.println("查找替換空格" + str2.replace(" ", ""));
str2= "abc,你好,abcd";
System.out.println("查找替換" + str2.replace("abc", "張三"));//判斷字符串開始和結(jié)束
str2 = "abcdefg";
str2.startsWith("a");
System.out.println("判斷起始=" + str2.startsWith("abc"));
System.out.println("判斷起始=" + (str2.indexOf("abc") == 0));
System.out.println("判斷結(jié)束=" + str2.endsWith("efg"));
String str3= "efg";
System.out.println("判斷結(jié)束="
+ (str2.lastIndexOf("efg") == str2.length() -str3.length()));
str1= new String("abc");//開辟了兩個類,兩個內(nèi)存空間“==”判斷是指針方向,是地址
str2 = new String("abc");
System.out.println("判斷字符串是否相等結(jié)果=" + (str1 == str2));//方法錯誤
System.out.println("判斷字符串是否相等結(jié)果=" +str1.equals(str2));
str1= "abc";
str2= "abc";//把已有的abc地址賦給str2,str1和str2指向同一個地址
str2 = "def";//重新開辟空間def,str新地址改為def,str1還是原來的地址abc
System.out.println("判斷字符串是否相等結(jié)果=" + (str1 == str2) + " str2=" +str2);
System.out.println("判斷字符串是否相等結(jié)果=" + (str1 == str2));//常量的方法能判斷?
str1 = "abc";
System.out.println("轉(zhuǎn)大小寫結(jié)果=" +str1.toUpperCase());
str1= "ABV";
System.out.println("轉(zhuǎn)大小寫結(jié)果=" +str1.toLowerCase());
str2= "abc#def#ghr#xyz";//用#存客戶不同數(shù)據(jù),然后用數(shù)組提取
String[] array = str2.split("#");for (int i = 0; i < array.length; i++) {
System.out.println("結(jié)果=" +array[i]);
}//數(shù)學運算
Math.round(123.556);//四舍五入
System.out.println("四舍五入" + Math.round(123.556));//取上限值 >=最小整數(shù)
System.out.println("取上限值" + Math.ceil(123.456));//去下限值 <=最大整數(shù)
System.out.println("取下限值" + Math.floor(123.456));//PI字母全大寫代表常量
System.out.println("PI=" +Math.PI);//取隨機數(shù)
System.out.println("隨機數(shù)=" +Math.random());
System.out.println("隨機數(shù)=" +Math.random());
System.out.println("隨機數(shù)=" +Math.random());
System.out.println("隨機數(shù)=" +Math.random());
System.out.println("整數(shù)suijishu=" + (int)(Math.random()*100));
System.out.println("整數(shù)suijishu=" + (int)(Math.random()*100));
System.out.println("整數(shù)suijishu=" + (int)(Math.random()*100));
Random r= new Random();//沒有種子的時候用時間做種子//r.nextInt(1);//隨機數(shù)種子
System.out.println("隨機數(shù)=" + r.nextInt(100));
System.out.println("隨機數(shù)=" + r.nextInt(100));
System.out.println("隨機數(shù)=" + r.nextInt(100));
System.out.println("隨機數(shù)=" + r.nextInt(100));
System.out.println("隨機數(shù)=" + r.nextInt(100));
System.out.println("隨機數(shù)=" + r.nextInt(1000));//包裝類---進行數(shù)據(jù)轉(zhuǎn)換
long l = 123;int m = (int) l;//同類型之間可以相互轉(zhuǎn)換
int i = 0;
String s= "123";
Integer Int= new Integer("123");//實例化int對象
Int.valueOf(s);//整數(shù)轉(zhuǎn)為字符串
Int.toString();//int轉(zhuǎn)為String類型
Int.longValue();
i= Integer.parseInt("12345");
System.out.println("MAX_VALUE=" +Integer.MAX_VALUE);
System.out.println("MIN_VALUE=" +Integer.MIN_VALUE);
Int= Integer.valueOf("12345");
Long.parseLong("123456");
Float.parseFloat(s);
Double.parseDouble(s);boolean b =true;
b=Boolean.parseBoolean("TRUe");
System.out.println("b=" +b);//集合 ArrayList集合
int [] arr=new int[]{1,2,3};//實例化
ArrayList al = new ArrayList();//<>里面要放包裝類//放數(shù)據(jù)
al.add("abc");
al.add("bcd");
al.add("efg");
al.add("hij");//修改
al.set(0, "bbb");
al.contains("bcd");
System.out.println("contains="+al.contains("bcd"));
al.get(0);//括號里面放索引值
System.out.println("al=" + al.get(0));//讀//集合的遍歷
for(int n=0;n
System.out.println("al=" +al.get(n));}
al.remove(0);
al.clear();//foreach循環(huán)
for(String st:al){
System.out.print("al="+st+"\t");
}//Map集合 鍵值對 key/value 255000/淄博
HashMap hm = new HashMap();
hm.put("1", "淄博");
hm.put("2", "濟南");
hm.put("3", "青島");
hm.put("3", "煙臺");//key鍵不能重復,重復會自動覆蓋 value可以重復
hm.put(null, null);//可以放空值
System.out.println("長度是" +hm.size());;
hm.remove("3");
hm.containsKey("4");
hm.get("3"); //沒有索引值概念 輸入key 值
System.out.println("3代表" + hm.get("3"));
Set h =hm.keySet();for(String strh: h ){
System.out.println(strh+"="+hm.get(strh));
}//HashSet
HashSet hs =new HashSet();
hs.add("ac");
TestThread tt= newTestThread();
tt.start();//啟動多線程
TestThread tt2 = newTestThread();
tt2.start();
TestThread tt3= newTestThread();
tt3.start();
}
}
總結(jié)
以上是生活随笔為你收集整理的java常用类总结_java——常用类的总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《史莱姆牧场》纯种史莱姆玩法攻略
- 下一篇: java 优化 寄存器_JVM性能优化系