(String类、static关键字、ArrayList类、Math类)

版权声明:Java基础,个人笔记。 https://blog.csdn.net/liu19951104/article/details/81501503

1.String类

java.lang.String类代表字符串,Java程序中所有的字符串文字(例如“abc”)都可以被看做是实现此类的实例。

特点

  • 字符串不变:字符串的值在创建后不能被更改
  • 因为String对象是不可变的,所以它们可以被共享
  • "abc"等效于char[ ] data = {'a','b','c'}
String str = "abc";
//上面的相当于
char data[] = {'a','b','c'};
String str = new String (data);
//String底层是靠字符数组实现的

构造举例:

//无参构造
String str = new String ();

//通过字符数组构造
char chars[] = {'a','b','c'};
String str2 = new String (chars);

//通过字符数组构造
byte bytes[] = {97,98,99};
String str3 = new String(bytes);

常用方法

判断功能的方法

  • public boolean equals (objects anobject):将此字符串与指定对象进行比较
  • public boolean equalsIgnoreCase(String anotherString):将此字符串与指定对象进行比较,忽略大小写
String s1 = "hello";
String s2 = "hello";
String s3 = "Hello";

//boolean equals(objects obj):比较字符串内容是否相同
System.out.println(s1 equals(s2));// true
System.out.println(s1 equals(s2));// false
System.out.println("------------------------------");

//boolean equalsIngoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println(s1 equalsIngoreCase(s2));// true
System.out.println(s1 equalsIngoreCase(s3));// true
System.out.println("------------------------------");

获取功能的方法

  • public int length();:返回此字符串的长度
  • public String concat (String str) :将此字符串连接到该字符串的末尾
  • public char charAt (int index) :返回指定索引处的char值
  • public int indexof (String str):返回指定子字符串第一次出现在该字符串内的索引
  • public Sring substring (int beginIndex):返回一个子字符串,从beginIndex开始截取字符串到字符串结尾
  • public Sring substring (int beginIndex, int endIndex):返回一个子字符串,从beginIndex 到endInedx截取字符串。含beginIndex,不含endInedx

方法演示如下:

public static void main(String[] args){
    //创建字符串对象
    String s = "helloworld";
    
    //int length():获取字符串的长度,其实也就是字符串的个数
    System.out.println(s.length());
    System.out.println("----------------------------");

    //String concat (String str):将指定的字符串连接到该字符串连接到该字符串的末尾
    String s1 = "helloworld";
    String s2 = s.concat("**hello itheima");
    System.out.println( s2 );//  helloworld**hello ithemia 

    // char charAt(int index):获取指定索引处的字符
     System.out.println( s.charAt(0) );
     System.out.println( s.charAt(1) );
     System.out.println( "-------------------------" );

    // int indexof(String str):获取str在字符串对象中第一次出现的索引,没有则返回 -1
     System.out.println( s.indexof("1") );
     System.out.println( s.indexof("owo") );
     System.out.println( s.indexof("ak") );
     System.out.println( "-------------------------" );

    // String substring (int start): 从start开始截取字符串到字符串结尾
     System.out.println( s.substring(0) );
     System.out.println( s.substring(5) );
     System.out.println("---------------------------");

    //String substring (int start,int end):从start到end截取字符串,含start,不含end
     System.out.println( s.substring(0,s.length() ) );
     System.out.println( s.substring(3,8) );
}

转换功能的方法

  • public char[ ] toCharArray () :将此字符串转换成新的字符数组
  • public byte[ ] getBytes():通过当前参数中的字符数组来构造新的String
  • public String replace (CharSequence target , CharSequence replacement):将与target匹配的字符串使用replacment字符串替换
public static void main(String[] args){ 
    //创建字符串对象
    String s = "abcde";

    //char[] toCharArray():把字符串转化为新的字符数组
    char[] chars = s.toCharArray();
    for(int i = 0;i <chars.length;i++){
        System.out.println(chars [i] );
    }

    //byte[]  getByte():把字符串转化为字节数组
     byte[] bytes = s.getByte();
     for(int i = 0;i <bytes.length;i++){
        System.out.println(bytes [i] );
    }

    //替换字母it为大写IT
    String str = "itcast itheima";
    String replace = str.replace("it","IT");
    System.out.println( replace );// ITcast ITheima
}

分割功能的方法

  • public String[ ] split(String regax):将此字符串按照给定的 regax(规则)拆分成字符串数组
public static void main(String[] args){
    //创建字符串对象
    String s = "aa|bb|cc";
    String[] strArray = s.split("|"); //  ["aa","bb","cc"]
    for(int x = 0; x < strArray.length; x++){
     System.out.println(strArray[x]);// aa bb cc
    }
}

static关键字

关于static 关键字的使用,他可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不单单是属于某个对象的,也就是说,既然是属于类的,就不可以靠着创建对象来调用了。

定义和使用格式

当static 修饰成员变量时,该变量称为类变量。该类的每个对象都共享同一个类变量的值。任何对象都可以更改该类变量的值,但也可以在不创建该类的对象的情况下对类变量进行操作

  • 类变量 :使用static关键字修饰的成员变量

定义格式:

static 数据类型  变量名;

举例

// 学生类
public class Student{
    private String name;
    private int age;
    //    学生的ID
    private int ID;
    //类变量,记录学生数据,分配学号
    public static int numberOfStudent = 0;

    public Student(String name, int age){
        this.name = name;
        this.age = age;
        //通过numberOfStudent 给学生分配学号
        this.age = ++numberOfStudent;

    //打印属性值
    public void show(){
        System.out.println("Student: name = "+ name+", age = "+age+",ID ="+ID);
        }
    }
}
//测试类
public class StuDemo{
    public static void main(String[] args){
        Student s1 = new Student("张三",23); 
        Student s2 = new Student("李四",24);
        Student s3 = new Student("王五",25);
        Student s4 = new Student("赵六",26);

         s1.show();// Student : name = 张三,age= 23,ID =1;
         s1.show();// Student : name = 李四,age= 24,ID =2;
         s1.show();// Student : name = 王五,age= 25,ID =3;
         s1.show();// Student : name = 赵六,age= 26,ID =4;
    }
}

静态方法

当static修饰成员方法时,该方程称为类方法。静态方法在生命中有static,建议使用类名来调用,而不需要创建类的对象。调用方式非常简单。

  • 类方法:使用static关键字修饰的成员方法,习惯称为静态方法

定义格式:

修饰符 static 返回值类型 方法名 (参数列表){
    // 执行语句
}

静态方法调用注意事项:

  • 静态方法可以直接访问类变量和静态方法
  • 静态方法不能直接访问普通成员变量或者成员方法。反之,成员方法可以直接访问类变量或静态方法
  • 静态方法中,不能使用this关键字
  • 静态方法只能访问静态成员

调用格式;

被static修饰的成员可以并且建议通过类名直接访问,虽然也可以通过对象名访问静态成员,原因在于多个对象均属于一个类,共享使用同一个静态成员,但是不建议,因为会出现警告信息

格式

//访问类变量
类名.类变量名

//调用静态方法
类名.静态方法名

例如

public class StuDemo02{
    public static void main(String[] args){
        // 访问类变量
        System.out.println(Student.numberOfStudent);

        // 调用静态方法
        Student.showNum();
    }
}

静态代码块

定义在成员位置,使用static修饰的代码块 { }

  • 位置:类中方法外
  • 执行:随着类的加载而执行且执行一次,优先于main方法和构造方法的执行

格式:

public class ClassName{
    static {
        //执行语句
    }
}

’作用:给类变量进行初始化赋值,演示如下:

public class Game{
    public static int number;
    public static ArrayList<String> list;

    static {
        //给类变量赋值
        number = 2;
        list = new ArrayList<String>();
        //添加元素到集合中
        list.add("张三");
        list.add("李四");  
    }
}

注意:

static关键字,可以修饰变量、方法和代码块。在使用的过程中,其主要目的还是想在不创建对象的情况下去调用方法。

Arrays 类

java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等,其所有方法均为静态方法,调用起来非常简单

操作数组的方法

  • public static String toString(int[ ] a):返回指定数组内容的字符串表示形式
public static void main(String[] args){
    //定义int数组
    int[] arr = {2,32,54,546,3,64,1};
    //打印数组,输出地址值
    System.out.println(arr); // [I@50cbc42f

    // 数组内容转为字符串
    String s = Arrays.tiString(arr);
    //打印字符串,输出内容
    System.out.println(s); // [2, 32, 54, 546, 3, 64, 1]
}
  • public  static void sort(int[] a):对指定的int 型数组按数字升序进行排序
public static void main(String[] args){
    //定义int数组
    int[] arr = {24,4,5,35,53,13,34,555,2};
    System.out.println("排序前"+Arrays.toString(arr)); // 排序前:[24, 4, 5, 35, 53, 13, 34, 555, 2]

    //升序排序
    Arrays.sort(arr);
    System.out.println("排序后"+Arrays.toString(arr)); // 排序后:[2, 4, 5, 13, 24, 34, 35, 53, 555]
}
//将一个随机字符串中的所有字符升序排列,并倒序打印
public static void main(String[] args){
    //定义随机字符串
    String line = "dkjafHDAHFddsakdpow";
    //转换成字符数组
    char[] chars = line.toCharArray();

    //升序排序
    Arrays.sort(chars);
    //反向遍历打印
    for(int i = chars.length -1; i >= 0; i++){
        System.out.print(chars[i]+" ");// w s p o k k j f d d d d a a H H F D A 
    }
    //倒序打印
    for(int startIndex = 0,endIndex = chars.length - 1;startIndex < endIndex; startIndex++,endIndex--){
        char temp = chars[startIndex];
        chars[startIndex] = chars[endIndex];
        chars[endIndex] = temp;
        }
        for(char i:chars){
          System.out.print(i +" ");// w s p o k k j f d d d d a a H H F D A
        }
}

Math 类

java.lang.Math 类包含于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单

基本运算方法

  • public static double abs (double a) :返回double值的绝对值
double d1 = Math.abs(-5);// d1的值为5
double d2 = Math.abs(5);// d2的值为5
  • public static double ceil (double a):返回大于等于参数的最小整数
double d1 = Math.ceil(3.3);//d1的值为4.0        
double d2 = Math.ceil(-3.3);//d1的值为-3.0    //向上取整
double d3 = Math.ceil(5.1);//d1的值为6.0
double d4 = Math.ceil(5.7);//d1的值为6.0
  • public static double floor (double a) :返回小于等于参数的最大整数
double d1 = Math.floor(3.3);//d1的值为3.0        
double d2 = Math.floor(-3.3);//d1的值为-4.0    //向下取整
double d3 = Math.floor(5.1);//d1的值为5.0
double d4 = Math.floor(5.7);//d1的值为5.0
  • public static long round (double a):返回最接近参数的long值(即相当于四舍五入法)
long d1 = Math.round(5.5);// d1的值为6
long d2 = Math.round(5.4);// d2的值为5
long d3 = Math.round(-5.5);// d3的值为-5 //特殊情况,为什么????
long d4 = Math.round(-5.4);// d4的值为-5
//计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个
public static void main(String[] args){
    //定义最小值
    double min = -10.8;
    //定义最大值
    double max = 5.9;
    //定义变量计数
    int count = 0;
    //范围内循环
    for(double i = Math.ceil(min); i <= max; i++){
        if (Math.abs(i) > 6 || Math.abs(i) < 2.1){
            //计数
            count++;
        }
    }
    System.out.println("个数为:"+ count+ "个");
}

猜你喜欢

转载自blog.csdn.net/liu19951104/article/details/81501503