JAVA学习笔记03——API

API

1.Scanner
实现键盘输入
语法:
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();

使用步骤:
1.导包
import 包路径.类名称;
如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写。
只有java.lang包下的内容不需要导包,其他都需要导包import。
2.创建
类名称 对象名=new 类名称();
3.使用
对象名.成员方法名;

import java.util.Scanner;
public class API {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt();       //接收整型
        System.out.println(num);
        String str=sc.next();       //接收字符串
        System.out.println(str);
    }
}

2.Random

import java.util.Random;

public class demo01 {
    
    
    public static void main(String[] args) {
    
    
        Random r=new Random();
        int num=r.nextInt(100); //产生[0,100)的随机数
        System.out.println(num);
    }
}

3.ArrayList
java.util.ArrayList是大小可变的数组的实现,存储在内的数据称为元素。此类提供一些方法来操作内部存储的元素。ArrayList中可不断添加元素,其大小也自动增长。

基本语法:
ArrayList<元素类型> list=new ArrayList()

常用方法:
add(E e) 添加元素
get(int index) 获取元素,参数是索引编号,返回值是对应位置的元素
remove(int index) 删除元素,参数是索引编号,返回值是被删除的元素
size() 获取集合的长度

import java.util.ArrayList;

public class demo01 {
    
    
    public static void main(String[] args) {
    
    
        Person p1=new Person("Ray",18);
        Person p2=new Person("Kobe",24);
        Person p3=new Person("James",23);
        ArrayList<Person> list=new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        for(int i=0;i<list.size();i++){
    
    
            System.out.println(list.get(i).getName()+" "+list.get(i).getAge());
        };
    }
}

ArrayList当中存储泛型只能是引用类型,不能是基本类型
如果向ArrayList中存储基本数据类型,必须使用基本类型的包装类
基本类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

import java.util.ArrayList;

public class demo02 {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<Integer> list=new ArrayList<>();
        list.add(23);
        list.add(24);
        list.add(100);
        for(int i=0;i<list.size();i++){
    
    
            System.out.println(list.get(i)+" ");
        }
    }
}

4.string
字符串特点:
1.字符串的内容不可以改变
2.字符串可以共享使用
3.字符串相当于char字符数组

字符串的创建:

        String s1=new String();     //空字符串
        char[] charArray={
    
    'A','B','C'};
        String s2=new String(charArray); 
        String s3="hello";

常用方法:
equals() 比较
equalsIgnoreCase() 忽略大小写
length() 字符串长度
concat(String s) 拼接字符串
charAt(int index) 获取指定索引位置处的字符
indexOf(String s) 查找参数字符串在字符串中首次出现的位置,如果没有,返回-1

public class demo05 {
    
    
    public static void main(String[] args) {
    
    
        String s="hello";
        System.out.println(s.length()); //5
        String str=s.concat(" world");  
        System.out.println(s+" "+str);  //hello hello world,字符串s不变
        System.out.println(str.charAt(3));  //l
        System.out.println(str.indexOf(s)); //0
    }
}

字符串的截取:
substring(int index) 截取从参数位置一直到字符串末尾返回新字符串
substring(int begin,int end) 截取从begin开始到end结束的中间字符串,左闭右开

public class demo06 {
    
    
    public static void main(String[] args) {
    
    
        String s1="hello world";
        String s2=s1.substring(s1.indexOf("w"));
        System.out.println(s2);     //world
        String s3=s1.substring(s1.indexOf("w"),s1.indexOf("d")+1);
        System.out.println(s3);     //world
    }
}

字符串的转换:
replace(目标字符串,替换字符串)

字符串的分割:
split(String s) 按照参数,将字符串切分为若干部分

        String str="hello,world";
        String[] s=str.split(",");
        for(int i=0;i<s.length;i++){
    
    
            System.out.print(s[i]+" "); //hello world 
        }

split方法的参数其实是一个正则表达式,如果按照英文".“进行切分,必须写”\."

    public static void main(String[] args) {
    
    
        String str="hello.world";
        String[] s=str.split("\\.");
        for(int i=0;i<s.length;i++){
    
    
            System.out.print(s[i]+" "); //hello world
        }
    }

6.Arrays
toString(数组) 将参数数组转换成字符串
sort(数组) 按照默认升序对数组进行排序

    public static void main(String[] args) {
    
    
        int[] array1={
    
    10,20,30,24,23};
        String str= Arrays.toString(array1);
        System.out.println(str);    //[10, 20, 30, 24, 23]
        Arrays.sort(array1);
        System.out.println(Arrays.toString(array1));    //[10, 20, 23, 24, 30]
    }

7.Math
abs(double num) 获取绝对值
ceil(double num) 向上取整
floor(double num) 向下取整
round(double num) 四舍五入

猜你喜欢

转载自blog.csdn.net/qq_44708714/article/details/106769685
今日推荐