JAVA学习笔记——JAVA基础语法(五)

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)
根据给定正则表达式的匹配拆分此字符串。

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);

              }

       }

       }

}

 

 

posted @ 2013-01-30 17:19  bod08zhongkewen  阅读(273)  评论(4编辑  收藏  举报