sort(Object[] a)可以对实现了Comparable接口的对象进行排序;使用
binarySearch(Object[] a, Object key)将可以对已经排序好了的对象数组进行二分法查找。实例如下:
- class Student implements Comparable
- {
- int num;
- String name;
- Student(int num,String name)
- {
- this.num=num;
- this.name=name;
- }
- public String toString()
- {
- return "number="+num+","+"name="+name;
- }
- public int compareTo(Object o)
- {
- Student s=(Student)o;
- //return num>s.num ? 1 : (num==s.num ? 0 : -1);
- int result=num>s.num ? 1 : (num==s.num ? 0 : -1);
- if(0==result)
- {
- result=name.compareTo(s.name);
- }
- return result;
- }
- }
- Student[] ss=new Student[]{new Student(1,"zhangsan"),
- new Student(2,"lisi"),
- new Student(3,"wangwu"),
- new Student(3,"mybo")};
- Arrays.sort(ss);
- for(int i=0;i<ss.length;i++)
- {
- System.out.println(ss[i]);
- }
- int index=Arrays.binarySearch(ss,new Student(2,"lisi"));
- System.out.println("index="+index);
- System.out.println(ss[index]);
- }
- int i=3;
- Integer in=new Integer(i);
- int j=in.intValue();
- System.out.println("j="+j);
- String str=in.toString();
- System.out.println("str="+str);
- String str2="134";
- System.out.println(Integer.valueOf(str2));
- Class c=Class.forName(args[0]);
- Constructor[] cons=c.getDeclaredConstructors();
- /*for(int i=0;i<cons.length;i++)
- {
- System.out.println(cons[i]);
- }
- Method[] ms=c.getDeclaredMethods();
- for(int i=0;i<ms.length;i++)
- {
- System.out.println(ms[i]);
- }*/
- Class[] params=cons[0].getParameterTypes();
- Object[] paramValues=new Object[params.length];
- for(int i=0;i<params.length;i++)
- {
- if(params[i].isPrimitive())
- {
- paramValues[i]=new Integer(i+3);
- }
- }
- Object o=cons[0].newInstance(paramValues);
- Method[] ms=c.getDeclaredMethods();
- ms[0].invoke(o,null);
- class Point
- {
- static
- {
- System.out.println("Loading Point");
- }
- int x,y;
- void output()
- {
- System.out.println("x="+x+","+"y="+y);
- }
- Point(int x,int y)
- {
- this.x=x;
- this.y=y;
- }
- }
- Runtime rt=Runtime.getRuntime();
- System.out.println(rt.freeMemory());
- System.out.println(rt.totalMemory());
- try
- {
- //rt.exec("notepad");
- Process p=rt.exec("javac ArrayTest.java");
- InputStream is=p.getInputStream();
- int data;
- while((data=is.read())!=-1)
- {
- System.out.print((char)data);
- }
- public class ServerService {
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(6000);
- Socket s = ss.accept();
- OutputStream os = s.getOutputStream();
- os.write("welcome client, this is server!".getBytes());
- InputStream is = s.getInputStream();
- byte[] b = new byte[100];
- int len = is.read(b);
- System.out.println(new String(b,0, len));
- is.close();
- os.close();
- s.close();
- ss.close();
- }
- }
- public class Client {
- public static void main(String[] args) throws Exception {
- Socket s = new Socket("127.0.0.1", 6000);
- OutputStream os = s.getOutputStream();
- os.write("hello server, this is client!".getBytes());
- InputStream is = s.getInputStream();
- byte[] b = new byte[100];
- int len = is.read(b);
- System.out.println(new String(b, 0, len));
- is.close();
- os.close();
- s.close();
- }
- }
- public class ServerService {
- public static void main(String[] args) throws Exception {
- ServerSocket serverSocket = new ServerSocket(6000);
- Server server = new Server(serverSocket);
- new Thread(server).start();
- }
- }
- class Server implements Runnable {
- private ServerSocket serverSocket;
- public Server(ServerSocket serverSocket) {
- this.serverSocket = serverSocket;
- }
- @Override
- public void run() {
- Socket s = null;
- try {
- while(true){
- s = serverSocket.accept();
- OutputStream os = s.getOutputStream();
- os.write("welcome client, this is server!".getBytes());
- InputStream is = s.getInputStream();
- byte[] b = new byte[100];
- int len = is.read(b);
- System.out.println(new String(b, 0, len));
- is.close();
- os.close();
- s.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public class UDPReceiver {
- public static void main(String[] args) {
- try {
- DatagramSocket ds = new DatagramSocket(6000);
- byte[] buf = new byte[100];
- DatagramPacket dp = new DatagramPacket(buf, 100);
- ds.receive(dp);
- System.out.println(new String(buf, 0, dp.getLength()));
- ds.close();
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public class UDPSender {
- public static void main(String[] args) {
- try {
- DatagramSocket ds = new DatagramSocket();
- String str = "hello, this is zhangsan";
- DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
- InetAddress.getByName("localhost"), 6000);
- ds.send(dp);
- ds.close();
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- File fDir=new File(File.separator);
- String strFile="JavaLesson"+File.separator+"Lesson7"+
- File.separator+"1.txt";
- File f=new File(fDir,strFile);
- f.createNewFile();
StringBuffer sb = new StringBuffer();
for (T o : set) {
sb.append(o.toString());
}
return sb.toString();
}
StringBuffer sb = new StringBuffer();
for (K k : configMap.keySet()) {
sb.append(k + "->" + configMap.get(k));
}
return sb.toString();
}
- private boolean flag = true;
- @Override
- public void run(){
- while(flag){
- //...
- }
- }
- public void stopThread(){
- flag = false;
- }
- @Override
- public void run(){
- while(true){
- if(...)
- break; //退出循环
- }
- }
- ObjectOutputStream os = new ObjectOutputStream();
- os.writeObject(new SerObject());
FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345); oos.writeObject("Today"); oos.writeObject(new Date()); oos.close();
FileInputStream fis = new FileInputStream("t.tmp"); ObjectInputStream ois = new ObjectInputStream(fis); int i = ois.readInt(); String today = (String) ois.readObject(); Date date = (Date) ois.readObject(); ois.close();
- public class Demo01 {
- public static void main(String[] args) throws Exception {
- MyThread mt = new MyThread();
- new Thread(mt).start();
- Thread.sleep(10); //必须等待,否则主线程会在自己的时间片内完成flag值的修改,则启动的时候执行的都是obj中的代码,达不到验证同步的目的
- mt.flag = true;
- new Thread(mt).start();
- }
- }
- class MyThread implements Runnable {
- private Logger logger;
- private Object object = new Object();
- public boolean flag = false;
- public MyThread() {
- super();
- DOMConfigurator.configure(this.getClass().getClassLoader()
- .getResource("com/alibaba/thread/log4j.xml"));
- logger = LoggerFactory.getLogger(this.getClass().getName());
- }
- private int ticket = 100;
- /*
- * (non-Javadoc)
- * @see java.lang.Runnable#run()
- */
- public void run() {
- if (flag == false) {
- while (true) {
- sell();
- }
- } else {
- while (true) {
- synchronized (this) {
- if (ticket > 0) {
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (logger.isInfoEnabled()) {
- logger.info("obj: " + Thread.currentThread().getName() + " sell: "
- + ticket--);
- }
- } else {
- return;
- }
- }
- }
- }
- }
- public synchronized void sell() {
- if (ticket > 0) {
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (logger.isInfoEnabled()) {
- logger.info("syn: " + Thread.currentThread().getName() + " sell: " + ticket--);
- }
- } else {
- return;
- }
- }
- }