当前位置: 首页 > news >正文

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();}
}
/*
张三 李四 赵宁 王五 
赵宁 王五 
这个值被修改了 王五 */

http://www.dtcms.com/a/548541.html

相关文章:

  • 【STM32项目开源】STM32单片机医疗点滴控制系统
  • 游戏类网站备案需要前置审批吗怎么制作图片表格
  • AWS EC2 服务器弹性伸缩:基于 CPU 使用率创建伸缩组,实现资源动态调整
  • srt服务器,推拉流
  • Rust API 设计中的零成本抽象原则:从原理到实践的平衡艺术
  • Work-Stealing 调度算法:Rust 异步运行时的核心引擎
  • 服务器恶意进程排查:从 top 命令定位到病毒文件删除的实战步骤
  • 【案例实战】初探鸿蒙开放能力:从好奇到实战的技术发现之旅
  • 服务器启动的时候就一个对外的端口,如何同时连接多个客户端?
  • LVS负载均衡集群理论详解
  • 三维重建【0-E】3D Gaussian Splatting:相机标定原理与步骤
  • Flutter---ListTile列表项组件
  • Spring Boot入门篇:快速搭建你的第一个Spring Boot应用
  • 《算法通关指南数据结构和算法篇(1)--- 顺序表相关算法题》
  • ReentrantLock 加锁与解锁流程详解(源码分析,小白易懂)
  • 鸿蒙Flutter三方库适配指南:06.插件适配原理
  • Linux 防火墙实战:用 firewalld 配置 External/Internal 区域,实现 NAT 内网共享上网
  • Java 学习29:方法
  • Kafka 全方位详细介绍:从架构原理到实践优化
  • Obsidian 入门教程(二)
  • [测试工具] 如何把离线的项目加入成为git项目的新分支
  • 让数据导入导出更智能:通用框架+验证+翻译的一站式解决方案
  • 今天我们学习Linux架构keepalived实现LVS代理双击热备
  • [Linux]内核队列实现详解
  • 【Spring Cloud】Spring Cloud Config
  • MySQL | 数据查询DQL语言:分组统计
  • 阿里云灵码IDE技术测评:从v0.1.0到v0.1.5的进化之路
  • 江门网站推广技巧asp网站服务建设
  • C++: inline 与 ODR,冲突的诞生
  • 营销型 展示类网站企业网站建设空间