码迷,mamicode.com
首页 > 其他好文 > 详细

06-常用类

时间:2018-01-07 20:15:12      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:常见   markdown   mat   忽略   nal   ast   对象引用   dir   char   

String类

  1. String 类是不可变的字符序列,String 字符串一旦分配好就不能改变其内容和长度了。(如果使用 s1+=s2; 并不是在s1的后面开辟空间将s2拷贝其内,而是另外开辟一个空间,将s1和s2拷贝其内。)
  2. String类是 final 的,不可以被继承。
  3. 任意一个字符串都是 String 类的对象。
  • String 类的常见构造方法
String(String original) // 创建一个String对象为original的拷贝
String(char[] value) // 用一个字符数组创建一个String对象
String(char[] value, int offset, int count) // 用一个字符数组从offset项开始的count个字符序列创建一个String对象

注意:

public class Test {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        String s3 = "hello";
        System.out.println(s1 == s3); // true
        // 因为 s1 和 s3 不是 new 出来的,s1 和 s3 指向同一片静态区域。

        s1 = new String("hello");
        s2 = new String("hello");
        System.out.println(s1 == s2); // false
        System.out.println(s1.equals(s2)); // true
        // s1.equals(s2) 是 true 的,我们知道,如果是 Object 类的话,只有 s1 == s2 的情况下才会使得
        // s1.equals(s2) 是 true,那么我们就要看 String 类有没有重写 equals 方法,查 API 确实重写了。

        char c[] = {‘s‘, ‘u‘, ‘n‘, ‘ ‘, ‘j‘, ‘a‘, ‘v‘, ‘a‘};
        String s4 = new String(c);
        String s5 = new String(c, 4, 4);
        System.out.println(s4);
        System.out.println(s5);

    }
}
  • String 类常用方法一

    public char charAt(int index) // 返回字符串中第 index 个字符
    public int length() // 返回字符串的长度
    public int indexOf(String str) // 返回字符串中出现 str 的第一个位置
    public int indexOf(String str, int fromIndex) // 返回字符串中从fromeIndex 开始出现str的第一个位置
    public boolean equalsIgnoreCase(String another) // 比较字符串 another 是否一样(忽略大小写)
    public String replace(char oldChar, char newChar) // 在字符串中用 newChar 字符替换 oldChar 字符

    举例:

    public class Test {
    public static void main(String[] args) {
        String s1 = "java", s2 = "Java";
        System.out.println(s1.charAt(1)); // u
        System.out.println(s1.length()); // 8
        System.out.println(s1.indexOf("java")); // 0
        System.out.println(s1.indexOf("Java")); // -1
        System.out.println(s1.equals(s2)); // false
        System.out.println(s1.equalsIgnoreCase(s2)); // true
    
        String s = " C programmer";
        String sr = s.replace(‘C‘, ‘java‘);
        System.out.println(sr); // java programmer
    }
    }
  • String 类常用方法二

public boolean startsWith(String prefix) // 判断字符串是否以 prefix 字符串开头
public boolean endsWith(String prefix) // 判断字符串是否以 prefix 字符串结尾
public String toUpperCase() //返回一个字符串为该字符串的大写形式
public String toLowerCase() //返回一个字符串为该字符串的小写形式
public String substring(int beginIndex) // 返回该字符串从 beginIndex 开始到结尾的字符串
public String substring(int beginIndex, int endIndex) // 返回该字符串从 beginIndex 开始到 endIndex 结尾的字符串
public String trim() // 返回将该字符串去掉开头和结尾空格后的字符串
  • String 类常用方法三

  • 静态重载方法

java public static String valueOf(...) //可以将基本类型的数据转换成字符串; //例如: public static String valueOf(double d) public static String valueOf(int i)

?

  • public String[] split(String regex) 可以将一个字符串按照指定的分隔符分割,返回分割后的字符串数组。

StringBuffer 类

  1. StringBuffer 类是可变的字符序列(和 String 类做比对)。
    如果使用 s1+=s2; 是在s1的后面开辟空间将s2拷贝其内,(与String做对比)。
  • StringBuffer 常用方法一:
    重载方法:

public StringBuffer append(...) 可以为该 StringBuffer 对象添加字符序列,返回添加后的该 StringBuffer 对象引用,例如:

public StringBuffer append(String str)
public StringBuffer append(StringBuffer sbuf)
public StringBuffer append(char[] str)
public StringBuffer append(char[] str, int offset, int len)
public StringBuffer append(double d)
public StringBuffer append(Object obj)
  • StringBuffer 常用方法二:
    重载方法 public StringBuffer insert(...) 可以为该 StringBuffer 对象在指定位置插入字符序列,返回修改后的该 StringBuffer 对象引用。例如:

    public StringBuffer insert(int offset, String str)
    public StringBuffer insert(int offset, double d)

方法 public StringBuffer delete(int start, int end) 可以删除从 start 开始到 end-1 为止的一段字符序列,返回修改后的该 StringBuffer 对象引用。

方法 public StringBuffer reverse() 用于将字符序列逆序,返回修改后该 StringBuffer 对象引用。

基本数据类型包装类

  • 基本的数据类型都是分配在栈上的,如果你想让他们分配在堆上面,需要使用到基础数据类型包装类,将其封装为一个对象。

  • 查看 API 文档即可。

// 编写一个方法,返回 double 类型的二维数组,数组中的元素通过解析字符串参数获得,如字符串参数:"1,2;3,4,5;6,7,8"
// 对应的数组为:d[0][0] = 1.0; d[0][1] = 2.0; 
// d[1][0] = 3.0; d[1][1] = 4.0; d[1][2] = 5.0; 
// d[20][0] = 6.0; d[2][1] = 7.0; d[2][2] = 8.0; 

public class Test {
    public static void main(String[] args) {
        String s = "1,2;3,4,5;6,7,8";

        String[] sp = s.split(";");
        String[] spp = null;

        double arr[][] = new double[sp.length][]; 

        for(int i=0; i<sp.length; i++) {
            spp = sp[i].split(",");
            arr[i] = new double[spp.length];

            for(int j=0; j<spp.length; j++) {
                arr[i][j] = double.parseDouble(spp[j]);
            }
        }

        for(int i=0; i<arr.length; i++) {
            for(int j=0; j<arr[i].length; j++) {
                System.out.println(arr[i][j]);
            }
        }
    }
}

Math 类


File 类

  • java.io.File 类表示系统文件名(一个加路径的文件名)

  • File 类是对这个文件名进行的操作,比如删除一个文件,得到一个文件的路径等,而不能对这个文件里面的内容进行操作,对文件内容操作要使用到io流。

  • File 类的构造方法:
    ```java
    File(String pathname) // 以 pathname 为路径穿件 File 对象
    File(String parent, String child) // 以 parent 为父路径,child 为子路径创建 File 对象。

```

  • File 的静态属性 String separator 存储了当前系统的分隔符。

  • File 类的常用方法

  1. 通过 File 对象可以访问文件的属性
public boolean canRead()
public boolean canWrite()
public boolean exists()
public boolean isDirectory()
public boolean isFile()
public boolean isHidden()
public long lastModified()
public long length()
public String getName()
public String getPath()
  1. 通过 File 对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)
public boolean createNewFile() throws IOException // createNewFile 不是 creatNewFile
public boolean delete()
public boolean mkdir()
public boolean mkdirs() // 创建路径中的一系列目录

举例:

import java.io.*;
public class TestFile {
  public static void main(String[] args) {
    String separator = File.separator;
    String filename = "myfile.txt";
    String directory = "mydir1" + separator + "mydir2";
    //String directory = "mydir1/mydir2";
    //String directory = "mydir1\\mydir2";
    File f = new File(directory, filename);
    if (f.exists()) {
      System.out.println("文件名:" + f.getAbsolutePath());
      System.out.println("文件大小:" + f.length());
    } else {
      f.getParentFile().mkdirs();
      try {
        f.createNewFile();
      } catch (IOException e) {
       e.printStackTrace();
      }
    }
  }
}

Tips: 分隔符写‘/‘在Windows 和 Linux 下都可以使用,而‘\‘只能在 Windows 下使用。

06-常用类

标签:常见   markdown   mat   忽略   nal   ast   对象引用   dir   char   

原文地址:https://www.cnblogs.com/lvonve/p/8228442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!