1. 集合类:集合类用于存储一组对象
2. 输入一串数字打印出这串数字的和
public class TestVector {
public static void main(String[] args) {
Vector v = new Vector();
System.out.println("enter number :");
while (true) {
int b = 0;
try {
b = System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(b == '\r' || b == '\n'){
break;
}else{
int num = b - '0';//通过相减得到实际的数字
v.addElement(new Integer(num));
}
}
int sum = 0;
Enumeration e =v.elements();
while(e.hasMoreElements()){
sum += (Integer)e.nextElement();
}
System.out.println(sum);
}
}
3. Vector和ArrayList非常相似,使用ArrayList实现上面的功能,注意两者的区别是是不是线程同步的,Vector是线程安全的,但是执行效率低,ArrayList是线程不同步的执行效率相对较高:
publicclass TestArrayList {
publicstaticvoid main(String[] args) {
ArrayList v = new ArrayList();
System.out.println("enter number :");
while (true) {
int b = 0;
try {
b = System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(b == '\r' || b == '\n'){
break;
}else{
int num = b - '0';//通过相减得到实际的数字
v.add(new Integer(num));
}
}
int sum = 0;
Iterator e = v.iterator();
while(e.hasNext()){
sum += (Integer)e.next();
}
System.out.println(sum);
}
}
4. 各种集合对象接口的区别
5. Collections实现对集合类的排序,该类是直接继承的Object
ArrayList list = new ArrayList();
list.add(1);
list.add(3);
list.add(2);
System.out.println(list.toString());
Collections.sort(list);
System.out.println(list.toString());
6. Hashtable类存储键值对,Hashtable中不能有相同的关键字,注意作为Hashtable类的关键字的类必须覆盖equls方法和hashCode方法;注意这两个方法是给Hashtable的get()方法使用的;
键值对象:
publicclass MyKey {
private String name;
privateintage;
public MyKey(String name, int age) {
this.name = name;
this.age = age;
}
@Override
publicint hashCode() {
finalint prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
publicboolean equals(Object obj) {
if (this == obj)
returntrue;
if (obj == null)
returnfalse;
if (getClass() != obj.getClass())
returnfalse;
MyKey other = (MyKey) obj;
if (age != other.age)
returnfalse;
if (name == null) {
if (other.name != null)
returnfalse;
} elseif (!name.equals(other.name))
returnfalse;
returntrue;
}
@Override
public String toString() {
return"MyKey [name=" + name + ",age=" + age + "]";
}
}
测试类:
publicclass HashtableTest {
publicstaticvoid main(String[] args) {
Hashtable h = new Hashtable();
h.put(new MyKey("zhangsan", 12), new Integer(1));
h.put(new MyKey("lisi", 13), new Integer(2));
h.put(new MyKey("wangwu", 12), new Integer(3));
Enumeration e = h.keys();
while (e.hasMoreElements()) {
MyKey key = (MyKey) e.nextElement();
System.out.print(key + " = " + h.get(key));
System.out.println();
}
System.out.println(h.get(new MyKey("zhangsan",12)));// 如果没有重写equals和hashCode方法则返回值为null
}
}
7. properties是Hashtable类的子类
8. System类中的所有的方法都是静态的;
9. 处理日期和时间的类:Calendar类
publicclass TestCalendar {
publicstaticvoid main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c.get(c.YEAR) + "年" + c.get(c.MONTH) + "月"
+ c.get(c.DAY_OF_MONTH) + "日 " + c.get(c.HOUR) + " : "
+ c.get(c.MINUTE) + " : " + c.get(c.SECOND));
c.add(c.DAY_OF_YEAR, 315);
System.out.println("315 天后的时间:");
System.out.println(c.get(c.YEAR) + "年" + c.get(c.MONTH) + "月"
+ c.get(c.DAY_OF_MONTH) + "日 " + c.get(c.HOUR) + " : "
+ c.get(c.MINUTE) + " : " + c.get(c.SECOND));
}
}
10.将2012-05-01格式的日期转换成2012年05月01日格式的日期:
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat s2 = new SimpleDateFormat("yyyy年MM月dd日");
Date d;
try {
d = s1.parse("2012-05-01");
System.out.println(s2.format(d));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
11.在指定的时间之后运行一段指定的代码:
class MyTimeTask extends TimerTask{
private Timer tm;
public MyTimeTask(Timer tm){
this.tm = tm;
}
publicvoid run() {
try {
Runtime.getRuntime().exec("calc.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tm.cancel();//结束任务线程的代码
}
}
Timer timer = new Timer();
timer.schedule(new MyTimeTask(timer), 20000);