标签:表达 scanner sort 工具 char replace bbb 基本 ***
Java常用API
基本步骤
? 1.导包
import java.util.Scanner;
? 2.创建
//System.in 代表从键盘输入
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
String str = sc.next();
new Person();
对象只使用一次时比较适合使用匿名对象
---------------------------------------------------------------------------
main方法里面
//一般使用Scanner的方式
Scanner sc = new Scanner(System.in);
int num = sc.nextInt()
//匿名对象使用的方式
int num = new Scanner(System.in).nextInt();
System.out.println("num is : " + num);
//匿名对象进行传参
methodProgram(new Scanner(System.in));
Scanner sc = methodReturn();
int x = sc.nextInt();
System.out.println(x);
---------------------------------------------------------------------------
public static void methodProgram(Scanner sc) {
int x = sc.nextInt();
System.out.println("num is :" + x);
}
public static Scanner methodReturn() {
return new Scanner(System.in);
}
Random r = new Random();
int rnum = r.nextInt();
System.out.println("随机数 rnum : " + rnum);
Random r = new Random();
for (int i = 0; i < 100; i++) {
System.out.println(r.nextInt(10));
}
? nextint(int x)随机输出一个[0~x-1)的数
public static void main(String[] args) {
Person[] array = new Person[3];
Person one = new Person("迪丽热巴",18);
Person two = new Person("古力娜扎",28);
Person three = new Person("马尔扎哈",38);
array[0] = one;
array[1] = two;
array[2] = three;
for (int i = 0; i < 3; i++) {
System.out.println(array[i]); //输出地址值
}
System.out.println(array[1].getName()); //输出古力娜扎
}
? 一旦创建程序运行期间无法改变长度
public class Demo02Array {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list);
}
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list);
list.add("亚索");
list.add("劫");
list.add("菲兹");
list.add("阿狸");
System.out.println(list);
}
获取对应下标的元素
获取ArrayList的长度
移除对应下标的元素
? 1.转换成char[]数组
String str = "abcde";
char[] a = str.toCharArray();
? 2.转换成byte[]数组
byte[] bytes = str.getBytes();
? 3.转换字符串中对应的字符串
String str1 = "会不会玩呀!你大爷的!你大爷的!你大爷的!你大爷的!";
System.out.println(str1);
String str2 = str1.replace("你大爷的!", "****");
System.out.println(str2);
? 结果截图:
?
? public String[] split(String regex)
? 参数regex是一个正则表达式
? 示例:
String str = "aaa,bbb,ccc,dd,ee,f";
String[] array1 = str.split(",");
for (int i = 0; i < array1.length; i++) {
System.out.println(array1[i]);
}
? 运行结果:
?
? 如果以“.”作为分离需要特殊处理
String str = "aaa.bbb.ccc";
String[] array2 = str.split("\\.");
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i]);
}
? 运行结果:
?
1.toString()
? 将数组转换成字符串
2.sort()
? 数:升序
? 字符串:按字母升序
1.向上取整 ceil()
public static double ceil(double num)
2.向下取整 floor()
public static double floor(double num)
3.取绝对值 abs()
public static double abs(double num)
4.四舍五入 round()
public static long round(double num)
标签:表达 scanner sort 工具 char replace bbb 基本 ***
原文地址:https://www.cnblogs.com/Waker-WH/p/11230382.html