java常用类

目录

        1 String类

1字符串的构造方法:

2.常量池

3 .字符串的比较

1 equals

2 equalsIgnorecase

3.字符串长度:

4.拼接字符串

5.获取单个字符:

6.子字符串的索引(首个)

7.截取字符串

8.将String的转换为数组:

9.replace用法

10.split()用法

11.String的具体练习:

2.Arrays类

1.将数组变成字符串

2.sort 升序排序

3.Math类

1.abs绝对值

2.ceil向上取整

3.floor向下取整

4.round四舍五入

5.max/min最大最小

4.Scanner类

  1.导入包:

2.创建对象:eg:myObj

3.调用对象:

5.Random类

6 ArrayList类

1 创建对象list

2 添加add

3 删除remove

4 输出

5 长度size

6 提取get

7 排序sort

8 倒序输出

9 更改元素

10 将一个数组变成一个ArrayList类创建的对象

11 截取部分元素

7 StringBuffer类与String类结合使用

构造方法

1 使用StringBuffer创建对象

2 append添加

3 delete删除

4 reverse倒置

5 charAt提取字符

6 replace区间字符替换

7 substring提取字符串

8 insert插入字符串

9 toString 变成字符串


几个特殊类;(String,Arrays,math,Scanner,Random,ArrayList,StringBuffer)

1 String类

1字符串的构造方法:

public class an2{
    public static void main(String[] args){
        //直接构造
        String stringArray = "anxian";
        System.out.println(stringArray);
        //使用空参构造
        String stringArray2 = new String();
        stringArray2="anxian";
        System.out.println(stringArray2);

        //使用字符数组创建
        char [] stringArray3 = {'a','b','c'};
        String stringArray4 = new String(stringArray3);
        System.out.println(stringArray4);
        
        //使用字节数组创建
        byte [] stringArray5 = {97,98,99};
        String stringarray6 =new String(stringArray5);
        System.out.println(stringarray6);

运行结果:

2.常量池

Java基本类型和引用类型的区别_java基本类型和引用类型区别-CSDN博客

3 .字符串的比较

(这种情况不受构造方法的影响)

(常量尽量写在前面,以免出现null值报错)

1 equals
public class an2{
    public static void main(String[] args){
        String Str = "anxian";

        String Str2="anxian1";

        System.out.println(Str.equals(Str2));

​

运行结果:

2 equalsIgnorecase

(这种方法忽略大小写)

import java.util.*;
public class an2{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String axStr = input.nextLine();

        String Str = "anxian";

        System.out.println(axStr.equalsIgnoreCase(Str));

运行结果:

3.字符串长度:

import java.util.*;
public class an2{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String axStr = input.nextLine();

        int axLength = axStr.length();

        System.out.println(axLength);

运行结果:

4.拼接字符串

import java.util.*;
public class an2{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String axStr = input.nextLine();

        String axStr2 = axStr.concat(" World");

        System.out.println(axStr2);

运行结果:

5.获取单个字符:

可选择指定索引(下标)位置。

import java.util.*;
public class an2{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String axStr = input.nextLine();

        char axChar = axStr.charAt(2);

        System.out.println(axChar);

运行结果:

6.子字符串的索引(首个)

import java.util.*;
public class an2{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String axStr = input.nextLine();

        int numAX = axStr.indexOf("an");

        System.out.println(numAX);

运行结果:

7.截取字符串

import java.util.*;
public class an2{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String axStr = input.nextLine();

        String ax = axStr.substring(2);//截取2:-1

        String axXian = axStr.substring(1,3);//截取1:3

        System.out.println(ax+" "+axXian);

运行结果:

8.将String的转换为数组:

字符char数组

字节byte数组

public class an4 {
    public static void main(String[] args){
        String aXian = "Hello World";

        char [] charaXian = aXian.toCharArray();

        byte [] byteaXian = aXian.getBytes();

        for (char ch : charaXian){
            System.out.print(ch+" ");
        }
        System.out.println();
        for (byte b : byteaXian){
            System.out.print(b+" ");
        }
    }
}

运行结果:

9.replace用法

public class an4 {
    public static void main(String[] args){
        String aXian = "Hello World My friends";
        String language = "会不会玩,你个大傻逼";

        String MaXian = aXian.replace(" ","|");

        String Mlanguage =language.replace("傻逼","**");

        System.out.println(MaXian);
        System.out.println(Mlanguage);
    }
}

运行结果:

10.split()用法

split()的参数实际上是一个正则表达式

public class an4 {
    public static void main(String[] args) {
        String aXian = "Hello World My friends";

        String[] ax = aXian.split(" ");//String数组


        for (String i :ax){
            System.out.print(i+"\t");
        }
    }
}

运行结果:

11.String的具体练习:

输入一个字符串然后统计这个字符串中大写小写数字以及其他出现的次数。

import java.util.*;
public class an5 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String axStr = input.nextLine();
        char [] axChar = axStr.toCharArray();
        int aStr = 0;
        int AStr = 0;
        int IStr = 0;
        int OStr = 0;
        for (int i = 0; i < axChar.length; i++) {
            char ch = axChar[i];
            if (ch >= 'A' &&  ch <= 'Z') {
                AStr += 1;
            }else if (ch >= 'a' &&  ch <= 'z') {
                aStr += 1;
            }else if (ch >= '0' &&  ch <= '9') {
                IStr += 1;
            }else{
                OStr += 1;
            }
        }
        System.out.println("大写"+AStr);
        System.out.println("小写"+aStr);
        System.out.println("数字"+IStr);
        System.out.println("其他"+OStr);
    }
}

运行结果:

2.Arrays类

java.util.Arrays;是一个与数组相关的工具类,里面提供了大量的静态方法,用来实现数组常见操作,

1.将数组变成字符串

将参数数组变成字符串,按照默认格式【元素1,元素2,元素3~~~】

import java.util.Arrays;
public class an6 {
    public static void main(String[] args) {
        int[] axInt = {1,2,3,4};
        
        String axStr = Arrays.toString(axInt);
        
        System.out.println(axStr);
    }
}

运行结果:

2.sort 升序排序

import java.util.Arrays;
public class an6 {
    public static void main(String[] args) {
        int[] axInt = {1,2,3,4,8,5,7,99,88,77,55,444,3333};

        Arrays.sort(axInt);
        
        String axStr = Arrays.toString(axInt);

        System.out.println(axStr);
    }
}

运行结果:

3.Math类

java.util.math;里面有大量的静态方法,完成与数学相关的方法。

1.abs绝对值

public class an7 {
    public static void main(String[] args) {
            int axNum = -23;
            
            int axNUM = Math.abs(axNum);
            
            System.out.println(axNUM + " " + axNum);
    }
}

运行结果:

2.ceil向上取整

public class an7 {
    public static void main(String[] args) {
            double axNum = -23.2;

            double ayNum = 23.2;

            double axNUM = Math.ceil(axNum);
            double ayNUM = Math.ceil(ayNum);

            System.out.println(axNUM + " " + axNum);
            System.out.println(ayNUM + " " + ayNum);
    }
}

运行结果:

3.floor向下取整

public class an7 {
    public static void main(String[] args) {
            double axNum = -23.2;

            double ayNum = 23.2;

            double axNUM = Math.floor(axNum);
            double ayNUM = Math.floor(ayNum);

            System.out.println(axNUM + " " + axNum);
            System.out.println(ayNUM + " " + ayNum);
    }
}

运行结果:

4.round四舍五入

public class an7 {
    public static void main(String[] args) {
            double axNum = 23.5;

            double ayNum = 23.2;

            double axNUM = Math.round(axNum);
            double ayNUM = Math.round(ayNum);

            System.out.println(axNUM + " " + axNum);
            System.out.println(ayNUM + " " + ayNum);
    }
}

运行结果:

5.max/min最大最小

        int aum=10;
        int bum = 100;
        int Maxum = Math.max(aum,bum);
        int Minum = Math.min(aum,bum);
        System.out.println(Maxum+"\n"+Minum);

执行结果:

4.Scanner类

  1.导入包:

java.util包中的Scanner

import java.util.Scanner;

2.创建对象:eg:myObj

Scanner 变量名 = new Scanner(System.in);

如下:创建一个myObj 对象。

Scanner myObj = new Scanner(System.in);

3.调用对象:

基本数据类型 标识符 = 变量名.next基本数据类型();

eg1.传入应该byte类型的参数---anxian1

import java.util.Scanner;

public class an3 {
    public static void main(String[] args) {

        System.out.println("输入应该数字:");

        Scanner myObj = new Scanner(System.in);

        //传入byte数据类型类型----anXian1

        byte anXian1 = myObj.nextByte();

        System.out.println("anXian1    \n" + anXian1);

    }
}

完整代码如下:

import java.util.Scanner;

public class an3 {
    public static void main(String[] args) {

        System.out.println("输入应该数字:");

        Scanner myObj = new Scanner(System.in);


        byte anXian1 = myObj.nextByte();

        short anXian2 = myObj.nextShort();

        int anXian3 = myObj.nextInt();

        long anXian4 = myObj.nextLong();

        float anXian5 = myObj.nextFloat();

        double anXian6 = myObj.nextDouble();

        String anXian7 = myObj.next();

        System.out.println("anXian1\t" + anXian1 +"\t"+ "anXian2\t"
                + anXian2+"\t"+"anXian3\t" + anXian3+"\t"+"anXian4\t" + anXian4+"\t"+"anXian5\t"
                + anXian5+"\t"+"anXian6\t" + anXian6+"\t"+"anXian7\t" + anXian7);
    }
}

执行结果:

补充:

5.Random类

含参/不含参

import java.util.Random;

public class ax1 {
    public static void main(String[] args){
        Random rand = new Random();
        int x = rand.nextInt(100);//范围0-99
        
        int y = rand.nextInt();//范围-21亿-21左右
    }
}

猜数字游戏

import java.util.Random;
import java.util.Scanner;

public class an1{
    public static void main(String[] args){
        //经典-猜数字游戏
        System.out.println("输入一个数字");

        Random rand = new Random();

        int num =rand.nextInt(5);

        Scanner input = new Scanner(System.in);
        
        for (int i = 1; i <= 5; i++){
            int nums = input.nextInt();
            if (nums == num)
            {
                System.out.println("猜对了");
                break;
            }
            else 
                if (i!=5)
                    continue;
                else
                    System.out.println("游戏结束");
        }
    }

}

6 ArrayList类

数组的长度不可发生改变

但是ArraryList集合的长度可以发生改变

对于ArrayList 有一个<E>这个里面放的东西只能是引用数据类型(String,Integer,Short....),不能是基本数据类型(byte,short,char,int,long,float,double,boolean)

包装类与String

        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Short> list2 = new ArrayList<>();
        ArrayList<Byte> list3 = new ArrayList<>();
        ArrayList<Character> list4 = new ArrayList<>();
        ArrayList<Long> list5 = new ArrayList<>();
        ArrayList<Float> list6 = new ArrayList<>();
        ArrayList<Double> list7 = new ArrayList<>();
        ArrayList<Boolean> list8 = new ArrayList<>();
        ArrayList<String> list9 = new ArrayList<>();

1 创建对象list

ArrayList <String> list = new ArrayList<>();

2 添加add

//添加
list.add("A");
list.add("B");
list.add("C");

3 删除remove

//删除
list.remove(1);

4 输出

//输出
list.forEach(System.out::println);
System.out.println(list);

5 长度size

//长度
int len = list.size();

6 提取get

//提取
String aa = list.get(1);

7 排序sort

import java.util.ArrayList;
public class ax4 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(11);
        list.add(21);
        list.add(13);
        list.add(41);
        list.forEach(System.out::println);
        System.out.println(list);
        list.sort(Integer::compareTo);
        System.out.println(list);
    }
}

执行结果:

8 倒序输出

使用collections

Collections.sort(list, Collections.reverseOrder());

9 更改元素

第一个空是索引,第二个是要改成的元素。

list.set(2,6);

10 将一个数组变成一个ArrayList类创建的对象

11 截取部分元素

ArrayList<Integer> list2 = new ArrayList<>(list.subList(0,2));

System.out.println(list2);

7 StringBuffer类与String类结合使用

具体的方法

构造方法

  • StringBuffer():构造一个没有字符的字符串缓冲区,初始容量为16字符;
  • StringBuffer(String str):构造一个初始化为指定内容的字符串缓冲区;
  • StringBuffer(int capacity):构造一个没有字符的字符串缓冲区和指定的初始容量;

1 使用StringBuffer创建对象

StringBuffer ist = new StringBuffer();

2 append添加

ist.append(1);
ist.append(2);
ist.append(3);
ist.append(44);
ist.append(4);
ist.append(51);
ist.append(6);

3 delete删除

ist.delete(0,4);//删除索引区间左闭右开
ist.deleteCharAt(ist.length()-1);//删除某个字符_索引位置

4 reverse倒置

ist.reverse();//倒置

5 charAt提取字符

char c = ist.charAt(1);//提取字符_索引位置

6 replace区间字符替换

ist.replace(0,1,"*");//某个区间内的字符进行替换

7 substring提取字符串

String s = ist.substring(1,4);
String  s_ = ist.substring(2);//提取字符串如果单个索引是将末索引省去(2--1)

8 insert插入字符串

ist.insert(2,"an");//索引位置插入字符串

9 toString 变成字符串

String str = ist.toString();

  • 30
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值