黑马程序员技术交流社区

标题: [邱烙考黑马]代码整理 [打印本页]

作者: 小爷邱烙    时间: 2014-11-27 19:34
标题: [邱烙考黑马]代码整理
整理下代码,用于后面翻看
  1. //if练习:把数字转成星期
  2.         int num = 2;
  3.         if(num==1)
  4.                 System.out.println("Monday");
  5.         else if(num==2)//此处如果用if,则是多条语句,多次操作,效率低
  6.                 System.out.println("Tuesday");
  7.         //...
  8.         else
  9.                 System.out.println("error");
复制代码




作者: 小爷邱烙    时间: 2014-11-27 19:36
  1.         int month = 4;
  2.         if(month>12 || month<1)//先验证数据的正确性
  3.                 System.out.println("error");
  4.         else if(month>=3 && month<=5)
  5.                 System.out.println("春季");
  6.         //...
  7.         else
  8.                 System.out.println("冬季");       
复制代码



作者: 小爷邱烙    时间: 2014-11-27 19:39
  1. switch语句
  2.         switch(表达式){
  3.         case 取值1:
  4.                 执行语句;
  5.                 break;
  6.         ...
  7.         default:
  8.                 执行语句;
  9.                 break;//最后一个为default时,break可以省略不写
  10.         }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 19:41
  1. //switch练习:数字转季节
  2.         int month = 4;
  3.         switch(month){
  4.                 case 3: //小技巧,当case不带执行语句且不break时,继续执行下一个case,适合区间条件
  5.                 case 4:
  6.                 case 5:
  7.                         System.out.println("春季");
  8.                         break;
  9.                 //...
  10.                 default:
  11.                         System.out.println("error");
  12.         }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 19:45
  1.         循环练习:获取1-10的和(重复执行加,使用累加思想)累加思想可用于加、减以及字符串操作
  2.         int sum = 0;//和始终变化,定义变量存储
  3.         int x = 1;//被加数也在变化,定义变量存储
  4.         while(x<=10){
  5.                 sum = sum + x;
  6.                 x++;
  7.         }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 19:47
  1.         //由于x相加后无意义,用for更合适
  2.         int sum = 0;
  3.         for(int x=0;x<=10;x++){
  4.                 sum +=x;
  5.         }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 19:48
  1.         循环练习:计算1-100之间7的倍数的个数,并打印
  2.         int count = 0;
  3.         for(int x=0;x<=100;x++){
  4.                 if(x%7==0)
  5.                         count++;
  6.                 System.out.println("count="+count);
  7.         }
复制代码



作者: 孟育俊    时间: 2014-11-27 19:50
学习啦!!!!
作者: 小爷邱烙    时间: 2014-11-27 19:51
  1.                 循环嵌套练习:打印倒三角形
  2. *****        int z = 5;
  3. ****        for(int x=0;x<5;x++){//控制固定的行数
  4. ***                        for(int y=0;y<z;y++){//控制递减的列数
  5. **                                System.out.print("*");
  6. *                        }
  7.                         System.out.println();//换行
  8.                         z--;//列数递减
  9.                 }
  10.                 //另一种形式
  11.                 int z = 0;
  12.                 for(int x=0;x<5;x++){
  13.                         for(int y=z;y<5;y++){
  14.                                 System.out.print("*");
  15.                         }
  16.                         System.out.println();
  17.                         z++;
  18.                 }
  19.                 //优化:z的变化规律与x相同
  20.                 for(int x=0;x<5;x++){
  21.                         for(int y=x;y<5;y++){
  22.                                 System.out.print("*");
  23.                         }
  24.                         System.out.println();
  25.                 }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 19:57
  1.         循环练习:打印正向三角形
  2.         *                for(int x=0;x<5;x++){
  3.         **                        for(int y=0;y<=x;y++){
  4.         ***                                System.out.print("*");
  5.         ****                }
  6.         *****                        System.out.println();
  7.                         }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 19:59
  1.                 循环练习:打印数字三角形
  2. 1                for(int x=1;x<=5;x++){
  3. 12                        for(int y=1;y<=x;y++){
  4. 123                                System.out.print(y);
  5. 1234                }
  6. 12345                System.out.println();
  7.                 }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:00
  1.         循环练习:打印九九乘法表
  2.         for(int x=1;x<=3;x++){
  3.                 for(int y=1;y<=x;y++){
  4.                         System.out.print(y+"*"+x+"="+y*x+"\t");
  5.                 }
  6.                 System.out.println();
  7.         }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:04
  1.         嵌套循环练习:打印等边三角形
  2.         *                   ----*         
  3.    * *                  ---**
  4.   * * *      ----->     --***
  5. * * * *                -****
  6. * * * * *               *****
  7. 思路:先转成两个三角形组成的正方形,用空格替换-,用*加空格替换*
  8. for(int x=0;x<5;x++){//控制列数
  9.         for(int y=x+1;y<5;y++){
  10.                 System.out.print(" ");
  11.         }
  12.         for(int z=0;z<=x;z++){
  13.                 System.out.print("* ");
  14.         }
  15.         System.out.println();
  16. }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:11
  1. 数组练习:取最值(使用遍历思想)
  2. int x = arr[0];
  3. for(int i=0;i<arr.length;i++){
  4.         if(x<arr[i])
  5.                 x = arr[i];
  6. }
  7. System.out.println(x);
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:12
  1. 使用角标技巧取最值
  2. int x = 0;//x为角标
  3. for(int i=1;i<arr.length;i++){
  4.         if(arr[x]<arr[i])
  5.                 x = i;
  6. }
  7. System.out.println(arr[x]);
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:13
  1. 数组排序(选择排序):先确定最小值,放在0角标,再一次确定各个角标的值
  2. for(int i=0;i<arr.length;i++){
  3.         for(int j=i+1;j<arr.length;j++){
  4.                 if(arr[i]>arr[j]){
  5.                         int temp = arr[i];
  6.                         arr[i] = arr[j];
  7.                         arr[j] = temp;
  8.                 }
  9.         }
  10. }//由于数组是引用数据类型,交换完位置就是在堆内存中排好序了,所以不用返回值
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:14
  1. 数组排序(冒泡排序):相邻的两个元素比较,若前面的大于后面的,互换位置,先确定最大值的位置
  2. for(int i=0;i<arr.length-1;i++){//外层控制圈数,最后一个元素无需比较
  3.         for(int j=0;j<arr.length-i-1;j++){//内层长度由外层控制,已经比较过的从长度中减去
  4.                 if(arr[j]>arr[j+1]){
  5.                         int temp = arr[j];
  6.                         arr[j] = arr[j+1];
  7.                         arr[j+1] = temp;
  8.                 }
  9.         }
  10. }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:15
  1. 排序细节:抽取交换位置功能
  2. public static void swap(int[] arr,int i,int j){
  3.         int temp = arr[i];
  4.         arr[i] = arr[j];
  5.         arr[j] = temp;
  6. }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:18
  1. int min = 0;
  2. int max = arr.length-1;
  3. int mid = (min+max)/2;
  4. while(arr[mid]!=key){
  5.         if(key>arr[mid]){
  6.                 min = mid+1;
  7.         }else if(key<arr[mid]){
  8.                 max = mid-1;
  9.         }
  10.         if(min>max){
  11.                 return -1;
  12.         }
  13.         mid = (max+min)/2;
  14. }
  15. return mid;
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:19
  1. //另一种形式
  2. int min = 0;
  3. int max = arr.length-1;
  4. int mid;
  5. while(min<=max){
  6.         mid = (min+max)>>1;
  7.         if(key>arr[mid]){
  8.                 min = mid+1;
  9.         }else if(key<arr[mid]){
  10.                 max = mid-1;
  11.         }else{
  12.                 return mid;
  13.         }
  14. }
  15. return -1;
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:21
  1. for(int i=0;i<8;i++){//一个整数是4个字节,32位,四位一移,移动八次
  2.         int temp = num & 15;
  3.         if(temp>9)
  4.                 System.out.println((char)(temp-10+'A'));
  5.         else
  6.                 System.out.println(temp);
  7.         num = num >>>4;
  8. }
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:22
  1. char[] chs = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  2. char[] arr = new char[8];//字符数组的默认值为'\u0000',即Unicode的0000,为空格
  3. int pos = arr.length;//定义指针,用于循环
  4. while(num!=0){
  5.         int temp = num & 15;
  6.         arr[--pos] = chs[temp];//倒序,方便输出
  7.         num = num >>>4;
  8. }
  9. //此时,正负的问题也解决了
复制代码



作者: 小爷邱烙    时间: 2014-11-27 20:23
  1. public static void trans(int num,int base,int offset){
  2.         if(num==0){
  3.                 System.out.println(0);
  4.                 return ;
  5.         }
  6.         char[] chs = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  7.         char[] arr = new char[32];
  8.         int pos = arr.length;
  9.         while(num!=0){
  10.                 int temp = num & base;
  11.                 arr[--pos] = chs[temp];
  12.                 num = num >>> offset;
  13.         }
  14.         for(int i=pos;i<arr.length;i++){
  15.                 System.out.print(arr[i]);
  16.         }
  17. }
复制代码



作者: 小爷邱烙    时间: 2014-12-7 19:50
  1. class Person{
  2.         private int age;
  3.         Person(int age){
  4.                 this.age = age;
  5.         }
  6.         public boolean compare(Person p){
  7.                 return this.age == p.age;//描述功能时,还没有对象。此时需要用到调用该函数的对象,用this
  8.         }
  9. }
  10. class PersonDemo{
  11.         public static void main(String[] args){
  12.                 Person p1 = new Person(20);
  13.                 boolean b = p1.compare(new Person(20));
  14.                 System.out.println(b);
  15.         }
  16. }
复制代码



作者: 小爷邱烙    时间: 2014-12-7 20:00
  1. //构造函数之间的调用,使用this
  2. class Person{
  3.         private String name;
  4.         private int age;
  5.         Person(String name){
  6.                 this.name = name;
  7.         }
  8.         Person(String name,int age){
  9.                 this(name);//本类构造函数间调用,必须这么写
  10.                 this.age = age;
  11.         }
  12. }
  13. //Person p = new Person("lisi",30);
  14. //执行过程:this("lisi");p("lisi");new Person("lisi");
  15. /*
  16. 注意:this();只能定义在构造函数的第一行,由于构造函数是初始化动作,相对于外层构造函数的一般操作动作,
  17.    初始化必须先执行。
  18.    构造函数间不允许循环调用(A调用B,B调用A)
  19.    一般函数不能调用构造函数
  20. */
复制代码



作者: 小爷邱烙    时间: 2014-12-7 20:25
  1. 类上注释
  2. /**
  3. *类的介绍
  4. *@author 作者
  5. *@version 版本
  6. */
  7. 方法上注释(public protected)其他权限不会被制作帮助文档
  8. /**
  9. * 功能简介
  10. * @param 参数 参数介绍 可以写多个,一行一个
  11. * @return 返回值介绍
  12. */
复制代码



作者: 禾青青    时间: 2014-12-7 23:13
学习了 谢谢
作者: 小爷邱烙    时间: 2014-12-8 16:46
  1. //饿汉式
  2. class Single{
  3.         private static Single s = new Single();
  4.         private Single(){}
  5.         public static Single getInstance(){
  6.                 return s;
  7.         }
  8. }
复制代码
  1. //懒汉式
  2. class Single{
  3.         private static Single s = null;
  4.         private Single(){}
  5.         public static Single getInstance(){
  6.                 if(s==null){
  7.                         synchronized (Single.class){
  8.                                 if(s==null){
  9.                                         s = new Single();
  10.                                 }
  11.                         }
  12.                 }
  13.                 return s;
  14.         }
  15. }
复制代码



作者: 小爷邱烙    时间: 2014-12-8 17:49
  1. //获取当前时间:System.currentTimeMillis();返回long 时间毫秒与1970年1月1日午夜的时间差
  2. abstract class GetTime{
  3.         public abstract void run();
  4.         public final void getTime(){
  5.                 long start = System.currentTimeMillis();
  6.                 run();
  7.                 long end = System.currentTimeMillis();
  8.                 System.out.println("毫秒:"+(end-start));
  9.         }
  10. }
  11. //模版方法模式:定义功能时,一部分是确定的,另一部分是不确定的,并且确定
  12. //的部分在使用不确定的部分,把不确定的部分暴露出去,让子类去实现
复制代码



作者: 小爷邱烙    时间: 2014-12-8 19:11
  1. //接口:降低耦合,提高扩展(子类实现方法),定义规则
  2. //多态:提高扩展(符合规则的类都可以纳入体系)
  3. //多态实例:主体调用接口
  4. interface PCI{
  5.         public void open();
  6.         public void close();
  7. }
  8. class MainBoard{
  9.         public void run(){
  10.                 System.out.println("mainboard run");
  11.         }
  12.         public void usePCI(PCI p){
  13.                 //if(p!=null)
  14.                 p.open();
  15.                 p.close();
  16.         }
  17. }
  18. class NetCard implements PCI{
  19.         public void open() {
  20.                 System.out.println("netcard open");
  21.         }
  22.         public void close() {
  23.                 System.out.println("netcard close");
  24.         }       
  25. }
复制代码



作者: 小爷邱烙    时间: 2014-12-8 19:41
  1. abstract class AbsDemo{
  2.         abstract void show();
  3. }
  4. class Outer{
  5.         int x = 3;
  6.         public void function(){
  7.                 new AbsDemo(){
  8.                         void show(){
  9.                                 System.out.println(x);
  10.                         }
  11.                 }.show();
  12.         }
  13. }
复制代码



作者: 小爷邱烙    时间: 2014-12-8 21:03
  1. class Demo{
  2.         int div(int a,int b){
  3.                 return a/b;//2、出现异常并被JVM识别,封装为AritchmeticException 3、抛给main函数
  4.         }
  5. }
  6. class ExceptionDemo{
  7.         public static void main(String[] args){
  8.                 Demo d = new Demo();
  9.                 try{//4、被try检测到(没有try时,main函数直接将问题交给JVM,调用异常处理机制)
  10.                         int x = d.div(4, 0);//1、参数传给形参进行运算
  11.                         System.out.println(x);//不执行
  12.                 }catch(Exception e){//5、将异常交给catch捕获,被catch形参接收
  13.                         System.out.println(".....");//6、执行处理代码,try...catch代码块到此结束
  14.                 }
  15.                 System.out.println("over");//7、继续向下执行
  16.         }
  17. }
复制代码



作者: 小爷邱烙    时间: 2014-12-8 21:50
  1. private String msg;//Throwable中已经有一个private String message;并提供了getter以及构造函数赋值方式
  2. xxxException(String msg){//xxxException(String msg){
  3.         this.msg = msg;      //     super(msg);
  4. }                        //}即可,除此之外,自定义异常类中可以定义自己的方法,特有数据
  5. public String getMessage(){
  6.         return msg;
  7. }
  8. //toString 内部调用getMessage
复制代码



作者: 小爷邱烙    时间: 2014-12-27 09:20
  1. /*
  2. *售票:多窗口同时售票
  3. */
  4. class Ticket extends Thread{
  5.         private static int t = 100;
  6.         public void run(){
  7.                 while(true){
  8.                         if(t>0){
  9.                                 System.out.println(Thread.currentThread().getName()+"...sale:"+t--);
  10.                         }
  11.                 }
  12.         }
  13. }
  14. class TicketDemo{
  15.         public static void main(String[] args) {
  16.                 Ticket t1 = new Ticket();
  17.                 Ticket t2 = new Ticket();
  18.                 t1.start();
  19.                 t2.start();
  20.         }
  21. }
  22. /*
  23. *问题:
  24. *    1、线程间数据共享,使用静态解决,不推荐静态,静态的生命周期太长
  25. *    2、 票不按数序出售:单核不会出现,多核时,会出现1号票出售,但未打印前,2号票同时完成出售+打印,之后1号票才打印的情况
  26. *    3、当同一个线程被多次启动时,会抛出IllegalThreadStateException:无效的线程状态异常。stat只针对线程创建状态
  27. */
复制代码



作者: 小爷邱烙    时间: 2014-12-27 09:38
  1. //模拟线程安全问题
  2. class Ticket implements Runnable{
  3.         private int t = 100;
  4.         public void run() {
  5.                 while(true){
  6.                         if(t>0){
  7.                                 try {
  8.                                         Thread.sleep(100);
  9.                                 } catch (InterruptedException e) {
  10.                                         e.printStackTrace();
  11.                                 }
  12.                                 System.out.println(t--);
  13.                         }
  14.                 }
  15.         }
  16.        
  17. }
  18. /*
  19. *注意:run方法不能抛出异常,因为run方法是继承来的,其父类/接口中的run方法未抛出任何异常,所以必须try
  20. *  多线程必须要十分小心安全问题,因为测试时可能测不出来
  21. *问题:多条语句操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没有执行完,
  22. *  另一个线程参与进来执行,导致共享数据的错误
  23. *解决办法:对多条操作共享数据的语句,只能让一个线程都执行完,在执行过程中,其他线程不可以参与执行
  24. */
复制代码



作者: 小爷邱烙    时间: 2014-12-27 09:52
  1. class Bank{
  2.         private int sum;
  3.         public synchronized void add(int n){
  4.                 sum = sum + n;
  5.                 System.out.println(sum);
  6.         }
  7. }
  8. class Cus implements Runnable{
  9.         private Bank b = new Bank();
  10.         public void run(){
  11.                 for(int x=0;x<3;x++){
  12.                         b.add(100);
  13.                 }
  14.         }
  15. }
  16. /*
  17. * 判断是否需要同步:1、哪些是多线程 2、是否共享数据 3、多线程哪些语句在操作共享数据
  18. * 本例子中,run方法中的语句有操作共享数据,调用add方法,所以add方法需要同步。
  19. * 此时不要在run中同步,run中同步,相当于单线程,要在add中同步。
  20. */
复制代码



作者: 小爷邱烙    时间: 2014-12-27 10:06
  1. //验证:两个线程,一个同步代码块(锁为其他则二线程不同步,锁为this则同步),一个同步函数。
  2. //实现:两个线程执行不同代码,定义一个flag,true执行一部分run,false执行另一部分run
  3. class Ticket implements Runnable{
  4.         private int tick = 100;
  5.         Object obj = new Object();
  6.         boolean flag = true;
  7.         public void run(){
  8.                 if(flag){
  9.                         while(true){
  10.                                 synchronized (obj) {//当锁为this时,同步
  11.                                         if(tick>0){
  12.                                                 try {
  13.                                                         Thread.sleep(100);
  14.                                                 } catch (InterruptedException e) {
  15.                                                         e.printStackTrace();
  16.                                                 }
  17.                                                 System.out.println(Thread.currentThread().getName()+"..code:"+tick--);
  18.                                         }
  19.                                 }
  20.                         }
  21.                 }else{
  22.                         while(true){
  23.                                 show();
  24.                         }
  25.                 }
  26.         }
  27.         public synchronized void show(){
  28.                 if(tick>0){
  29.                         try {
  30.                                 Thread.sleep(100);
  31.                         } catch (InterruptedException e) {
  32.                                 e.printStackTrace();
  33.                         }
  34.                         System.out.println(Thread.currentThread().getName()+"..show:"+tick--);
  35.                 }
  36.         }
  37. }
  38. class ThisLockDemo{
  39.         public static void main(String[] args) {
  40.                 Ticket t = new Ticket();
  41.                 Thread t1 = new Thread(t);
  42.                 Thread t2 = new Thread(t);
  43.                 t1.start();
  44.                 try {
  45.                         Thread.sleep(100);//防止t1启动后不执行,直接改变flag,造成混乱
  46.                 } catch (InterruptedException e) {
  47.                         e.printStackTrace();
  48.                 }
  49.                 t.flag = false;
  50.                 t2.start();
  51.         }
  52. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 10:11
  1. class Single{
  2.         private static Single s = null;
  3.         private Single(){}
  4.         public static Single getInstance(){//使用同步函数低效
  5.                 if(s==null){
  6.                         synchronized (Single.class) {//静态锁
  7.                                 if(s==null){
  8.                                         s = new Single();
  9.                                 }
  10.                         }
  11.                 }
  12.                 return s;
  13.         }
  14. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 10:21
  1. class SynAddRunnable implements Runnable{
  2.         int a,b;
  3.         public SynAddRunnable(int a,int b){
  4.                 this.a = a;
  5.                 this.b = b;
  6.         }
  7.         public void run(){
  8.                 synchronized (Integer.valueOf(a)) {
  9.                         synchronized (Integer.valueOf(b)) {//Integer.valueOf为了节省资源,同一个数字只创建一个对象
  10.                                 System.out.println(a+b);
  11.                         }
  12.                 }
  13.         }
  14.         public static void main(String[] args) {
  15.                 for(int x=0;x<100;x++){
  16.                         new Thread(new SynAddRunnable(1, 2)).start();
  17.                         new Thread(new SynAddRunnable(2, 1)).start();
  18.                 }
  19.         }
  20. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 10:41
  1. class Ticket implements Runnable{
  2.         private int tick = 100;
  3.         Object obj = new Object();
  4.         boolean flag = true;
  5.         public void run(){
  6.                 if(flag){
  7.                         while(true){
  8.                                 synchronized (obj) {//代码块中有函数中有代码块
  9.                                         show();
  10.                                 }
  11.                         }
  12.                 }else{
  13.                         while(true){
  14.                                 show();
  15.                         }
  16.                 }
  17.         }
  18.         public synchronized void show(){
  19.                 synchronized (obj) {
  20.                         if(tick>0){
  21.                                 try {
  22.                                         Thread.sleep(100);
  23.                                 } catch (InterruptedException e) {
  24.                                         e.printStackTrace();
  25.                                 }
  26.                                 System.out.println(Thread.currentThread().getName()+"...code:"+tick--);
  27.                         }
  28.                 }
  29.         }
  30. }
  31. class DeadLockDemo{
  32.         public static void main(String[] args) {
  33.                 Ticket t = new Ticket();
  34.                 Thread t1 = new Thread(t);
  35.                 Thread t2 = new Thread(t);
  36.                 t1.start();
  37.                 try {
  38.                         Thread.sleep(100);
  39.                 } catch (InterruptedException e) {
  40.                         e.printStackTrace();
  41.                 }
  42.                 t.flag = false;
  43.                 t2.start();
  44.         }
  45. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 10:57
  1. class Testa implements Runnable{
  2.         private boolean flag;
  3.         Testa(boolean flag){
  4.                 this.flag = flag;
  5.         }
  6.         public void run(){
  7.                 if(flag){
  8.                         while(true){
  9.                                 synchronized (MyLock.locka) {
  10.                                         System.out.println("if locka");
  11.                                         synchronized (MyLock.lockb) {
  12.                                                 System.out.println("if lockb");
  13.                                         }
  14.                                 }
  15.                         }
  16.                 }else{
  17.                         while(true){
  18.                                 synchronized (MyLock.lockb) {
  19.                                         System.out.println("else lockb");
  20.                                         synchronized (MyLock.locka) {
  21.                                                 System.out.println("else locka");
  22.                                         }
  23.                                 }
  24.                         }
  25.                 }
  26.         }
  27. }
  28. class MyLock{
  29.         static Object locka = new Object();
  30.         static Object lockb = new Object();
  31. }
  32. class DeadLockTest{
  33.         public static void main(String[] args) {
  34.                 new Thread(new Testa(true)).start();
  35.                 new Thread(new Testa(false)).start();
  36.         }
  37. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 19:35
  1. class Res{
  2.         String name;
  3.         String sex;
  4. }
  5. class Input implements Runnable{
  6.         private Res r;
  7.         Input(Res r){
  8.                 this.r = r;
  9.         }
  10.         public void run(){
  11.                 int x = 0;
  12.                 while(true){
  13.                         synchronized (r) {//r对象唯一,也可以用class对象
  14.                                 if(x==0){
  15.                                         r.name = "mike";
  16.                                         r.sex = "man";
  17.                                 }else{
  18.                                         r.name = "丽丽";
  19.                                         r.sex = "女";
  20.                                 }
  21.                                 x = (x+1)%2;
  22.                         }
  23.                 }
  24.         }
  25. }
  26. class Output implements Runnable{
  27.         private Res r;
  28.         Output(Res r){
  29.                 this.r = r;
  30.         }
  31.         public void run(){
  32.                 while(true){
  33.                         synchronized (r) {
  34.                                 System.out.println(r.name+"..."+r.sex);
  35.                         }
  36.                 }
  37.         }
  38. }
  39. class InputOutputDemo{
  40.         public static void main(String[] args) {
  41.                 Res r = new Res();
  42.                 Input in = new Input(r);
  43.                 Output out = new Output(r);
  44.                 Thread t1 = new Thread(in);
  45.                 Thread t2 = new Thread(out);
  46.                 t1.start();
  47.                 t2.start();
  48.         }
  49. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 20:08
  1. class Resource{
  2.         private String name;
  3.         private int count = 1;
  4.         private boolean flag = false;
  5.         public synchronized void set(String name){//生产和消费线程有相同的锁,生产时不能消费,消费时不能生产
  6.                 if(flag){
  7.                         try {
  8.                                 this.wait();
  9.                         } catch (InterruptedException e) {
  10.                                 e.printStackTrace();
  11.                         }
  12.                 }
  13.                 this.name = name+"--"+count++;
  14.                 System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
  15.                 flag = true;
  16.                 this.notify();
  17.         }
  18.         public synchronized void get(){
  19.                 if(!flag){
  20.                         try {
  21.                                 this.wait();
  22.                         } catch (InterruptedException e) {
  23.                                 e.printStackTrace();
  24.                         }
  25.                 }
  26.                 System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
  27.                 flag = false;
  28.                 this.notify();
  29.         }
  30. }
  31. class Producer implements Runnable{//生产者
  32.         private Resource res;
  33.         Producer(Resource res){
  34.                 this.res = res;
  35.         }
  36.         public void run(){
  37.                 while(true){
  38.                         res.set("商品");
  39.                 }
  40.         }
  41. }
  42. class Consumer implements Runnable{
  43.         private Resource res;
  44.         Consumer(Resource res){
  45.                 this.res = res;
  46.         }
  47.         public void run(){
  48.                 while(true){
  49.                         res.get();
  50.                 }
  51.         }
  52. }
  53. class ProducerConsumerDemo{
  54.         public static void main(String[] args) {
  55.                 Resource res = new Resource();
  56.                 new Thread(new Producer(res)).start();
  57.                 new Thread(new Consumer(res)).start();
  58.         }
  59. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 21:18
  1. import java.util.concurrent.locks.*;
  2. class ProducerConsumerDemo2{
  3.         public static void main(String[] args) {
  4.                 Resource r = new Resource();
  5.                 Producer pro = new Producer(r);
  6.                 Consumer con = new Consumer(r);
  7.                 Thread t1 = new Thread(pro);
  8.                 Thread t2 = new Thread(pro);
  9.                 Thread t3 = new Thread(con);
  10.                 Thread t4 = new Thread(con);
  11.                 t1.start();
  12.                 t2.start();
  13.                 t3.start();
  14.                 t4.start();
  15.         }
  16. }
  17. class Resource{
  18.         private String name;
  19.         private int count = 1;
  20.         private boolean flag = false;
  21.         private Lock lock = new ReentrantLock();
  22.         private Condition condition_pro = lock.newCondition();
  23.         private Condition condition_con = lock.newCondition();
  24.         public void set(String name)throws InterruptedException{
  25.                 lock.lock();
  26.                 try{
  27.                         while(flag)
  28.                                 condition_pro.await();
  29.                         this.name = name+"--"+count++;
  30.                         System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
  31.                         flag = true;
  32.                         condition_con.signal();
  33.                 }finally{
  34.                         lock.unlock();
  35.                 }
  36.         }
  37.         public void get()throws InterruptedException{
  38.                 lock.lock();
  39.                 try{
  40.                         while(!flag)
  41.                                 condition_con.await();
  42.                         System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
  43.                         flag = false;
  44.                         condition_pro.signal();
  45.                 }finally{
  46.                         lock.unlock();
  47.                 }
  48.         }
  49. }
  50. class Producer implements Runnable{
  51.         private Resource res;
  52.         Producer(Resource res){
  53.                 this.res = res;
  54.         }
  55.         public void run(){
  56.                 while(true){
  57.                         try{
  58.                                 res.set("商品");
  59.                         }catch(InterruptedException e){
  60.                                 e.printStackTrace();
  61.                         }
  62.                 }
  63.         }
  64. }
  65. class Consumer implements Runnable{
  66.         private Resource res;
  67.         Consumer(Resource res){
  68.                 this.res = res;
  69.         }
  70.         public void run(){
  71.                 while(true){
  72.                         try{
  73.                                 res.get();
  74.                         }catch(InterruptedException e){
  75.                                 e.printStackTrace();
  76.                         }
  77.                 }
  78.         }
  79. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 21:31
  1. class StopThread implements Runnable{
  2.         private boolean flag = true;
  3.         public synchronized void run(){
  4.                 while(flag){
  5.                         try{
  6.                                 wait();
  7.                         }catch(InterruptedException e){
  8.                                 System.out.println(Thread.currentThread().getName()+"...Exception");
  9.                                 changeFlag();
  10.                         }
  11.                         System.out.println(Thread.currentThread().getName()+"...run");
  12.                 }
  13.         }
  14.         public void changeFlag(){
  15.                 flag = false;
  16.         }
  17. }
  18. class StopThreadDemo{
  19.         public static void main(String[] args) {
  20.                 StopThread st = new StopThread();
  21.                 Thread t1 = new Thread(st);
  22.                 Thread t2 = new Thread(st);
  23.                 t1.start();
  24.                 t2.start();
  25.                 int num = 0;
  26.                 while(true){
  27.                         if(num++==60){
  28.                                 t1.interrupt();
  29.                                 t2.interrupt();
  30.                                 break;
  31.                         }
  32.                         System.out.println(Thread.currentThread().getName()+"..."+num);
  33.                 }
  34.                 System.out.println("over");
  35.         }
  36. }
复制代码



作者: 小爷邱烙    时间: 2014-12-27 21:47
  1. new Thread(){
  2.         public void run(){
  3.                 for(int x=0;x<100;x++){
  4.                         System.out.println(Thread.currentThread().getName()+"..."+x);
  5.                 }
  6.         }
  7. }.start();
复制代码



作者: 小爷邱烙    时间: 2014-12-28 10:12
  1.         /*
  2.          *练习一:模拟一个trim方法,去除字符串两端的空格
  3.          *思路:1、判断第一个位置是否是空格,依次直到不是空格为止。判断最后一个是否空格,依次直到不是空格为止
  4.          *    2、开头和结尾都不是空格时,根据角标获取子串
  5.          */
  6.         public static String myTrim(String str){
  7.                 int start = 0,end = str.length()-1;
  8.                 while(start<=end && str.charAt(start)==' '){
  9.                         start++;
  10.                 }
  11.                 while(start<=end && str.charAt(end)==' '){
  12.                         end--;
  13.                 }
  14.                 return str.substring(start, end+1);
  15.         }
复制代码



作者: 小爷邱烙    时间: 2014-12-28 10:22
  1. /*
  2. *练习二:将字符串指定的部分反转
  3. *思路:将字符串变成数组,将数组反转,将数组变成字符串
  4. */
  5.         public static String reverseString(String s,int start,int end){
  6.                 char[] chs = s.toCharArray();
  7.                 reverse(chs,start,end);
  8.                 return new String(chs);
  9.         }
  10.         private static void reverse(char[] arr,int x,int y){
  11.                 for(int start=x,end=y-1;start<end;start++,end--){//y-1,包含头不包含尾,约定俗成
  12.                         swap(arr,start,end);                         //java定义成这样方便传length得到length-1
  13.                 }
  14.         }
  15.         private static void swap(char[] arr,int x,int y){
  16.                 char temp = arr[x];
  17.                 arr[x] = arr[y];
  18.                 arr[y] = temp;
  19.         }
  20.         public static String reverseString(String s){
  21.                 return reverseString(s,0,s.length());
  22.         }
复制代码



作者: 小爷邱烙    时间: 2014-12-28 10:28
  1. /*
  2. *练习三:获取一个字符串中另一个字符串出现的次数
  3. *思路:定义一个计数器,获取子串第一次出现的位置,从第一次出现位置后剩余的字符串中继续获取子串出现的位置,每获取一次,
  4. *    就计数一次,当获取不到时,计数完成
  5. */
  6.         public static int getSubCount(String str,String key){
  7.                 int count = 0;
  8.                 int index = 0;
  9.                 while((index=str.indexOf(key))!=-1){
  10.                         str = str.substring(index+key.length());
  11.                         count++;
  12.                 }
  13.                 return count;
  14.         }
复制代码



作者: 小爷邱烙    时间: 2014-12-28 10:48
  1. /*
  2. *方式二:方式一在内存中产生太多字符串
  3. */
  4.         public static int getSubCount(String str,String key){
  5.                 int count = 0;
  6.                 int index = 0;
  7.                 while((index=str.indexOf(key, index))!=-1){
  8.                         index = index + key.length();
  9.                         count++;
  10.                 }
  11.                 return count;
  12.         }
复制代码



作者: 小爷邱烙    时间: 2014-12-28 10:56
  1. /*
  2. *练习四:取两个字符串中最大相同子串
  3. *思路:获取两个字符串中较短的那个,获取一个字符串从长到短的子串,判断子串是否在长串中
  4. */
  5.         public static String getMaxSubString(String s1,String s2){
  6.                 String max = (s1.length()>s2.length())?s1:s2;
  7.                 String min = (max==s1)?s2:s1;
  8.                 for(int x=0;x<min.length();x++){
  9.                         for(int y=0,z=min.length()-x;z!=min.length()+1;y++,z++){
  10.                                 String temp = s2.substring(y,z);
  11.                                 if(max.contains(temp)){
  12.                                         return temp;
  13.                                 }
  14.                         }
  15.                 }
  16.                 return "";
  17.         }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 15:29
  1.         Collection 接口 java.util包  JDK1.2出现
  2.                 |——List 接口
  3.                         |——ArrayList
  4.                         |——LinkedList
  5.                         |——Vector
  6.                 |——Set 接口
  7.                         |——HashSet
  8.                         |——TreeSet
  9.         每一个容器对数据的存储方式都不同,存储方式称为数据结构
复制代码



作者: 小爷邱烙    时间: 2015-1-8 15:53
  1.         boolean add(E e)//集合对象在堆内存中,和数组一样,存储的是元素在堆内存中的地址
  2.         boolean addAll(Collection c)//把指定集合中的元素添加到此集合中
  3.         void clear()//清空集合
  4.         boolean remove(Object o)//移除指定元素,只移除第一次出现的
  5.         boolean removeAll(Collection c)//移除交集
  6.         boolean contains(Object o)//判断是否包含该元素
  7.         boolean containsAll(Collection c)//必须包含所有元素才返回true
  8.         boolean equals(Object o)//集合与对象是否相同
  9.         boolean isEmpty()//集合是否为空
  10.         int size()//集合中元素数
  11.         boolean retainAll(Collection c)//取交集,仅保留两个集合中都有的元素,属于改
  12.         Object[] toArray()//返回所有元素的数组
  13.         <T> T[] toArray(T[] a)//按泛型返回数组,必须所有元素都是泛型指定类型
  14.         String toString()//Collection的toString方法重写了Object的toString方法,
  15.                                         //不再打印哈希值,而以一对[]中括号为边界,打印集合中元素
  16.         Collection的操作没有取出,需要一个方法
  17.          Iterator<T> iterator()//返回集合的迭代器,由迭代器做取出
复制代码



作者: 小爷邱烙    时间: 2015-1-8 16:14
  1.         Iterator<E> it = al.iterator();//返回一个Iterator的子类对象,至于是什么对象,已经被封装了,这里是多态
  2.         while(it.hasNext()){
  3.                 System.out.println(it.next());
  4.         }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 16:24
  1. for(Iterator it=al.iterator();it.hasNext();){
  2.         System.out.println(it.next());
  3. }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 16:35
  1. void add(int index,E e)
  2. boolean addAll(int index,Collection c)//在指定位置添加
  3. E get(int index)//获取
  4. int indexOf(Object o)
  5. int lastIndexOf(Object o)//判断位置
  6. ListIterator listIterator()//List特有迭代器
  7. ListIterator listIterator(int index)//从指定位置开始
  8. E remove(int index)
  9. E set(int index,E e)//替换
  10. List subList(int fromIndex,int toIndex)//包含头,不包含尾
复制代码



作者: 小爷邱烙    时间: 2015-1-8 16:38
  1. for(int x=0;x<al.size();x++){
  2.         System.out.println(al.get(x));
  3. }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 16:47
  1. void add(E e)//在被迭代的最后一个元素后添加
  2. int nextIndex()//获取下一个元素的角标
  3. void set(E e)//修改被迭代的最后一个元素
  4. boolean hasPrevious()//逆向迭代,同next
  5. E previous()
  6. int previousIndex()
  7. 这些都是ListIterator特有方法,因为只有List有角标
复制代码



作者: 小爷邱烙    时间: 2015-1-8 17:53
  1. void addFirst(E e)
  2. void addLast(E e)
  3. E getFirst()
  4. E getLast()
  5. E removeFirst()
  6. E removeLast()
  7. get系列方法:获取元素
  8. remove系列方法:获取元素,并且删除元素
  9. 一种由remove方法实现的获取,LinkedList特有方法
  10. while(!link.isEmpty()){
  11.         System.out.println(list.removeFirst())
  12. }
  13. 当LinkedList中为空时,remove,get抛出NoSuchElementException
复制代码



作者: 小爷邱烙    时间: 2015-1-8 17:57
  1. E pollFirst()//相当于remove
  2. E pollLast()
  3. E peekFirst()//相当于get
  4. E peekLast()
  5. boolean offerFirst(E e)//相当于add
  6. boolean offerLast(E e)
  7. 这一系列方法当List为空时,返回null,不抛出异常
复制代码



作者: 小爷邱烙    时间: 2015-1-8 18:03
  1. import java.util.LinkedList;

  2. /*练习:使用LinkedList模拟一个堆栈,队列数据结构
  3. *堆栈:先进后出
  4. *队列:先进先出
  5. */
  6. class DuiLie{
  7.         private LinkedList link;
  8.         DuiLie(){
  9.                 link = new LinkedList();
  10.         }
  11.         public void myAdd(Object obj){
  12.                 link.addFirst(obj);
  13.         }
  14.         public Object myGet(){
  15.                 return link.getLast();
  16.         }
  17.         public boolean isNull(){
  18.                 return link.isEmpty();
  19.         }
  20. }
  21. //javaweb中的session appication域都是这种封装方式
复制代码



作者: 小爷邱烙    时间: 2015-1-8 18:07
  1. import java.util.ArrayList;
  2. import java.util.Iterator;

  3. //练习:除去ArrayList中的重复元素
  4. class Test{
  5.         public static ArrayList singleElement(ArrayList<E> al){
  6.                 ArrayList newAl = new ArrayList();
  7.                 Iterator it = al.iterator();
  8.                 while(it.hasNext()){
  9.                         Object obj = it.next();
  10.                         if(!newAl.contains(obj))
  11.                                 newAl.add(obj);
  12.                 }
  13.                 return newAl;
  14.         }
  15. }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 18:16
  1. /*
  2. * 练习:将自定义对象作为元素存储到ArrayList中,并去除重复元素
  3. * Person同名同年龄视为同一个人,为重复元素
  4. * List集合的contains方法判断元素是否相同,用的是元素的equals方法
  5. * 由此,remove方法的寻找也用equals
  6. */
  7. class Person{
  8.         private String name;
  9.         private int age;
  10.         public Person(String name, int age) {
  11.                 super();
  12.                 this.name = name;
  13.                 this.age = age;
  14.         }
  15.         public String getName() {
  16.                 return name;
  17.         }
  18.         public int getAge() {
  19.                 return age;
  20.         }
  21.         public boolean equals(Object obj) {
  22.                 if (!(obj instanceof Person))
  23.                         return false;
  24.                 Person p = (Person) obj;
  25.                 return this.name.equals(p.name) && this.age==p.age;
  26.         }
  27. }
  28. //注意,在没有泛型的情况下,迭代器获取的是Object,需要做强转
复制代码



作者: 小爷邱烙    时间: 2015-1-8 18:36
  1. public int hashCode(){
  2.         return name.hashCode()+age*39;//防止出现40+20=20+40的情况,乘以一个任意数,尽量保证哈希值唯一。
  3. }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 18:55
  1. public int compareTo(Object o){
  2.         return 1;
  3. }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 19:03
  1. //使用构造函数 TreeSet(Comparator comparator)
  2. class Mycompare implements Comparator{
  3.         public int compare(Object o1,Object o2){}
  4. }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 19:22
  1. /*
  2. * 练习:向TreeSet中存入字符串,按字符串长度排序
  3. * String本身具备比较性,但不符合要求,要使用比较器
  4. */
  5. class StringLengthComparator implements Comparator{
  6.         public int compare(Object o1,Object o2){
  7.                 String s1 = (String)o1;
  8.                 String s2 = (String)o2;
  9.                 if(s1.length()>s2.length())
  10.                         return 1;
  11.                 if(s1.length()==s2.length())
  12.                         return s1.compareTo(s2);
  13.                 return -1;
  14.         }
  15. }
  16. /*
  17. * 可改为
  18. * int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
  19. * if(num==0)
  20. *   return s1.compareTo(s2);
  21. * return num;
  22. */
  23. /*
  24. * 可以使用匿名内部类
  25. * TreeSet ts = new TreeSet(new Comparator{
  26. *         public int compare(Object o1,Object o2){}
  27. * })
  28. */
复制代码



作者: 小爷邱烙    时间: 2015-1-8 19:42
  1. class Utils<T>{
  2.         private T t;
  3.         public void setObject(T t){
  4.                 this.t = t;
  5.         }
  6.         public T getObject(){
  7.                 return t;
  8.         }
  9. }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 20:32
  1. class InterImpl implements Inter<String>{
  2.         public void show(String str){
  3.                 System.out.println("show"+s);
  4.         }
  5. }
复制代码



作者: 小爷邱烙    时间: 2015-1-8 20:34
  1. //继承泛型:此时创建对象时明确泛型
  2. class InterImpl<T> implements Inter<T>{
  3.         public void show(T t){
  4.                 System.out.println("show"+t);
  5.         }
  6. }
复制代码



作者: 小爷邱烙    时间: 2015-1-9 11:08
  1. void chear()
  2. boolean containsKey(Object key)
  3. boolean containsValue(Object value)
  4. V get(Object key)//获取返回null可用于判断键是否存在
  5. boolean isEmpty()
  6. V put(K key,V value)//当存相同键时,新值替换老值,返回老值,首次存入键返回null
  7. void putAll(Map m)
  8. V remove(Object key)
  9. int size()
  10. Collection values()
  11. Set entrySet()
  12. Set keySet()
  13. String toString()//返回的是{key=value,key=value}
复制代码



作者: 小爷邱烙    时间: 2015-1-9 11:17
  1. Set<String> keySet = map.keySet();
  2. Iterator<String> it = keySet.iterator();
  3. while(it.hasNext()){
  4.         String key = it.next();
  5.         String value = map.get(key);
  6.         System.out.println(key+"..."+value)
  7. }
  8. //keySet返回本集合所有键,存入Set中
复制代码



作者: 小爷邱烙    时间: 2015-1-9 11:20
  1. Set<Map.Entry<String,String>> entrySet = map.entrySet();
  2. Iterator<Map.Entry<String,String>> it = entrySet.iterator();
  3. while(it.hasNext()){
  4.         Map.Entry<String,String> me = it.next();
  5.         String key = me.getKey();
  6.         String value = me.getValue();
  7. }
复制代码



作者: 小爷邱烙    时间: 2015-1-9 11:40
  1. import java.util.Iterator;
  2. import java.util.Map;
  3. import java.util.Set;
  4. import java.util.TreeMap;

  5. //练习:获取字母出现次数
  6. class Test{
  7.         public static String charCount(String str){
  8.                 char[] chs = str.toCharArray();
  9.                 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
  10.                 int count = 0;
  11.                 for(int x=0;x<chs.length;x++){
  12.                         if(!((chs[x]>='a' && chs[x]<='z') || (chs[x]>='A' && chs[x]<='Z')))
  13.                                 continue;
  14.                         Integer value = tm.get(chs[x]);
  15.                         if(value!=null)
  16.                                 count = value;
  17.                         count++;
  18.                         tm.put(chs[x], count);
  19.                         count = 0;
  20.                 }
  21.                 StringBuilder sb = new StringBuilder();
  22.                 Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
  23.                 Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();
  24.                 while(it.hasNext()){
  25.                         Map.Entry<Character, Integer> me = it.next();
  26.                         Character ch = me.getKey();
  27.                         Integer value = me.getValue();
  28.                         sb.append(ch+"("+value+")");
  29.                 }
  30.                 return sb.toString();
  31.         }
  32. }
复制代码



作者: 小爷邱烙    时间: 2015-1-9 12:03
  1. static <T extends Comparable<? super T>> void sort(List<T> list)
  2.   //当List元素具备比较性时,对List排序。也可以传比较器
  3.   static <T> void sort(List<t> list,Comparator<? super T> )
  4. static <T extends Object&Comparable<? super T>> T max(Collection<? extends T> coll)
  5.   //根据自然排序,取集合的最大元素,也可以传比较器。min方法类似
  6. static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key)
  7.   //对集合进行二分查找。也可以使用比较器。注意使用比较器时,排序和二分查找要用同一个比较器。
  8.   //二分查找的前提是集合有序。二分查找当找不到时,返回-插入点-1
  9. static <T> void fill(List<? super T> list,T obj)
  10.   //用指定元素obj替换集合中的所有元素
  11. static boolean replaceAll(List<T> list,T oldVal,T newVal)
  12.   //替换指定元素
  13. static void reverse(List<?> list)
  14.   //反转集合
  15. static <T> comparator<T> reverseOrder()
  16.   //返回一个自然排序的逆序比较器
  17. static <T> comparator<T> reverseOrder(Comparator<T> cmp)
  18.   //将一个比较器逆转返回
  19. static Collection<T> synchronizedCollection(Collection<T> c)
  20.   //一系列方法将传入的集合转成线程同步的并返回,其底层实现用同锁同步代码块,Collection,Map,List,Set都有该方法
  21. static void swap(List<?> list,int i,int j)
  22.   //交换元素位置
  23. static void shuffle(List<?> list)
  24.   //随机重置集合中元素位置
复制代码



作者: 小爷邱烙    时间: 2015-1-9 12:22
  1. static int binarySearch(int[] a,int key)
  2.   //一系列方法,二分查找,也可以设置起始位置,结束位置。byte[],char[],double[],float[]
  3.   //long[],Object[],short[],T[]都有该方法
  4. static int[] copyOf(int[] arr,int newLength)
  5.   //一系列方法,数组复制。各数据类型都有。copyOfRange(int[] arr,int from,int to)复制一个范围
  6. static boolean equals(int[] a,int[] b)
  7.   //一系列方法,比较数组。比较的是元素,要元素和顺序都一致。各类型都有。
  8. static void fill(int[] a,nt val)
  9.   //一系列方法,替换数组值,可指定范围,各类型都有
  10. static void sort(int[] a)
  11.   //一系列方法,排序,可指定范围,各类型都有
  12. static String toString()
  13.   //一系列方法,返回数组内容的字符串
复制代码



作者: 李票    时间: 2015-1-9 19:55
谢谢楼主分享,有的看过了有的学习了
作者: 小爷邱烙    时间: 2015-1-11 11:04
  1. static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
  2.   //修改dest数组,从srcPos开始截取,截取length个,从destPos开始覆盖
  3. static long currentTimeMillis()
  4.   //返回当前时间毫秒值
  5. static void exit(int status)
  6.   //终止JVM  等效于 Runtime.getRuntime().exit(n)
  7. static void gc()
  8.   //执行垃圾回收器
  9. static Properties getProperties()
  10.   //System类会描述系统环境,记录系统启动时的加载信息。该方法获取系统属性信息。
  11.   //获取的JVM,JDK以及当前操作系统的一些信息
  12.   Properties:HashTable的子类,与IO相关。其作为一个集合,可以取也可以存。
  13.   //注意该Properties取出时是<Object,Object>,存入时则是<String,String>
  14.   //java的一个命令参数java -Dkey=value能在启动JVM时动态的为系统添加一些信息。
复制代码



作者: 小爷邱烙    时间: 2015-1-11 11:42
  1. static int abs(int a)//取绝对值,float long double都有
  2. static double ceil(double a)//入,注意小数
  3. static double floor(double a)//舍,注意小数
  4. static int round(float a)//四舍五入 long-double也有
  5. static double pow(double a,double b)//a的b次方
  6. static double random()//返回一个0.0~1.0的随机小数,包含0不包含1
  7.   java.util包中有一个Random类,可以直接new,其全部方法就是各种next。
  8.     常用int r.nextInt(n)  0~n的随机数,不包含n
复制代码



作者: Afridoce    时间: 2015-1-11 14:54
lz加油~~~~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2