您的位置:首页 > 编程语言 > Java开发

(JAVA自学笔记)随笔

2013-01-17 13:29 316 查看
day:20130117(上班时间,没事总结一下)

String常用方法如下:

public int length();//获得字符串长度

public int indexOf(int ch,int fromIndex);//查找字符

public int lastIndexOf(int ch,int fromIndex);//逆向查找

public boolean startsWith(String prefix);//判断开始子串

public boolean endsWith(String suffix);//判断结束子串

public char charAt(int index);//根据索引得到字符串

public int compareTo(String str);//字符串比较

public boolean equalsIgnoreCase(String str);//判断是否相等

public boolean equals(Object obj);//判断是否等值

public String concat(String str);//连接字符串

实例:

StringDemo.java:

public class StringDemo{

public static void main(String[] args){

String myName = "G.leeman";

int length = "Best Wishes!".length();

System.out.println("length 1:" + myName.length());

System.out.println("length 2:" + length);

//访问字符串长度

String name1 = "programming in java";

String name2 = "Programming in Java";

//比较字符串大小

System.out.println("compare 1:" + name1.equals(name2));

System.out.println("compare 2:" + name1.equalsIgnoreCase(name2));

System.out.println("compare 3:" + name2.compareTo("Program"));

//访问字符串中的字符

System.out.println("char 1:" + name1.charAt(4));

System.out.println("char 2:" + name1.indexOf('a'));

System.out.println("char 3:" + name2.lastIndexOf('a'));

//访问字符串中的子串

String subname = "in";

System.out.println("substring 1:" + name1.substring(3,10));

System.out.println("substring 2:" + "abc".concat("123"));

System.out.println("substring 3:" + name2.startsWith("Pro"));

System.out.println("substring 4:" + name2.endsWith("in Java"));

System.out.println("substring 5:" + name1.indexOf(subname));

System.out.println("substring 6:" + name1.lastIndexOf(subname));

//字符串的其他操作

System.out.println("Lower write:" + name2.toLowerCase());

System.out.println("Upper write:" + name2.toUpperCase());

System.out.println("replace:" + name1.replace('a','A'));

}

}

输出:

length 1:8

length 2:12

compare 1:false

compare 2:true

compare 3:12

char 1:r

char 2:5

char 3:18

substring 1:grammin

substring 2:abc123

substring 3:true

substring 4:true

substring 5:8

substring 6:12

Lower write:programming in java

Upper write:PROGRAMMING IN JAVA

replace:progrAmming in jAvA

String对象表示的是不可更改的字符串对象,如果需要修改String对象所表示的内容,必须重新创建一个对象:

String str = "Petter";

str = str + "&Bob" + "&Tom";

当修改操作频繁,或字符串的值很大时,会额外分配大量内存。因此,java语言引入了一个StringBuffer类,用来表示内容可以扩充和修改字符串对象。StringBuffer是一个线程安全的可变字符序列。一个类似于String的字符串缓冲区,但不能改变。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。

可将字符串缓冲区安全的用于多个线程。可以在必要时对这些方法进行同步,因此特定实例上的所有操作就好像是以串行顺序发生的,该顺序与所涉及的每个线程进行的方法调用顺序一致。

StringBuffer上的主要操作是append和insert方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效的将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串缓冲区中。append方法始终将这些字符添加到缓冲区的末端;而insert方法则在指定的点添加字符。

定义可修改的字符串,需要 根据StringBuffer类的构造函数创建字符串变量。

public StringBuffer();

public StringBuffer(int length);

public StringBuffer(String str);

可根据StringBuffer类提供的方法操作字符串对象:

public int length()-------得到当前StringBuffer的长度。

public int capacity()-----得到当前StringBuffer的容量。

public synchronized void setCharAt(int index,char ch)------设置位置index的字符为ch。

public synchronized StringBuffer append(String str)------将字符串添加到StringBuffer中

public synchronized StringBuffer delete(int start,int end)-----删除start和end之间的字符。

public sysnchronized replace(int start,int end,String str)----用字符串str代替start和end之间的字符。

public sysnchronized StringBuffer insert(int offset,char c)-----用于向StringBuffer中插入字符。

实例:

StringBufferDemo.java:

public class StringBufferDemo{

public StringBufferDemo(){

}

public static void main(String[] args){

StringBuffer sb = new StringBuffer("Java SE");

double d = 6;

sb.append(d).append(" have").append(new String(" a lot of new features"));

System.out.println(sb);

sb.insert(4,"SDK");

System.out.println(sb);

sb.delete(4,10);

System.out.println(sb.reverse());

}

}

输出:

Java SE6.0 hava a lot of new features

JavaSDK SE6.0 have a lot of new features

serutaef wen of tol a evah 0.6avaJ

Math类用来完成一些常用的数学计算,它提供了若干实现不同标准数学函数的方法,这些方法都是static的类方法。Math类所有运算都以double进行。

举例:

MathDemo.java:

public class MathDemo{

public static void main(String[] args){

System.out.println("abs(-5) = " + Math.abs(-5));

System.out.println("max(2.72,3.14) = " + Math.max(2.72,3.14));

System.out.println("min(256,285) = " + Math.min(256,285));

System.out.println("round(3.8) = " + Math.round(3.8));

System.out.println("round(-3.8) = " + Math.round(-3.8));

System.out.println("sqrt(2) = " + Math.sqrt(2));

System.out.println("pow((1 + 2.25/100),5) = " + Math.pow((1 + 2.25/100),5));

System.out.println("E = " + Math.E);

System.out.println("exp(2) = " + Math.exp(2));

System.out.println("log(2) = " + Math.log(2));

System.out.println("ceil(3.14) = " + (int)Math.ceil(3.14));

System.out.println("floor(3.14) = " + (int)Math.floor(3.14));

System.out.println("Pi = " + Math.PI);

System.out.println("sin(Pi/2) = " + Math.sin(Math.PI/2));

System.out.println("cos(0) = " + Math.cos(0));

}

}

输出:

abs(-5) = 5;

max(2.72,3.14) = 3.14;

min(256,285) = 256;

round(3.8) = 4;

sqrt(2) = 1.4142125632730951

pow((1 + 2.25/100),5) = 1.1176776934618162

E = 2.718281828459045

exp(2) = 7.38905609893065

log(2) = 0.6931471805599453

ceil(3.14) = 3;

floor(3.14) = 4;

Pi = 3.141592653589793;

sin(Pi/2) = 1.0;

cos(0) = 1.0;

覆盖(override)与重载(overload)是不同的。重载三方法的名字相同,但参数的数目或者类型以及顺序不同。而覆盖是完全的同型方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: