常用API(String,ArryList)
String
1.String
String对象不可变的原因:
1.String变量每次的修改其实都是产生并指向了新的字符对象
2.原来的字符对象都是没有改变的,所以称为不可变字符串
2.创建String对象
//1." "给出对象
String s3="你好hello";
//2.new 构造器给出对象
//字符拼接成串
char[] chars={'a','b','你','好'};
String s1=new String(chars);
System.out.println(s1); // 输出的结果:ab你好
//byte转为abc
byte[] bytes={97,99,98}; //byte:-128~127
String s2=new String(bytes);
System.out.println(s2); //输出结果:acb
常用的构造器
区别:
1.“ ”给出对象:字符串在 字符串常量池中 存储,相同内容只存一份。
2.构造器new给出对象:每new一次都会产生一个新对象,放在 堆 中。
String s1="你好hello";
String s2="你好hello";
System.out.println(s1==s2); //true,s1,s2地址相同
char[] chars={'a','b','你','好'};
String s3=new String(chars);
String s4=new String(chars);
System.out.println(s3==s4); //false,s3,s4地址不同
面试题目
String s1=new String("abc");//创建了2个对象,常量池里+堆里
String s2="abc";//创建0个对象,常量池里已经有了
System.out.println(s1==s2);//false,s1指向堆里的,s2指向常量池里
String s3="abc";
String s4="a"+"b"+"c";
System.out.println(s3==s4);//true,java存在编译优化机制,程序编译时"a"+"b"+"c"直接转成"abc"
3.String常用功能
1字符串内容比较
//1.字符串是否相等:password.equals(okpassword)
String okpassword="123456";
System.out.println("请输入密码:");
Scanner sc=new Scanner(System.in);
String password=sc.next();
if(password.equals(okpassword)){
System.out.println("密码正确");
}else{
System.out.println("密码错误");
}
//2.忽略大小写进行比较
String okcode="16oFAh";
String code="16ofah";
System.out.println(okcode.equals(code));//false
System.out.println(okcode.equalsIgnoreCase(code));//true
//遍历字符串
String s1="我爱你中国";
for (int i = 0; i < s1.length(); i++) {
char c=s1.charAt(i);
System.out.print(c+"\t");
}
System.out.println();
//转化为字符数组
char[] ch1=s1.toCharArray();
for (int i = 0; i < ch1.length; i++) {
System.out.print(ch1[i]+"\t");
}
System.out.println();
//截取片段
String s2="床前明月光";
String ss2=s2.substring(0,3);//范围包前不包后
String sss2=s2.substring(1);//从1开始,截到尾
System.out.println(ss2);
System.out.println(sss2);
//替换片段
String s3="张三是学生,张三在学习Java";
String ss3=s3.replace("张三","***");
System.out.println(ss3);//***是学生,***在学习Java
//是否包含片段
System.out.println(s3.contains("张三"));//true
System.out.println(s3.contains("张四"));//false
//是否以片段开始
System.out.println(s3.startsWith("张三"));//true
//按某个字符分割
String s4="张三,李四,王五";
String[] names=s4.split(",");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
3.练习:
//随机生成验证码5位
String chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String code="";
Random r=new Random();
for (int i = 0; i < 5; i++) {
int index=r.nextInt(chars.length());
code+=chars.charAt(index);
}
System.out.println("验证码为:"+code);
//用户名、密码登录,3次机会
String username="John";
String okpassword="123456";
Scanner sc=new Scanner(System.in);
for(int i=1;i<=3;i++){
System.out.println("请输入用户名:");
String name=sc.next();
System.out.println("请输入密码:");
String code=sc.next();
if(name.equals(username)){
if(code.equals(okpassword)){
System.out.println("登陆成功");
break;
}else {
System.out.println("密码错误,请从新输入(还有"+(3-i)+"次机会");
}
}else{
System.out.println("用户名不正确,请重新输入(还有"+(3-i)+"次机会");
}
}
//手机号遮蔽:保留前3位,屏蔽中间4位,保留后3位
System.out.println("请输入一个手机号:");
Scanner sc=new Scanner(System.in);
String phoneNumber=sc.next();
//138****7341
String s1=phoneNumber.substring(0,3);
String s2=phoneNumber.substring(7);
System.out.println("屏蔽后:"+s1+"****"+s2);
}
ArryList
1.ArryList(集合)特点
集合适合元素个数不确定,且要进行增删操作的业务场景
2.使用
public static void main(String[] args) {
//1.创建集合对象
ArrayList list=new ArrayList();
//2.添加数据
list.add("Java");
list.add("MySQL");
list.add("汽车");
list.add(10.2);
list.add(false);
list.add('a');
System.out.println(list); //结果:[Java, MySQL, 汽车, 10.2, false, a]
//3.给指定索引位置插入元素
list.add(1,"【插入元素】");
System.out.println(list); //结果:[Java, 【插入元素】, MySQL, 汽车, 10.2, false, a]
}
泛型概述
ArrayList
public class test2 {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
list.add("Java");
list.add("MySQL");
ArrayList<Integer> list2=new ArrayList<Integer>();
list2.add(10);
list2.add(200);
}
}
3.常用API
ArrayList<String> list=new ArrayList<>();
list.add("Java");
list.add("MySQL");
list.add("MyBatis");
list.add("HTML");
list.add("Java");
System.out.println(list);//[Java, MySQL, MyBatis, HTML, Java]
//1.获取某索引位置的元素
String s1=list.get(2);
System.out.println(s1);
//2.获取集合大小
System.out.println(list.size());
//3.集合遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//4.删除某索引位置的元素
System.out.println(list);
String s=list.remove(2);//返回String
System.out.println(list);
System.out.println(s);
//5.删除某个元素,返回true,false
System.out.println(list.remove("HTML"));//true
System.out.println(list);
System.out.println(list.remove("Java"));//若有多个重复元素,只删除最前面的一个
System.out.println(list);
//6.修改某个索引位置的元素值,返回被修改的的元素
String e=list.set(0,"[修改]");
System.out.println(e);//MySQL
System.out.println(list);
4.案例练习
1.遍历成绩,删除80分以下的成绩
public static void main(String[] args) {
//存一个班级的成绩
ArrayList<Integer> scores=new ArrayList<>();
scores.add(60);
scores.add(70);
scores.add(85);
scores.add(55);
scores.add(98);
scores.add(87);
scores.add(76);
System.out.println(scores); //[60, 70, 85, 55, 98, 87, 76]
//去掉80分以下的
//错误案例:
// for (int i = 0; i < scores.size(); i++) {
// if(scores.get(i)<80){
// scores.remove(i);
// }
// }
// System.out.println(scores); //[70, 85, 98, 87],并没有删干净
//【注意】:将60删除后,70自动向前移动,但循环中i继续加,70被跳过
//正确案例:
for (int i = 0; i < scores.size(); i++) {
if(scores.get(i)<80){
scores.remove(i);
i--; //每删除成功一个,i要向前退一位,保证下次回到这个位置,不会跳过元素
}
}
System.out.println(scores);//[85, 98, 87]
}
//法二:倒着遍历集合
for (int i =scores.size()-1; i < =0; i--) {
if(scores.get(i)<80){
scores.remove(i);
}
}
2.集合装3个电影
public class test4 {
public static void main(String[] args) {
Movie m1=new Movie("《肖申克的救赎》",9.7,"罗宾斯");
Movie m2=new Movie("《霸王别姬》",9.6,"张国荣、张丰毅");
Movie m3=new Movie("《阿甘正传》",9.5,"汤姆·汉克斯");
ArrayList<Movie> movies=new ArrayList<>();
movies.add(m1); //集合中存的是对象的地址
movies.add(m2);
movies.add(m3);
for (int i = 0; i < movies.size(); i++) {
Movie m=movies.get(i);
System.out.println(m.getName()+", "+m.getScore()+", "+m.getActor());
}
}
}
public class Movie {
private String name;
private double score;
private String actor;
public Movie() {
}
public Movie(String name, double score, String actor) {
this.name = name;
this.score = score;
this.actor = actor;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
public String getActor() {
return actor;
}
public void setName(String name) {
this.name = name;
}
public void setScore(double score) {
this.score = score;
}
public void setActor(String actor) {
this.actor = actor;
}
}
4.学生信息搜索
public class test6 {
public static void main(String[] args) {
ArrayList<Student> students=new ArrayList<>();
students.add(new Student("1010","张三",21,"一班"));
students.add(new Student("1018","李四",24,"二班"));
students.add(new Student("1024","王五",20,"一班"));
students.add(new Student("1011","李华",22,"六班"));
//遍历展示
System.out.println("学号\t\t姓名\t\t年龄\t\t班级");
for (int i = 0; i < students.size(); i++) {
Student s=students.get(i);
System.out.println(s.getId()+"\t"+s.getName()+
"\t\t"+s.getAge()+"\t\t"+s.getClassName());
}
//按学号搜索,定义一个方法
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("请输入要查找的学生的id:");
String id=sc.next();
Student s1=getStudentByID(students,id);
if(s1!=null){
System.out.println("找到了");
System.out.println(s1.getId()+"\t"+s1.getName()+
"\t\t"+s1.getAge()+"\t\t"+s1.getClassName());
}else{
System.out.println("查无此人,请重新输入id");
}
}
}
public static Student getStudentByID(ArrayList<Student> s,String id){
//遍历查找
for (int i = 0; i < s.size(); i++) {
Student s1=s.get(i);
if(s1.getId().equals(id)){
return s1;
}
}
//查无此人
return null;
}
}
public class Student {
private String id;
private String name;
private int age;
private String className;
public Student() {
}
public Student(String id, String name, int age, String className) {
this.id = id;
this.name = name;
this.age = age;
this.className = className;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getClassName() {
return className;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setClassName(String className) {
this.className = className;
}
}
标签:String,ArryList,Day4,list,System,add,println,out
From: https://www.cnblogs.com/mt-nika/p/17045168.html