常用类

常用类
1.字符串相关类(String、StringBuffer)
String类的常用方法:
public char charAt(int index);
返回字符串中第index个字符
public int length();
返回字符串的长度
public int indexOf(String str);
返回字符串中出现str的第一个位置
public int indexOf(String str,int fromIndex);
返回字符串中从fromIndex开始的第一个位置
public boolean equalsIgnoreCase(String another);
比较字符串与another是否一样(忽略大小写)
public String replace(char oldChar,char newChar);
在字符串中用newChar字符替代oldChar字符
public boolean startsWith(String prefix);
判断字符串是否以prefix字符串开头
public boolean endsWith(String suffix);
判断字符串是否以prefix字符串结尾
public String toUpperCase();
返回一个字符串为该字符串的大写形式
public String toLowerCase();
返回一个字符串为该字符串的小写形式
public String substring(int beginIndex);
返回该字符串从beginIndex开始到结尾的子字符串
public String substring(int beginIndex,int endIndex);
返回该字符串从beginIndex开始到endIndex结尾的子字符串
public String trim();
返回将该字符串去掉开头和结尾空格后的字符串

例1

public class Test{
 public static void main(String[] args){
  String s1 = "hello";
  String s2 = "world";
  String s3 = "hello";
  System.out.println(s1==s3);//true
  
  s1 = new String("hello");
  s2 = new String("hello");
  System.out.println(s1==s2);//false
  System.out.println(s1.equals(s2));//true; equals已经重写了s1,s2
  
  char c[] = {'s','u','n',' ','j','a','v','a'};
  String s4 = new String(c);
  String s5 = new String(c,4,4);//从第四个字符开始往后截取四个字符
  System.out.println(s4);//sun java
  System.out.println(s5);//java
 }
}

输出结果:

true
false
true
sun java
java

例2:

public class Test{
 public static void main(String[] args){
  String s1 = "sun java";
  String s2 = "Sun Java";
  System.out.println(s1.charAt(1));//u
  System.out.println(s2.length());//8; 8个字符
  System.out.println(s1.indexOf("java"));//4; 出现“java”的第一个位置是4
  System.out.println(s1.indexOf("Java"));//-1; s1没有“Java”,返回-1
  System.out.println(s1.equals(s2));//false
  System.out.println(s1.equalsIgnoreCase(s2));//true; equalsIgnoreCase忽略大小写后判断是否相等
  
  String s = "我是程序员,我在学java";
  String sr = s.replace('我','你');//把s中的“我”用“你”替换
  System.out.println(sr);//你是程序员,你在学java
 }
}

输出结果:

u
8
4
-1
false
true
你是程序员,你在学java

例3:

public class Test{
 public static void mian(String[] args){
  String s = "Welcome to Java World!";
  String s1 = "   sun java   ";
  System.out.println(s.startsWith("Welcome"));//true
  System.out.println(s.endsWith("World"));//false
  String sL = s.toLowerCase();//转换成小写
  String sU = s.toUpperCase();//转换成大写
  System.out.println(sL);//welcome to java world!
  System.out.println(sU);//WELCOME TO Java WORLD!
  String subS = s.substring(11);//返回字符串从第11个字符开始到结尾的子字符串
  System.out.println(subS);//Java World!
  String sp = s1.trim();//去掉字符串前后空格
  System.out.println(sp);//sun java
 }
}

输出结果:

true
false
welcome to java world!
WELCOME TO JAVA WORLD!
Java World!
sun java

(1)静态重载方法:
public static String valueOf(…)可以将基本类型数据转换为字符串;
例如: public static String valueOf(double d);
public static String valueOf(int i);
(2)方法public String[] split(String regex)可以将一个字符串按照指定的分隔符分割,返回分割后的字符串数组。

例4:

public class Test{
 public static void mian(String[] args){
  int j = 1234567;
  String sNumber = String.valueOf(j);//将基本类型数据转换为字符串
  System.out.println("j 是" + sNumber.length() + "位数");
  String s = "Mary,F,1976";
  String[] sPlit = s.split(",");//将字符串s按照指定的分隔符","分割
  for(int i=0;i<sPlit.length;i++){
   System.out.println(sPlit[i]);
  }
 }
}

输出结果:

j 是7位数
Mary
F
1976

例5:

public class Test{
 public static void main(String[] agrs){
  String s = "jiavsdsjavadafjavafjaijvavjavajava";
  String sToFind = "java";
  int count=0;
  int index=-1;
  while((index = s.indexOf(sToFind)) != -1) {
   s = s.substring(index + sToFind.length());
   count++;
  }
  System.out.println(count);
 }
}

输出结果:

4

2.StringBuffer类 ——缓冲区
(1)java.lang.StringBuffer; 代表可变的字符序列。
(2)StringBuffer和String类似,但StringBuffer可以对其字符串进行改变。
(3)StringBuffer类的常见构造方法:
(a)StringBuffer()
创建一个不包含字符序列的“空”的StringBuffer对象。
(b)StringBuffer(String str)
创建一个StringBuffer对象,包含与String对象str相同的字符序列。
(4)常用方法
(a)重载方法public StringBuffer append(…)可以为该StringBuffer 对象添加字符序列,返回添加后的该StringBuffer对象引用,例如:
public StringBuffer append(String str);
添加另外一个字符串
public StringBuffer append(StringBuffer sbuf);
添加另外一个StringBuffer
public StringBuffer append(char[] str);
添加另外一个字符串数组
public StringBuffer append(char[] str,int offset,int len);
添加另外一个字符串数组的一部分
public StringBuffer append(double d);
添加一个double数,把double转成字符串后添加在后面
public StringBuffer append(Object obj);
添加一个Object(调用到toString方法)

(b)重载方法public StringBuffer append(…)可以为该StringBuffer 对象在指定位置插入字符序列,返回修改后的该StringBuffer对象引用,例如:
public StringBuffer insert(int offsert,String str);
public StringBuffer insert(int offsert,double d);

(c)方法public StringBuffer delete(int start,int end);可以删除从start开始到end-1为止的一段字符序列,返回修改后的该StringBuffer对象引用。
(d)和String类含义类似的方法:
public int indexOf(String str);
public int indexOf(String str,int fromImdecx);
public String substring(int start);
public String substring(int start,int end);
public int();
(e)方法public StringBuffer reverse();用于将字符序列逆序,返回修改后的该StringBuffer对象引用。

例子:

public class Test{
 public static void main(String[] args){
  String s = "Year";
  char[] a = {'a','b','c'};
  //添加一
  StringBuffer sb1 = new StringBuffer(s);
  sb1.append('/').append("Month").append('/').append("Day");
  System.out.println(sb1);
  //添加二
  StringBuffer sb2 = new StringBuffer("数字");
  for(int i=0;i<=9;i++){
   sb2.append(i);
  }
  System.out.println(sb2);
  //指定位置修改
  //delete删除从第8个字符(在字符串中从0开始数)开始到sb2.length()为止的一段字符序列;insert在指定位置0插入字符串a
  sb2.delete(8,sb2.length()).insert(0,a);
  System.out.println(sb2);
  //返回逆序列
  System.out.println(sb2.reverse());//reverse逆序
 }
}

输出结果:

Year/Month/Day
数字0123456789
abc数字012345
543210字数cba

3.基本数据类型包装类
(1)包装类(如:Integer,Double等)这些类封装了一个相对应的基本数据类型数值,并为其提供了一系列操作。
(2)以java.leng.Integer类为例,构造方法:
(a)Intger(int value);
(b)Intger(String s);

以下方法以java.leng.Integer类为例:
public static final int MAX_VALUE;
最大的int型的数(2^31-1)
public static final int MIN_VALUE;
最小的int型的数(-2^31)
public long longValue();
返回封装数据的long型的值
public double doubleValue();
返回封装数据的double型的值
public int intValue();
返回封装数据的int型的值
public static int parseInt(String s) throws NumberFormatException;
将字符串解析成int型的数据,返回该数据
public static Intger valueOf(String s) throws NumberFormatException;
返回Intger对象,其中封装的整型数据为字符串s所表示

例:

public class Test{
 public static void main(String[] args){
  Integer i = new Integer(100);
  Double d = new Double("123.456");
  int j = i.intValue()+d.intValue();//intValue()返回封装数据的int型的值
  float f = i.floatValue()+d.floatValue();//floatValue()返回封装数据的float型的值
  System.out.println(j);
  System.out.println(f);
  double pi = Double.parseDouble("3.1415926");//parseDouble将字符串解析成double型的数据,返回该数据
  //valueOf返回Intger对象,其中封装的整型数据为字符串“2.0”所表示;doubleValue返回封装数据的double型的值
  double r = Double.valueOf("2.0").doubleValue();
  double s = pi*r*r;
  System.out.println(s);
  try{
   int k = Integer.parseInt("1.25");
  }catch(NumberFormatException e){
   System.out.println("数据格式不对!");
  }
  System.out.println(Integer.toBinaryString(123)+"B");//二进制
  System.out.println(Integer.toOctalString(123)+"O");//八进制
  System.out.println(Integer.toHexString(123)+"H");//十六进制
 }
}

输出结果:

223
223.456
12.5663704
数据格式不对!
1111011B
173O
7bH

练习题:
编写一个方法,返回一个double型二维数组,数组中的元素通过解析字符串参数获得。
如字符串参数:“1,2;3,4,5;6,7,8”
对应的数组为:
d[0,0]=1.0 d[0,1]=2.0
d[1,0]=3.0 d[1,1]=4.0 d[1,2]=5.0
d[2,0]=6.0 d[2,1]=7.0 d[2,2]=8.0

public class ArrayParser{
 public static void main(String[] args){
  double[][] d;
  String s = "1,2;3,4,5;6,7,8";
  String[] sFrist = s.split(";");
  d = new double[sFrist.length][];
  for(int i=0;i<sFrist.length;i++){
   String[] sSecond = sFrist[i].split(",");
   d[i] new double[sSecond.length];
   for(int j=0;j<sSecond.length;j++){
    d[i][j] = Double.parseDouble(sSecond[j]);
   }
  }
  for(int i=0;i<d.length;i++){
   for(int j=0;j<d[i].length;j++){
    Sstem.out.print(d[i][j] + " ");
   }
   System.out.println();
  }
 }
}

输出结果:

1.0 2.0 
3.0 4.0 5.0 
6.0 7.0 8.0 

4.Math类
java.lang.Math提供了一系列静态方法用于科学计算,其方法的参数和返回值类型一般为double型。
abs 绝对值
acos,asin,atan,cos,sin,tan 三角函数
sqrt 平方根
pow(double a,double 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型(四舍五入)
toDegree(double angrad) 弧度–>角度
toRadians(double angdeg) 角度–>弧度

例:

public class MathTest{
 public static void main(String[] args){
  double a = Math.random();//生成0.0-1.0的随机数a
  double b = Math.random();//生成0.0-1.0的随机数b
  System.out.println(Math.sqrt(a*a+b*b));//求平方根
  System.out.println(Math.pow(a,8));//a的8次幂
  System.out.println(Math.round(b));//对b四舍五入
  System.out.println(Math.log(Math.pow(Math.E,15)));//Math.E 静态常量
  double d = 60.0;
  double r = Math.PI/4;
  System.out.println(Math.toRadians(d));
  System.out.println(Math.toDegrees(r));
 }
}

输出结果:

0.438551915210369
1.0641617623749604E-5
0
15.0
1.0471975511965976
45.0

5.File类
(1)java.io.File类代表系统文件名(路径和文件名)。
(2)File类的常见构造方法:
(a)public File(String pathname)
以pathname为路径创建File对象,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中储存。
(b)public File(String parent,String child)
以parent为父路径,child为子路径创建File对象。
(3)File 的静态属性String separator储存了当前系统的路径分隔符。
(4)通过File对象可以访问文件的属性。
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();
获得文件路径
(5)通过File对象创建文件或目录(在该对象所指的文件或目录不存在的情况下)。
public boolean createNewFile()throws IOException
public boolean dslete();
删除
public boolean mkdir();
创建一个路径
public boolean mkdirs();
创建在路径中的一系列目录

例:

import java.io.*;
public class TestFile{
 public static void main(String[] args){
  String separator = File.separator;
  String filename = "myfile.txt";//定义
  String directory = "mydir1" + separator + "mydir2";//定义路径,separetor路径分隔符
  //String directory = "mydir1/mydir2";

//反斜杠'\'在java中为转义字符,所以用两个反斜杠'\\'代表一个反斜杠'\',但是该写法在Linux系统下无法实现,因此不推荐该写法
//(补充:正斜杠'/'在window系统和Linux系统都能实现分隔)
  //String directory = "mydir1\\mydir2"

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

注意:
当类位于包中的时候,先找包再找类!

6.枚举类
枚举类型
(a)只能够取特定值中的一个
(b)使用enum关键字
(c)时java.lang.Enum类型

例:

public class TestEnum{
 public enum MyColor{red,green,blue};
 public static void main(){
  MyColor m = MyColor.red;
  switch(m){
   case red:
    System.out.println("red");
    break;
   case green:
    System.out.println("green");
    break;
   default:
    System.out.println("default");
  }
  System.out.println(m);
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值