常用类

本章内容:
  1.字符串相关类(String StringBuffer)
  2.基本数据类型包装类
  3.Math类
  4.File类
  5.枚举类
 
String : java.lang.String 类代表不可变的字符序列.
  "xxxx" 为该类的对象的方法:API
  String(byte[] bytes)  :把一种编码格式转成另一种编码格式.
  常用构造方法和不可变equals理解:示例
  public class DomeString1 {


public static void main(String[] args) {
     String str1 = "hello";
     String str2 = "hello";
     System.out.println(str1==str2);//true
     
     String str3 = new String("hello");
     String str4 = new String("hello");
     System.out.println(str3 == str4);//false
     System.out.println(str3.equals(str4));//true
     
     char[] ca = {'h','e','l','l','o','j','a','v','a'};
     String str5 = new String(ca);
     String str6 = new String(ca ,4,4);
     System.out.println(str5);
     System.out.println(str6);
}
 }


类常用方法:
  public char charAt(int index)
  public int length()
  public int indexOf(String str)
  public int indexOf(String str,int fromIndex)
  public boolean equalsIgnoreCase(String another)
  public String replace(char oldChar,char newChar)
  public boolean startWith(String prefix)
  public boolean endsWith(String suffix)
  public String toUpperCase()
  public String toLowrCase()
  public String substring(int beginIndex)
  public String substring(int beginIndex,int endIndex)
  public String trim()
  
示例程序1:
 public class DomeString2 {


public static void main(String[] args) {
    String str1 = "sun java";
    String str2 = "Sun Java";
    System.out.println(str1.charAt(2)); //n
    System.out.println(str1.length());//8
    System.out.println(str1.indexOf("java"));//4
    System.out.println(str1.indexOf("Java"));//-1
    System.out.println(str1.equals(str2));//false
    System.out.println(str1.equalsIgnoreCase(str2));//true
    
    String s ="我是程序员,我在学java";
    String sr = s.replace("我","你");
    System.out.println(sr);//你是程序员你在学java
}
}
 
静态重载方法: 
  1.public static String valueOf(Object obj)可以把基本数据类型转换成字符串类型。
    把作何对象都当成object对象来看然后再调用toString方法调用的什么类型就用相应toString方法
    这里有多态存在。
    
  2.String[] split(String regex) 
          根据给定正则表达式的匹配拆分此字符串。 
          
两个练习: 
   1.string str = "saifndfsalDIFSNFSA@#$!SAIFMDSASINFSA模snfsaffna";找出其中的大写字母小写字母和非字母个数。
   思路:1.用assci码比较: 2.用character的包装类里的方法. isLowerCase() isUpCase()
   2.求一个字符中出现一个字符串的个数。
   
StringBuffer类:
  java.lang.StringBuffer 代表可变的字符序列:
  StringBuffer和String类似,但StringBuffer可以对字符进行修改。
  str1 += str2 的过程是把两个字符串都拷到一个新String 里再把str1的引用手指向新的。
  
  重载方法: public StringBuffer append(....)可以为该StringBuffer
             添加对象,返回添加后的该对象的引用 。
  public StringBuffer append(String str)
  public StringBuffer append(StringBuffer sbuf)
  public StringBuffer append(char[] str)
  public StringBuffer append(char[] str ,int offset,int len)
  public StringBuffer append(double d)
  public StringBuffer append(Object obj)
  public StringBuffer insert(int offset,String str)
  public StringBuffer delete(int start ,int end)
 
 方法应用示例:
   public class DomeStringBuffer {


public static void main(String[] args) {
    String str1 = "hello";
    char[] a ={'a','b','c'};
    StringBuffer sb1 = new StringBuffer(str1);
    sb1.append("/").append("world");
    System.out.println(sb1);
    
    StringBuffer sb2 = new StringBuffer("数字");
    for(int i=0;i<9;i++){
    sb2.append(i);
    }
    System.out.println(sb2);
    sb2.delete(4,sb2.length()).insert(0, a);
    System.out.println(sb2);
    System.out.println(sb2.reverse());
}
}


基础数据类型的包装类: 
  包装类: (如Integer ,Double等)这些类封闭了一个相应的基本数据值。并提供了一系列的操作。
  练习:把一个String str = "1,2;3,4;5,6"放到一个double类型的二维数组里。
  public class DomeDouble {
public static void main(String[] args) {
String str = "1,2;3,4;5,6";
        String[] strarr=str.split(";");
        String[] strarr2;
        double[][] arr2 = new double[strarr.length][];
        for(int i=0;i<strarr.length;i++){
        strarr2 = strarr[i].split(",");
        arr2[i] = new double[strarr2.length];
        for(int j=0;j<strarr2.length;j++){
        arr2[i][j] = Double.parseDouble(strarr2[j]);
        System.out.print(arr2[i][j]+ " ");
        }
        System.out.println(" ");
        }
}
}


java.lang.Math提供了一系列静态方法用于计算:
   abs/acos/asin/atan/cos/sin/tan
   sqrt(平方根)/pow(int a,int b)//a 的 b 次方幂。
   log 自然对数。
   exp e 为底指数。
   max(double a ,double b)
   min(double a, double b)
   random()返回0.0到1.0的随机数。
   long round(double a) double型的数据a转换为long型 。(四舍五入)
   toDegrees(double angrad)
   toRadians(double angdeg)
   public static double ceil(double a)//向下取整
   public static double floor(double a)//向上取整
   public static double rint(double a)//最近最整
  
  示例:
  public class DomeMath {
public static void main(String[] args) {
double a = Math.random();
double b = Math.random();
System.out.println(Math.sqrt(a*a+b*b));
System.out.println(Math.pow(a, 8));
System.out.println(Math.round(b));
System.out.println(Math.log(Math.pow(Math.E, 5)));
double d = 60.0 ,r = Math.PI/4;
System.out.println(Math.toDegrees(r));
System.out.println(Math.toRadians(d));
}
}


File类:
  java.io.File类代表系统“文件名”:(路径和文件名)。
  常见构造方法:
    public File(String pathname)在内存里创建的。
    以pathname为路径创建File对象,如果pathname是相对路径,刚默认的当前
    路径在系统属性user.dir中存储。
    
    public File(String parent,String child)
    以parent为父路径,child为子路径创建file对象。
    
    File的静态属性String separator存储了当前系统的路径分隔符。
    在WIN系统里分隔符为"\",在Linux里是"/"(这个两个系统 都支持)
    
    public boolean canRead()
    public boolean canWrite()
    public boolean exists()
    public boolean isDirectory();
    public boolean isFile()
    public boolean isHidden()
    public long lastModified()//上次修改的时间
    public long length()//文件的大小
    public String getName()
    public String getPath()
  通过File对象创建空文件或者目录(在该对象所指的文件或者目录不存在的情况下)
    public boolean creatNewFile() throws IOException
    public boolean delete()
    public boolean mkdir()
    public boolean mkdirs()//创建在路径中的一系列目录。
 示例小程序:
  public class DomeFile {


public static void main(String[] args) {
String directory = "mydir1" + File.separator + "mydir2";
String filename = "my.txt";
File f = new File(directory,filename);
if(f.exists()){
System.out.println("文件名:" + f.getAbsolutePath());
System.out.println("文件大小:" + f.length());
} else{
f.getParentFile().mkdirs();
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
注意问题: 1.如果说程序在带包的情况下找到的父路径是先找到包再找包的父路径。
           2.创建目录时,一系列的路径一定要是mkdirs。
练习:打印出目录结构树状展现:
  public class DomeTree {


public static void main(String[] args) {
      File f = new File("D:/a");
      System.out.println(f.getName());
      tree(f,1);
}


public static void tree(File f, int step){
String prestr = "";
for(int i=0;i<step;i++){
prestr += "   ";
}

File[] childs = f.listFiles();
for(int i=0;i<childs.length;i++){
System.out.println(prestr + childs[i].getName());
if(childs[i].isDirectory()){
tree(childs[i],step+1);
}
}
}
}


枚举类型:java.lang.Enum(如果一个错误要产生一个最好先产生)
 1.枚举类型: 只能够取特定值中的一个。
 2.使用enum关键字。(相当于一个类,只取取里面类型于静态的成员变量)
 3.是java.lang.Enum
 示例:(固定写法注意书写的格式)
 public class DomeEnum {
   public enum MyColor {red,black,green};
   public static void main(String args[]){
  MyColor m = MyColor.red;
  switch(m){
  case red:
  System.out.println("red");
  break;
  case black:
  System.out.println("black");
  break;
  case green:
  System.out.println("green");
  break;
  }
  System.out.println(m);
   }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值