Java—常见API(String、ArrayList)
目录
一、String
1、认识String
2、String创建字符串对象的方式
2.1、String不同方式创建对象的区别
3、String提供的常用的方法
4、案例开发验证码
二、ArrayList
1、认识集合
2、集合的种类
3、ArrayLiat集合学什么、常用的方法
一、String
1、认识String
String是什么,有什么用?
String代表字符串,它的对象可以封装字符串数据,并提供了很多方法完成对字符串的处理。

2、String创建字符串对象的方式

代码1:来创建字符串对象
Test类
public class StringTest {public static void main(String[] args) {// 创建字符串对象String str = "hello world";System.out.println(str);System.out.println(str.length());String str1 = new String("hello world");System.out.println(str1);String str2 = new String(); // 创建空字符串System.out.println(str2);System.out.println(str2.length());char[] ch = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};// 创建字符串对象String str3 = new String(ch);System.out.println(str3);byte[] b = {104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100};// 创建字符串对象String str4 = new String(b);System.out.println(str4);}
}
/*
hello world
11
hello world0
hello world
hello world*/
2.1、String不同方式创建对象的区别
- 只要是以“...”方式写出的字符串对象,会存储到字符串常量池,且相同内容的字符串只存储一份;
- 通过new方式创建字符串对象,每new一次都会产生一个新的对象放在堆内存中。
代码2:比较不同创建字符串的区别
public class Test {public static void main(String[] args) {String str = "hello world";String str1 = "hello world";System.out.println(str == str1);  //tureString str2 = new String("hello world");String str3 = new String("hello world");System.out.println(str2 == str3);  //falseSystem.out.println(str == str2);}
}
/*
输出结果为:
true
false
false*/通过“ ”创建的字符串对象

其他方式创建的字符串对象

3、String提供的常用的方法

4、案例开发验证码

import java.util.Scanner;public class StringTest1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入验证码长度:");int n = sc.nextInt();System.out.println(getCode(n));
//        double a = Math.random();[0,1)的随机数
//        System.out.println(a);}//生成一个验证码包含大小写字母和数字//使用Stringpublic static String getCode(int n) {String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";String code = "";for (int i = 0; i < n; i++) {int number = (int) (Math.random() * str.length()); //[0,1)->[0,str.length())code += str.charAt(number);}return code;}
}
二、ArrayList
1、认识集合
什么是集合?
- 集合是一种容器,用来装数据的,类似于数组。
有数组,为啥还学习集合?
- 数据定义完成并启动后长度就固定了。
- 集合大小可变,动能丰富,开发中用的更多。
2、集合的种类

3、ArrayLiat集合学什么、常用的方法
- 创建ArrayLiat对象,代表一个集合容器。
- 调用ArrayLiat提供的方法,对容器中的数据进行增删改查操作。
 
ArrayLiat集合的创建格式
  ArrayList<ElementType> list = new ArrayList<>();其中 ElementType 是你希望存储在列表中的元素类型。例如,如果要存储字符串,则可以这样创建:
  ArrayList<String> stringList = new ArrayList<>();注意:如果把<ElementType>省略不写,这样定义出来的集合可以放任何类型的元素。
ArrayListTest类
import java.util.*;
public class ArrayListTest {public static void main(String[] args) {ArrayList<String> al = new ArrayList<>();//添加元素al.add("张三");al.add("李四");al.add("赵宁");al.add("王五");//遍历for (int i = 0; i < al.size(); i++) {System.out.printf("%s ", al.get(i));}System.out.println();//删除al.remove(1);//根据值删除al.remove("张三");for (int i = 0; i < al.size(); i++) {System.out.printf("%s ", al.get(i));}System.out.println();//修改al.set(0, "这个值被修改了");for (int i = 0; i < al.size(); i++) {System.out.printf("%s ", al.get(i));}System.out.println();}
}
/*
张三 李四 赵宁 王五 
赵宁 王五 
这个值被修改了 王五 */
