`

java常用类

阅读更多
常用类
1. 字符串相关类(String、StringBuffer)
1.1 String类
1.1.1 java.lang.String类代表不可变的字符序列
1.1.2 "abcde" 为该类的一个对象
1.1.3 String类的常见构造方法:
String(String original) //创建一个String对象为original的拷贝
String(char[] value) //用一个字符数组创建一个String对象
String(char[] value,int offset,int count)//用一个字符数组从offset项开始的count个字符序列创建一个
                                                //String对象
1.1.4 example1
public class StudyString {
public static void main(String[] args) {
String s1 = "金刚葫芦娃";
String s2 = "人参娃";
String s3 = "金刚葫芦娃";
System.out.println(s1 == s3); //true

String s4 = new String("金刚葫芦娃");
String s5 = new String("金刚葫芦娃");
System.out.println(s4 == s5); //false
System.out.println(s4.equals(s5)); //true

char song[] = {'n','e','v','e','r',' ','s','a','y',' ',
                                                                 'g','o','o','d','b','y','e'};
String s6 = new String(song);
String s7 = new String(song, 10, 4);
System.out.println(s6); //never say goodbye
System.out.println(s7); //good

}
}
1.1.5 String类常用方法
public char charAt(int index) //返回字符串中第index个字符
public int length() 返回字符串的长度
public int indexOf(String str) //返回字符串中出现str的第一个位置
public int indexOf(String str, int fromIndex) //返回字符串中从fromIndex开始出现str的第一个位置
public boolean equalsIgnoreCase(String another) //比较字符串与another是否一样(忽略大小写)
public String replace(char oldChar, char newChar)//在字符串中用newChar字符替换oldChar字符

public boolean startsWidth(String prefix) //判断字符串是否以prefix字符串开头
public boolean endsWidth(String suffix) //判断字符串是否以suffix字符串结尾
public String toUpperCase() //返回一个字符串为该字符串的大写形式
public String toLowerCase() //返回一个字符串为该字符串的小写形式
public String substring(int beginIndex) //返回该字符串从beginIndex开始到结尾的字符串
public String substring(int beginIndex,int endIndex)//返回从beginIndex开始到endIndex结尾的子字符串
public String trim() //返回将该字符串去掉开头和结尾空格的字符串

//静态重载方法
public static String valueOf(...)        //可以将基本类型数据转换为字符串

public String[] split(String regex) //可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
1.1.6 example2
public class StudyString {
public static void main(String[] args) {
String s1 = "you jump,I jump";
String s2 = "You Jump,I Jump";
System.out.println(s1.charAt(1)); //o
System.out.println(s1.length()); //15
System.out.println(s1.indexOf("jump")); //4
System.out.println(s1.indexOf("Jump")); //-1
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equalsIgnoreCase(s2)); //true

String s3 = s1.replace("jump","not jump");
System.out.println(s3); //you not jump,I not jump
}
}
1.2 StringBuffer类
1.2.1 java.lang.StringBuffer代表可变的字符序列
1.2.2 StringBuffer和String类似,但StringBuffer可以对其字符串进行改变
1.2.3 StringBuffer类的常见的构造方法:
StringBuffer() 创建一个不包含字符序列的"空"的StringBuffer对象
StringBuffer(String str) 创建一个StringBuffer对象,包含与String对象str相同的字符序列
1.2.4 StringBuffer常用方法
//重载方法public StringBuffer append(...) 可以为该StringBuffer 对象添加字符序列,
//返回添加后的该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(...) 可以为该StringBuffer对象在指定位置插入字符序列,
//返回修改后的该StringBuffer对象引用
public StringBuffer insert( int offset, String str)
public StringBuffer insert( int offset double d)

//delete方法可以删除从start开始到end-1为止的一段字符序列,返回修改后的该StringBuffer对象的引用
public StringBuffer delete(int start,int end)

1.2.5 examle3
public class StudyStringBuffer {
public static void main(String[] args) {
String s = "C:\Program Files";
char[] c = {'p','h','o','n','e'};
StringBuffer sb1 = new StringBuffer(s);
sb1.apped('\').append("easyMule");
System.out.println(sb1); //C:\Program Files\easyMule

StringBuffer sb2 = new StringBuffer("海豚");
for(int i=0; i<=9; i++) {
sb2.append(i);
}
System.out.println(sb2); //海豚0123456789

sb2.delete(8,sb2.length()).insert(0,a);
System.out.println(sb2); //phone海豚012345
System.out.println(sb2.reverse()); //543210豚海enohp
}
}

2. 基本数据类型包装类
2.1 包装类(如: Integer, Double等)这些类封装了一个相应的基本数据类型数值,并为其提供了一系列操作。
2.2 以java.lang.Integer类为例;构造方法:
Integer(int value)
Integer(String s)
2.3 以下是java.lang.Integer的常用方法
public static final int MAX_VALUE //最大的int型数(2^31-1)
public static final int MIN_VALUE //最小的int型数(-2^31)
pubic long longValue() //返回封装数据的long型值
public double doubleValue() //返回封装数据的double型值
public int intValue() //返回封装数据的int型值
public static int parseInt(String s) throws NumberFormatException //将字符串解析成int型数据,返回该数据
public static Integer valueOf(String s) throws NumberFormatException// 返回Integer对象, 其中封装的
整型数据为字符串s表示
3. Math类
3.1 jav.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型(四舍五入)
toDegrees(double angrad) //弧度->角度
toRadians(double angdeg) //角度->弧度

4. File类
4.1 java.io.File类代表系统文件名(路径和文件名).
4.2 File类的常见构造方法:
4.2.1 public File(String pathname)
以pathname为路径创建File对象,如果pathname是相对路径,则默认的当前路径在系统
属性user.dir中存储
4.2.2 public File(String parent,String child)
以parent为父路径,child为子路径创建File对象
4.3 File的静态属性String separator存储了当前系统的路径分隔符
4.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()
4.5 通过File对象创建空文件或目录(在该对象所指的文件或目录不存在的情下)
public boolean createNewFile() throws IOException
public boolean delete()
public boolean mkdir()
public boolean mkdirs() //创建在路径中的一系列目录

5. 枚举类
5.1 枚举类型:
只能够取特定值中的一个
使用enum关键字
是java.lang.Enum类型
5.2 枚举类型的定义和调用
public enum MyGrade { one, two, three, four };
MyGrade g = MyGrade.one; //枚举类型的调
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics