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型数组构造

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);//相当于数组,按索引值来查找

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", "张三"));//判断字符串开始和结束

str2 = "abcdefg";

str2.startsWith("a");

System.out.println("判断起始=" + str2.startsWith("abc"));

System.out.println("判断起始=" + (str2.indexOf("abc") == 0));

System.out.println("判断结束=" + str2.endsWith("efg"));

String str3= "efg";

System.out.println("判断结束="

+ (str2.lastIndexOf("efg") == str2.length() -str3.length()));

str1= new String("abc");//开辟了两个类,两个内存空间“==”判断是指针方向,是地址

str2 = new String("abc");

System.out.println("判断字符串是否相等结果=" + (str1 == str2));//方法错误

System.out.println("判断字符串是否相等结果=" +str1.equals(str2));

str1= "abc";

str2= "abc";//把已有的abc地址赋给str2,str1和str2指向同一个地址

str2 = "def";//重新开辟空间def,str新地址改为def,str1还是原来的地址abc

System.out.println("判断字符串是否相等结果=" + (str1 == str2) + " str2=" +str2);

System.out.println("判断字符串是否相等结果=" + (str1 == str2));//常量的方法能判断?

str1 = "abc";

System.out.println("转大小写结果=" +str1.toUpperCase());

str1= "ABV";

System.out.println("转大小写结果=" +str1.toLowerCase());

str2= "abc#def#ghr#xyz";//用#存客户不同数据,然后用数组提取

String[] array = str2.split("#");for (int i = 0; i < array.length; i++) {

System.out.println("结果=" +array[i]);

}//数学运算

Math.round(123.556);//四舍五入

System.out.println("四舍五入" + Math.round(123.556));//取上限值 >=最小整数

System.out.println("取上限值" + Math.ceil(123.456));//去下限值 <=最大整数

System.out.println("取下限值" + Math.floor(123.456));//PI字母全大写代表常量

System.out.println("PI=" +Math.PI);//取随机数

System.out.println("随机数=" +Math.random());

System.out.println("随机数=" +Math.random());

System.out.println("随机数=" +Math.random());

System.out.println("随机数=" +Math.random());

System.out.println("整数suijishu=" + (int)(Math.random()*100));

System.out.println("整数suijishu=" + (int)(Math.random()*100));

System.out.println("整数suijishu=" + (int)(Math.random()*100));

Random r= new Random();//没有种子的时候用时间做种子//r.nextInt(1);//随机数种子

System.out.println("随机数=" + r.nextInt(100));

System.out.println("随机数=" + r.nextInt(100));

System.out.println("随机数=" + r.nextInt(100));

System.out.println("随机数=" + r.nextInt(100));

System.out.println("随机数=" + r.nextInt(100));

System.out.println("随机数=" + r.nextInt(1000));//包装类---进行数据转换

long l = 123;int m = (int) l;//同类型之间可以相互转换

int i = 0;

String s= "123";

Integer Int= new Integer("123");//实例化int对象

Int.valueOf(s);//整数转为字符串

Int.toString();//int转为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();//<>里面要放包装类//放数据

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循环

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值