`
北斗文昌
  • 浏览: 9459 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

思考与实践2

 
阅读更多

<!--[if !supportLists]-->1. <!--[endif]-->API

Application Programming Interface:有两层含义,一个是系统给提供的一些能够实现一些功能的接口的集合。我们可以通过这些接口来快速的组建自己想要的符合某种功能的结构,而不必知道这些接口的内部是怎么实现这些功能的。另一层含义是给提供以上接口的公司给提供的一种帮助文档,它可以快速的帮助我们查找我们需要的接口以及这些接口的使用方式。


<!--[if !supportLists]-->2. <!--[endif]-->StringStringBuffer的差异;replace()toUppercase()的区别

String类定义的是一种不可变长的字符串,用于比较连个字符串,查找和抽取串中的字符或子串,字符串与其他类型的转换等;

StringBuffer类定义的是一种可变长的字符串,可以将其他各种类型的数据增加、插入到字符串中,也可以翻转字符串中的内容。可以通过toString()方法来转换成String

String类中的replace()方法可以实现将字符串中的特定字符转换成其他字符,并且会创建新的String对象来保存新的字符串并返回新的字符串。StringBuffer类中的replace()方法实现将给定的字符串替换指定的位置的子字符串。先将子字符串中的字符移除,然后将给定的字符串插入。不会创建新的对象。

String类中的toUppercase()方法实现将字符串中的所有字符按照指定的Locale规则转换成大写字母。StringBuffer类中没有toUppercase()方法


<!--[if !supportLists]-->3. <!--[endif]-->三种将字符串转换成整数的方式

Sting s = new String(“315”);int t = 0;

1t=Integer.valueOf(s).intValue();

2t=Integer.parseInt(s);

3char[] ch = s.toCharArray();

for (int i= 0; i < ch.length; i++) {

t=t*10+(ch[i]-48);

System.out.println(t);

}


<!--[if !supportLists]-->4. <!--[endif]-->VectorArrayList的区别及应用场合

编程中,若要考虑线程安全问题,程序员又不想对其处理,可以使用Vector,因为在Vector中已经为我们考虑处理了,使用ArrayList时,需要程序员考虑线程安全并对其进行相应的处理。单线程中ArrayList效率更高。

VectorArrayList可以从两个方面来比较:

1Vector是线程安全的,也就是说是同步的,而ArrayList是线程不安全的,不同步的。

2)当需要增长时,Vector默认增长为原来的一倍,而ArrayList却是原来的一半

因此,如果涉及到堆栈,队列等操作,应该考虑Vector;对于需要快速随机访问元素的,应该使用ArrayList。而对于要进行快速插入、删除元素操作的,两者都不用,而使用LinkedList


<!--[if !supportLists]-->5. <!--[endif]-->编写一个能用作Hashtable关键字的子类,其中含String name;int age这两个成员变量,编写出验证该关键字类是否正确的检测代码

import java.util.*;

public class HashTableTest {

public static void main(String[] args) {

Hashtable numbers = new Hashtable();

numbers .put(new MyKey("zhangsan",18), new Integer(1));

numbers .put(new MyKey("lisi",15), new Integer(2));

numbers .put(new MyKey("wangwu",20), new Integer(3));

Enumeration e = numbers.keys();

while(e.hasMoreElements()){

MyKey key = (MyKey)e.nextElement();

System.out.print(key + "=");

System.out.println(numbers.get(key));

}

System.out.print(numbers.get(new MyKey("zhangsan",18)));

}

}

public class MyKey {

private String name = null;

private int age = 0;

public MyKey(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String toString() {

return "MyKey [name=" + name + ", age=" + age + "]";

}

public boolean equals(Object obj) {

if(obj instanceof MyKey){

MyKey objTemp = (MyKey)obj;

if(name.equals(objTemp.name) && age == objTemp.age){

return true;

}

}

return false;

}

public int hashCode() {

return name.hashCode() + age;

}

}


<!--[if !supportLists]-->6. <!--[endif]-->编写出打印出当前虚拟机的所有系统属性的程序,并在启动这个程序时,为Java虚拟机增加一个系统属性

import java.util.*;

public class GetProperties {

public static void main(String[] args) {

// TODO: Add your code here

Properties sp= System.getProperties();

System.getProperties().list(System.out);

sp.setProperty("Mypath", "d:/woshihaoren");

System.out.println("/n/n/n");

System.getProperties().list(System.out);

}

}


<!--[if !supportLists]-->7. <!--[endif]-->为什么Runtime类被设计成不能在程序中直接创建它的实例对象?Java设计者又是通过什么样的方式来保证在程序中只能有一个Runtime的实例对象呢?

程序只可在一个JVM中应用,一个Runtime对应一个JVM,若可以创建实例对象的话,那么多个实例对象就有多个JVM,程序就不知道该在哪个里面跑了。

Runtime不能够用new来创建实例,是因为Runtime作为一个类不提供公有的构造函数,而是采用私有的构造函数强化其不可在用户程序中实例化的能力。Runtime是和java当前的应用程序相关联的,应该只有一个实例,这个实例是在应用程序运行初自动创建的。要使用这个实例,可以用Runtime.getRuntime()方法来获得这个实例的引用。


<!--[if !supportLists]-->8. <!--[endif]-->编写程序,使在该程序中启动windows自带的计算器程序,并且立即结束

import java.io.IOException;

import java.util.*;

public class TestRuntime {

public static void main(String[] args) {

// TODO Auto-generated method stub

Timer tm = new Timer();

tm.schedule(new MyTimerTask(tm), 3000);

}

}

class MyTimerTask extends TimerTask{

private Timer tm = null;

public MyTimerTask(Timer tm){

this.tm = tm;

}

public void run(){

Process p = null;

try {

p = Runtime.getRuntime().exec("calc.exe");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

tm.cancel();//结束任务线程的代码

try{

Thread.sleep(1000); //启动1秒后就结束

p.destroy();

}catch(Exception e){

System.out.println(e.toString());

}

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics