夢想帝國

          音樂,程序

           

          2012年10月28日

          用最少的循環次數求出兩個數組中的相同元素

          問題:有兩個長度分別為M和N的數組,用最少的循環次數求出他們中的相同元素。
          分析:用容器來解決。將這兩個數組分別放在兩個ArrayList中,再調用ArrayList的retainAll方法即可。
                  A.retainAll(B):從A集合中刪除B集合中不包含的元素.
          實現代碼如下:

          package com.miaojian.li;

          import java.util.ArrayList;
          import java.util.Collection;

          public class FindFromArray {

           
              public static void main(String[] args) {

              int[] array1={1,2,3,4,5,6,7};
              int[] array2={2,4,6,8,10,1};
              Collection c1=new ArrayList();
              Collection c2=new ArrayList();
            
              for(int i=0;i<array1.length;i++)    //將array1添加到c1中
              {
                  c1.add(array1[i]);
              }
              for(int i=0;i<array2.length;i++)   //將array2添加到c2中
             {
                  c2.add(array2[i]);
             }
             c1.retainAll(c2);          //從c1集合中刪除c2集合中不包含的元素
             System.out.println(c1);
             }
          }
          因此總的循環次數是M+N
          結果如下 :

          [1, 2, 4, 6]

          posted @ 2012-10-30 22:34 天鷹之翼 閱讀(321) | 評論 (1)編輯 收藏

          用遞歸的方法列出文件目錄結構

          package com.miaojian.li;

          import java.io.File;

          public class FileList {
           
              public static void main(String[] args) {
            
              File f= new File("d:/software");//待列出的文件目錄
              tree(f,0);
              }
              private static void tree(File f, int level)
              {
                  String preStr="";
            
                  for(int i=0;i<level;i++)
                  {
                      preStr+="    ";
                  }
                  File[] childs=f.listFiles();
                  for(int i=0;i<childs.length;i++)
                 {
                     System.out.println(preStr + childs[i].getName());
                     if(childs[i].isDirectory())
                     tree(childs[i],level+1);
                 }
             }

          }
          結果如下圖:

          DataBase
              Mysql
                  mysqltoolwin32.rar
                  MySQL_5.5.20_win32_XiaZaiBa.zip
              Oracle
                  Oracle_client_win32.zip
                  Toad DBA Suite for Oracle 10.5 Commercial.exe
              SqlServer
                  Power Design
                      powerdesigner125_eval.exe
                  SQL Server2000
                      PERSONAL
                          AUTORUN.EXE
                          AUTORUN.INF
                          AUTORUN.INI
                          BOOKS

           


          posted @ 2012-10-30 11:59 天鷹之翼 閱讀(129) | 評論 (0)編輯 收藏

          測試static修飾的方法是否可以被復寫

          package com.miaojian.li;
          //測試static修飾的方法是否可以被復寫。
          public class TT {

           public static void getName()
           {
            System.out.println("TT");
           }
           public static void main(String[] args) {
            
            TT tt=new TT();
            tt.getName();
           }
          }

          class T extends TT
          {
           public static void getName()
           {
            System.out.println("T");
           }
          }


          運行父類,得到的結果為:TT
          運行子類,得到的結果為:TT

          證明staic修飾的方法不能被復寫。

          posted @ 2012-10-29 21:48 天鷹之翼 閱讀(150) | 評論 (0)編輯 收藏

          Java中類變量和實例變量的區別

          類變量也叫靜態變量,也就是在變量前加了static 的變量;
          實例變量也叫對象變量,即沒加static 的變量;
          區別在于:
              類變量和實例變量的區別在于:類變量是所有對象共有,其中一個對象將它值改變,其他對象得到的就是改變后的結果;而實例變量則屬對象私有,某一個對象將其值改變,不影響其他對象;
          例:

          class A{
              static  int a = 0; //類變量
              public int b = 0; //實例變量
          }

          public class Test{
              public static void main (String[] args){
                  A a1 = new A();
                  A a2 = new A();
                  a1.a = 3;  // 等同于 A.a = 3;
                  a1.b = 4 ;
                  System.out.println(a2.a); //結果為3
                  //類變量是針對所有對象的,所以a1改變a,a2的a也改變
                  System.out.println(a2.b); //結果為0
                  //實例只改變自身的,所以a1對象的b改變,不影響對象a2的b變量
              }
          }

          posted @ 2012-10-28 21:52 天鷹之翼 閱讀(164) | 評論 (0)編輯 收藏

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 牡丹江市| 如东县| 台中县| 新昌县| 托里县| 古蔺县| 萝北县| 长沙县| 呈贡县| 浦东新区| 聊城市| 乌兰浩特市| 屏东市| 永春县| 栖霞市| 定南县| 呼和浩特市| 宜春市| 锡林郭勒盟| 大安市| 准格尔旗| 深泽县| 县级市| 贵州省| 化州市| 阜南县| 古浪县| 建始县| 普陀区| 巴南区| 建宁县| 沙坪坝区| 汪清县| 栖霞市| 双峰县| 黄平县| 彭阳县| 景东| 鄂托克旗| 杨浦区| 阜城县|