摘要:
网络编程基础 (Socket套接字) 在包java.net.*里面1.import java.net.*; //创建服务器端 import java.io.*; public class Helloworld1 {public static void main(String args[]) throws Exception { //创建服务器端和连接时,都会报异常 ServerSocket ss = new ServerSocket(6666); //创建服务器端,并且定义端口号为6666 (每个程序只能占用一个端口号) Boolean flag =true; //定义一个变量以便控制服务器. 阅读全文
posted @ 2012-07-17 23:45
建志
阅读(98)
评论(0)
推荐(0)
摘要:
写多线程的两种方法 (多线程) 之前写的程序都是单线程的(只有一条执行路径)1.public class Helloworld1 { //第一种方法(实现Runnable接口)更好,能继承,又能实现尽量实现(实现了之后,还能继承)public static void main(String args[]) { Thread t1 = new Thread(new T("t1")); Thread t2 = new Thread(new T("t2")); t1.start(); //只有Thread里面的start()方法,才叫做开启线程 t2.star 阅读全文
posted @ 2012-07-17 23:44
建志
阅读(99)
评论(0)
推荐(0)
摘要:
字节流的先输出后输入 (io流)import java.io.*;public class Helloworld1 {public static void main(String args[]) throws Exception { FileOutputStream fos = new FileOutputStream("d:/java/a.txt"); //new一个输出字节流对象,后面加上文件的路径 FileInputStream fis = new FileInputStream("d:/java/a.txt"); //new一个输入字节流对象,从 阅读全文
posted @ 2012-07-17 23:43
建志
阅读(116)
评论(0)
推荐(0)
摘要:
import java.util.*;public class Helloworld1 {public static void main(String args[]) {Set<String> set = new HashSet<String>(); //不加上<String>泛型会报不安全List<String> list = new ArrayList<String>();set.add("my"); //add()添加容器的内容set.add("name");set.add("i 阅读全文
posted @ 2012-07-17 23:41
建志
阅读(127)
评论(0)
推荐(0)
摘要:
打印文件的目录 (io下的File类)import java.io.*;public class Helloworld1 {public static void main(String args[]) {File file = new File("d:/aa");Helloworld1.show(file,1); //静态方法,类名调用}public static void show(File file,int level) {String str = ""; for(int i=1;i<level;i++) {str = str +" 阅读全文
posted @ 2012-07-17 23:38
建志
阅读(132)
评论(0)
推荐(0)
摘要:
打印1-10之间的随机数 (lang包底下的math类)1.public class Helloworld1 { //lang包里面的math类的random()方法public static void main(String args[]) { System.out.println(Math.floor(Math.random()*10)+1); //也可以(int)(Math.random()*10)+1或Math.ceil(Math.random()*10)}}2.import java.util.*; //util包里面的Random类的nextInt()方法 public class 阅读全文
posted @ 2012-07-17 23:37
建志
阅读(121)
评论(0)
推荐(0)
摘要:
//一个字符串,大写字母个数,小写字母个数,其他字符个数(String类)public class Helloworld1 {public static void main(String args[]) { String str = "360 dazhan QQ"; int num2[] = new int[13]; for(int j=0;j<str.length();j++) { num2[j] = str.codePointAt(j); //将转化后的ASCII值,传给数组 } int a = 0; int b = 0; int c = 0; for(int k 阅读全文
posted @ 2012-07-17 23:35
建志
阅读(146)
评论(0)
推荐(0)
摘要:
//数组的冒泡法排序 (数组)public class Helloworld1 {public static void main(String args[]) {int nums[] = new int[]{4,8,1,3,6,5,7,9,2};for(int j=0;j<nums.length-1;j++) { //j到j<nums.length-1结束,最后一个不需要比较for(int i=j+1;i<nums.length;i++) { //i从j+1开始循环if(nums[j]<nums[i]) {int temp = nums[i];nums[i] = num 阅读全文
posted @ 2012-07-17 23:33
建志
阅读(132)
评论(0)
推荐(0)
摘要:
接口(特殊的抽象类,里面只能有静态常量和抽象的方法,定义interface car)。public class Helloworld1 {public static void main(String args[]) { Bmw bmw = new Bmw(); bmw.stop(); bmw.run(); bmw.m(); bmw.n(); System.out.println(Car.name); //car的name是静态常量,只要用类名调用 }}interface A {public void m();}interface B {public void n();}interface Ca 阅读全文
posted @ 2012-07-17 23:32
建志
阅读(200)
评论(0)
推荐(0)
摘要:
抽象类(不需要创建他的对象),和抽象方法(不能有方法体,就是给别人重写的)注:抽象类既可以有抽象的方法,也可以有非抽象的方法。而有抽象方法的类,一定是抽象类。public class Helloworld1 {public static void main(String args[]) {Cat cat = new Cat();cat.name = "tom"; cat.enjoy();}}abstract class Animal { //加上abstract,抽象类String name;public void sleep() {System.out.println(n 阅读全文
posted @ 2012-07-17 23:30
建志
阅读(99)
评论(0)
推荐(0)
浙公网安备 33010602011771号