JAVA學習筆記——JAVA基礎文法(五)

來源:互聯網
上載者:User

JAVA的一些簡單的常用類介紹

1、String類  代表不可變的字串序列

  String str1 = new (“hello”); 和 String str = “hello”的區別 前者是建立一個對象儲存在堆當中,而後者的字串是放在靜態一個記憶體地區當中

  思考:上面的str1和str2相等嗎?為什嗎?

  瞭解String類當中的各種構造方法,記下String類中常用的方法

  比如:

charAt(int index)  返回在index位置出的字元

endsWith(String suffix)  判斷字串是否是以該字串結束

startsWith(String prefix)   判斷該字串是否是以該字串開始

equalsIgnoreCase(String anotherString)  將此 String 與另一個 String 比較,不考慮大小寫。

indexOf(int ch, int fromIndex) 返回在此字串中第一次出現指定字元處的索引,從指定的索引開始搜尋。

replace(char oldChar, char newChar) 返回一個新的字串,它是通過用 newChar 替換此字串中出現的所有 oldChar 得到的。

substring(int beginIndex)

返回一個新的字串,它是此字串的一個子字串。

trim()

返回字串的副本,忽略前置空白和尾部空白。

toLowerCase(Locale locale)

使用給定 Locale 的規則將此 String 中的所有字元都轉換為小寫。

toUpperCase()

使用預設語言環境的規則將此
String 中的所有字元都轉換為大寫。

split(String regex)

根據給定Regex的匹配拆分此字串。

2、StringBuffer類
可變的字串序列

  適用於經常可變的一個字元序列,效率比String高  允許字串追加在後面也允許刪除某個字串

     初始化以及簡單的使用語句為:

              StringBuffer str = new StringBuffer(“hello”);  建立一個對象

              Str.append(“world”); 
直接在hello後面追加一個world

      控制台最後輸出效果為helloworld   

3、封裝類

  Int 的封裝類是Integer

  現在的JAVA5.0給我們提供了自動裝箱和自動拆箱

  Integer I = 10;      這叫做自動裝箱

  Int n = I;             這叫做自動拆箱

 

  下面代碼簡單的呈現了int封裝類將字串數群組轉換成int類型的過程

public class test{

       public
static void main(String[] args){

       int
num1 = Integer.parseInt(args[0]);

       int
num2 = Integer.parseInt(args[1]);

       int
num3 = num1 + num2;

       System.out.println(num3);

       }

}

  Char的封裝類是Character

4、Math類(全部是靜態方法)

       產生隨機數的方法:

       1)、Math類

      Math.random()
     
  返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0。(懂得靈活轉換成擷取其他數位函數)

      2)、JAVA.util.Random包下

    New Randow.nextInt(int n)

    返回一個偽隨機數,它是取自此隨機數產生器序列的、在 0(包括)和指定值(不包括)之間均勻分布的
int 值。

      abs(資料類型  a)

  返回 資料類型 值的絕對值。

5、JAVA.io.File

  瞭解如何建立檔案夾包括建立單個和多個檔案夾,使用方法mkdir和mkdirs;瞭解listFile()和list()的區別 list()是輸出某個目錄下所包含的子目錄

  如下程式所示

public class test{

import java.io.File;

public class test{

       public
static void main(String[] args){

       File
file = new File("e:/jxauzhongkewen");

       //file.mkdirs();
在某個路徑下建立多個檔案夾

       //file.mkdir();
在某路徑下建立單個檔案夾

       File[]
fs = file.listFiles();  //迴圈輸出某個檔案夾下的目錄

       for(int
i = 0;i < fs.length;i++){

              System.out.println(fs[i]);

       }

       }

}

 

以下這個也是File類當中最常用的思想,採用樹形結構的思想將檔案內部包含的檔案層次化顯示出來:

import java.io.File;

public class test{

       public
static void main(String[] args)throws Exception{

             

              test.show(new
File("e:/zhongkewen"),0);

      

       }

      

       public
static void show(File f,int level){

              StringBuffer
str = new StringBuffer("");

              for(int
i = 0;i < level;i++){

                     str.append("
");

              }

             

              File[]
fs = f.listFiles();

              for(int
i = 0;i < fs.length;i++){

                     System.out.println(str.append(fs[i].getName()));

              if(fs[i].isDirectory()){

                     show(fs[i],++level);

              }

       }

       }

}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.