理解API的概念
API的概念(Application Programming Interface)应用程序编程接口
String类和StringBuffer类
1.位于java.lang包中(直接使用,不用引包)
2.String类对象中的内容一旦被初始化就不能再改变(查找 提取 子串 )
3.StringBuffer类用于封装内容可以改变的字符串(增 插 颠倒)
4.一旦通过StringBuffer生成想要的字符串,用toString方法转换成String类型
5.连接操作符:”+” :String x=”a”+4+”c”;
6.编译时等于:
String x=new StringBuffer().append(“a”).append(4).append(“c”).toString();
字符串常量(如”hello”)实际上是一种特殊的匿名String对象。
比较下面两种情况的差异:
String s1=”hello”;
String s2=”hello”;
有则不创建,节约内存空间,指向同一个String s1 和String s2指向同一个”hello”
String s1=new String(“hello”);
String s2=new String(“hello”);
创建两个对象,内容相同,指向不同hello ==: false
String类的常用成员方法
构造方法:
String(byte[] bytes,int offset,int length)
将一个字节数组中内容转换成字符串从第offset个开始,一直取出length个
equalslgnoreCase方法
忽略大小写 (用户登录验证))
indexOf(int ch) 方法
1.返回一个字符在字符串首次出现位置,找不到返回-1
eg. System.out.print("hello world".indexOf('o')); 返回4
2.指定从哪个位置开始查找
System.out.print("hello world".indexOf('o',5)); 返回7
substring(int beginIndex)方法
返回从beginIndex到结尾的字符串 beginIndex>字符串长度 返回空串
eg. System.out.print("hello world".substring(6)); 返回world
substring(int beginIndex,int endIndex)
从beginIndex到endIndex-1之间字符。
eg. System.out.print("hello world".substring(6,8)); 返回wo
基本数据类型的对象包装类
基本数据类型包装类的作用
(基本数据类型不具备对象特性,不能满足某些特殊需求Java api 很多方object 类型)
eg.add(Object o)
boolean---->Boolean
byte ---->Byte
char ---->Character
short ---->Short
int ---->Integer
long ---->Long
float ---->Float
double ---->Double
Integer(String s)
Integer(int t)
将字符串转换为整数的三种方法:
Integer的方法intValue()将Integer转换为int
parseInt() //可转换进制,默认十进制
valueOf() //将Integer转换为Interger对像
1.String sb=new String();
for(int j=0;j<w;j++){
sb=sb+”*”;
}
2.StringBuffer sb=new StringBuffer();
for(int j=0;j<w;j++){
sb.append(“*”);
}
运行效率:1.每次先将sb转换stringbuffer,追加,新结果转换string,每次都重复这一过程2.每次循环都还是stringbuffer并没有产生新对象,运行效率高
集合类
集合类用于存储一组对象,其中的每个对象称之为元素,经常会用到的有Vector,Enumeration,ArrayList,Collection,Iterator,Set,List等集合类和接口。
Vector类与Enmeration接口
Enumeration 不用管那种数据结构都可取出其中的元素
Collection接口与Iterator接口
Collection、Set、List的区别如下:
Collection各元素对象之间没有指定的顺序,允许有重复元素和多个null元素对象。
Set个元素对象之间没有指定的顺序,不允许有重复元素,最多允许有一个null元素对象。
List各元素对象之间有指定的顺序,允许有重复元素和多个null元素对象。
List对象之间有顺序,可排序
Collections主要用于操作集合对象,方法静态
Hashtable类
Hashtable不仅可以像Vector一样动态存储一系列的对象,而且对存储的每一个对象(称为值)都要安排另一个对象(称为关键字)与之相关联。
Hashtable numbers=new Hashtable();
numbers.put(“one”,new Integer(1));
numbers.put(“two”,new Integer(2));
numbers.put(“three”,new Integer(3));
Integer n=(Integer)numbers.get(“two”); //通过关键字检索值
If(n!=null){
System.out.println(“two”+n);
}
用作关键字的类必须覆盖Object.hashCode方法和Object.equals方法
Properties类
Properties类是Hashtable的子类
增加了将Hashtable对象中的关键字和值保存到文件和从文件中读取关键字和值到Hashtable对象中的方法
如果要用Properties.store方法存储Properties对象中的内容,每个属性的关键字和值都必须是String类型
System类与Runtime类
System类
exit方法
currentTimeMillis方法
java虚拟机的系统属性
getProperties和setProperties方法
Runtime类
Runtime.getRuntime静态方法
与日期和时间有关的类
最常用的几个类:Date,DateFormat,和Calendar
Calendar类
Calendar.add方法
Calendar.get方法
Calendar.set方法
Calendar.getInstance静态方法
GregorianCalendar子类
Date类
Java.text.DateFormat与java.text.SimpleDateFormat子类
Timer与TimerTask类
Schedule方法主要有如下几种重载形式:
Schedule(TimeTask task,long delay)
Schedule(TimeTask task,Date time)
Schedule(TimeTask task,long delay,long period)
Schedule(TimerTask task,Date firstTime,long period)
TimerTask类实现了Runnable接口,要执行的任务由它里面实现的run方法来完成。
Math与Random类
Math类包含了所有用于几何和三角运算的方法
Random类是一个伪随机数产生