1、线程:
//通过继承Tread
public class People extends Thread {
StringBuffer str; People(String s,StringBuffer str) { setName(s); this.str=str; } public void run() { for(int i=1;i<=3;i++) { str.append(getName()+","); System.out.println("我是"+getName()+",字符串为:"+str); try { sleep(200); } catch(InterruptedException e){} } } public static void main(String[] args) { People personOne,personTwo; StringBuffer str=new StringBuffer(); personOne=new People("张三",str); //创建线程 personTwo =new People("李四",str); personOne.start(); //启动线程 personTwo.start(); } }
//通过实现Runnable
public class Bank implements Runnable {
private int number=0; public void setMoney(int m) { number=m; } public void run() { while(true) { String name=Thread.currentThread().getName(); if(name.equals("One")) { if(number<=160) { System.out.println(name+"进入死亡状态"); return; } number=number+10; System.out.println("我是"+name+"现在number="+number);
} if(Thread.currentThread().getName().equals("Two")) { if(number<=0) { System.out.println(name+"进入死亡状态"); return; } number=number-100; System.out.println("我是"+name+"现在number="+number); } try{ Thread.sleep(800); } catch(InterruptedException e){} } } public static void main(String args[ ]) { } Bank bank=new Bank(); bank.setMoney(300); Thread threadOne,threadTwo; threadOne=new Thread(bank); threadOne.setName("One"); threadTwo=new Thread(bank); threadTwo.setName("Two"); threadOne.start(); threadTwo.start(); }
2、网络编程
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) {
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try{ mysocket=new Socket("127.0.0.1",4331);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream()); char c='a';
while(true) {
if(c>'z')
c='a';
out.writeChar(c);
char s=in.readChar(); //in读取信息,堵塞状态
System.out.println("客户收到:"+s);
c++;
Thread.sleep(500);
}
}
catch(Exception e) {
System.out.println("服务器已断开"+e);
}
}
}
import java.io.*;
import java.net.*;
public class Server {
public static void main(String args[]) {
ServerSocket server=null;
Socket you=null;
DataOutputStream out=null;
DataInputStream in=null;
try { server=new ServerSocket(4331);
}
catch(IOException e1) {
System.out.println(e1);
}
try{ System.out.println("等待客户呼叫");
you=server.accept(); //将客户端与服务器端的套接字连接起来 out=new DataOutputStream(you.getOutputStream()); in=new DataInputStream(you.getInputStream()); while(true) {
char c=in.readChar(); //in读取信息,堵塞状态 System.out.println("服务器收到:"+c);
out.writeChar((char)(c-32));
Thread.sleep(500);
}
}
catch(Exception e) {
System.out.println("客户已断开"+e);
}
}
}
文件操作(缓冲流)
import java.io.*;
public class Buffer {
public static void main(String args[]) { File file=new File("Student.txt"); String content[]={"天气预报:","北京晴","上海多云,有小雨","大连晴,有
时多云"};
} try{ FileWriter outOne=new FileWriter(file); BufferedWriter outTwo= new BufferedWriter(outOne); for(String str:content) { outTwo.write(str); outTwo.newLine(); } outTwo.close(); outOne.close(); FileReader inOne=new FileReader(file); BufferedReader inTwo= new BufferedReader(inOne); String s=null; while((s=inTwo.readLine())!=null) { System.out.println(s); } inOne.close(); inTwo.close(); } catch(IOException e) { System.out.println(e); } }
数据库
import java.sql.*; public class DAO {
public static Connection getConnection() { } String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url = "jdbc:sqlserver://localhost:1433; String user = "sa"; String pass = "123"; Connection conn = null; try { } return conn; Class.forName(driver); conn = DriverManager.getConnection(url, user, pass); e.printStackTrace(); DatabaseName=test"; } catch (Exception e) {
}
public void update(String sql) { } public static void main(String[] args) { } DAO dao = new DAO(); String sql = "update employee set name='李四' where number=1"; dao.update(sql); Connection conn = null; PreparedStatement state = null; try { } catch (Exception e) { } finally { } try { } state.close(); conn.close(); e.printStackTrace(); e.printStackTrace(); conn = DAO.getConnection(); state = conn.prepareStatement(sql); state.executeUpdate(); } catch (Exception e) {
LinkedList泛型//Iterator
import java.util.LinkedList; public class Goods {
private String name; private int price; public Goods(String name, int price) { } public String toString() { } return "Goods name=" + name + ", price=" + price + ""; this.name = name; this.price = price;
} public static void main(String[] args) { LinkedList<Goods> list=new LinkedList<Goods>(); list.add(new Goods("iphone",8000)); list.add(new Goods("iair",10000)); list.add(new Goods("ipad",3000)); list.remove(1);//指定位置删除 list.add(1,new Goods("itouch",2000));//指定位置添加 System.out.println(list); }
Treeset泛型
import java.util.*;
class Student implements Comparable {
int english=0;
String name;
Student(int english,String name) {
this.name=name;
this.english=english;
}
public int compareTo(Object b) {
Student st=(Student)b;
return (this.english-st.english);
}
public static void main(String args[]) {
TreeSet<Student> mytree=new TreeSet<Student>(); Student st1,st2,st3,st4;
st1=new Student(90,"赵一");
st2=new Student(66,"钱二");
st3=new Student(86,"孙三");
st4=new Student(76,"李四");
mytree.add(st1);
mytree.add(st2);
mytree.add(st3);
mytree.add(st4);
Iterator<Student> te=mytree.iterator();
while(te.hasNext()) {
Student stu=te.next();
System.out.println(""+stu.name+" "+stu.english); }
}
}
数组
import java.util.Arrays;
import java.util.Scanner;
public class Array {
public void input(int[] a) { Scanner sc = new Scanner(System.in); for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
}
public void output(int[] a)
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" "); }
}
public static void main(String[] args) { int[] a = new int[10];
Array array = new Array();
array.input(a);
Arrays.sort(a);
array.output(a);
System.out.println("最小值:"+a[0]); System.out.println("最大值:"+a[9]); }
}