java——常用类的总结

 

package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class Testlianxi
{
public static void main (String args[]){    
String str1="abcdefgh";//定义字符串
int a=str1.length();//a表示str1的字符串个数


//查找字符串
int b=str1.indexOf("h");//相当于数组,查找索引值
System.out.println("查找结果为"+b);//查到了"h",输出结果为7,从0开始数
int c=str1.indexOf("i");
System.out.println("查找结果为"+c);    //查不到"i",输出结果为-1
    
int d=str1.lastIndexOf("h");    
System.out.println("查找结果为"+d);    //lastIndexOf("h")从后往前查

//获取字符串
str1.substring(4, 7);
String str2 = str1.substring(4, 7);    
System.out.println("截取结果为"+str2);

//判断相等
str1.equals(str2);

//分割 split
String str3="abc#def#ghi#jkl";
str3.split("#");
String [] array = str3.split("#");
for(int i=0;i<array.length;i++)
System.out.println("截取结果为"+array[i]);//遍历数组


//包装类
String e = "1234";

Integer f = new Integer(e);
f.parseInt(e);//将字符串类转为整数类
f.valueOf(e);        
        
System.out.println(Integer.MAX_VALUE);        
//ArrayList集合
ArrayList<String>str4=new ArrayList<String>();
str4.add("abc");
str4.add("bcd");        
str4.add("cde");        
str4.add("def");        
        
System.out.println(str4);        
//修改  (索引值,字符串)        
str4.set(0, "bbb");        
System.out.println(str4);            
str4.contains("bbb");
//查询字符串是否在集合里 boolean型
System.out.println(str4.contains("bbb"));
//取字符串
System.out.println(str4.get(2));
//集合里的字符串个数  size
System.out.println(str4.size());

//遍历集合  
for(int n=0;n<str4.size();n++){
System.out.println("遍历集合" + str4.get(n));}
//foreach循环
for(String st:str4)
{System.out.println("遍历集合" + str4);}

//HashMap集合  键值对
HashMap<String,String>str5=new HashMap<String,String>();
str5.put("1", "济南");
str5.put("2", "青岛");
str5.put("3", "淄博");
//遍历集合  要用HashSet
Set<String> h = str5.keySet();
for(String strh: h ){
    System.out.println(strh+"="+str5.get(strh));}
}}

    
package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Stringlei {

    public static void main(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 = new String(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<String> al = new  ArrayList<String>();//<>里面要放包装类
        //放数据
        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<al.size();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<String,String> hm = new HashMap<String,String>();
        
        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<String> h = hm.keySet();
        for(String strh: h ){
            System.out.println(strh+"="+hm.get(strh));
        }
        
        
        //HashSet
        HashSet<String> hs =new HashSet<String>();
        hs.add("ac");
        
     
        
        
       TestThread tt = new TestThread();
       tt.start();//启动多线程
       TestThread tt2 = new TestThread();
       tt2.start();
       TestThread tt3 = new TestThread();
       tt3.start();
    }

}

 

posted on 2015-12-20 11:13  Chen_s  阅读(312)  评论(0编辑  收藏  举报

导航