Java总结之常用类

来源:互联网 发布:打车软件推广方案 编辑:程序博客网 时间:2024/04/29 19:34
【常用类】
字符串相关类(String、StringBuffer)
基本数据类型包装类
Math类
File类
枚举类
【String类】
java.lang.String类代表不可变的字符序列。
"xxxxx"为该类的一个对象。
String类的常见构造方法:
String(String original)
  创建一个String对象为original的拷贝。
String(char[] value)
  用一个字符数组创建一个String对象
String(char[] value,int offset,int count)
  用一个字符数组从offset项开始的count个字符序列创建一个String对象
【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 startsWith(String prefix) 判断字符串是否以prefix字符串开头
public boolean endsWith(String suffix) 判断字符串是否以prefix字符串结尾
public String toUpperCase() 返回一个字符串为该字符串的大写形式
public String toLowerCase() 返回一个字符串为该字符串的小写形式
public String substring(int beginIndex) 返回该字符串从beginIndex开始到endIndex结尾的子字符串
public String trim() 返回该字符串去掉开头和结尾空格后的字符串
静态重载方法:
  public static String valueOf(...) 可以讲基本数据类型转换为字符串
  例如:public static String valueOf(double d)
方法public String[] split(String regex)可以讲一个字符串按照指定的分隔符,返回分隔后的字符串数组。
【StringBuffer类】
java.lang.StringBuffer代表可变的字符序列。
StringBuffer和String类似,但StringBuffer可以对其字符串进行改变。
StringBuffer类的常见构造方法:
  StringBuffer() 创建一个不包含字符序列的“空”的StringBuffer对象。
  StringBuffer(String str) 创建一个StringBuffer对象,包含与String对象str相同的字符序列。
【StringBuffer常用方法】
重载方法public StringBuffer append(...)可以为该StringBuffer对象添加字符序列,返回添加后的该StringBuffer


对象引用,例如:
public StringBuffer append(String str)
public StringBuffer append(StringBuffer sbuf)
public StringBuffer apend(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)
方法public StringBuffer delete(int start,int end)可以删除从start开始到end-1为止的一段字符序列,返回修改


后的该StringBuffer对象引用。
和String类含义类似的方法:indexOf,subString,length
方法public StringBuffer reverse()用于将字符序列逆序,返回修改后的StringBuffer对象引用。
【基础数据类型包装类】
包装类(如:Integer,Double等)这些类封装了一个相应的基本数据类型数值,并为其提供了一系列操作。
以java.lang.Integer类为例;构造方法:
Integer(int value)
Integer(String s)
【包装类常见方法】
以下方法以java.lang.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 Integer valueOf(String s) throws NumberFormatException
返回Integer对象,其中封装的整型数据为字符串s所表示。
【Math类举例】
public class Test {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,15)));double d = 60.0 , r = Math.PI / 4;System.out.println(Math.toRadians(d));System.out.println(Math.toDegrees(r));}}


【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型(四舍五入)
toDegrees(double angrad) 弧度->角度
toRadians(double angdeg) 角度->弧度
【File类】
java.io.File类代表系统文件名(路径和文件名)。
File类的常见构造方法:
public File(String pathname)
 以pathname为路径创建File对象,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
public File(String parent,String chile)
 以parent为父路径,child为子路径创建File对象。
File的静态属性String separator存储了当前系统的路径分隔符。
【File类常用放阿福】
通过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 Strign getPath()
通过File对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)。
public boolean createNewFile() throws IOException
public boolean delete()
public bolean mkdir()
public boolean mkdirs() //创建在路径中的一系列目录
【File类举例】
import java.io.*;public class Test {public static void main(String[] args) {String separator = File.separator;String filename = "myfile.txt";String directory = "mydir1" + separator + "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();}}}}


【Java递归列出目录结构】
import java.io.*;

public class FileList {public static void main(String[] args) {File f = new File("d:/A");System.out.println(f.getName());tree(f , 1);}private static void tree(File f,int level) {String preStr = "";for(int i=0;i<level;i++) {preStr += "";}File[] child = f.listFiles();for(int i=0;i<child.length;i++) {System.out.println(preStr + child[i].getName());if(child[i].isDirectory()) {tree(child[i] , level+1);}}}}



【Enum】
java.lang.Enum枚举类型
枚举类型:
只能去特定之中的一个
使用enum关键字
是java.lang.Enum类型
0 0
原创粉丝点击