第三章:常用api

image.png

一、api的使用

image.png

快捷键的话必须先完整输入方法 例如:sc.nextLine()

二、String构造方法

image.png

  1. public class Demo {
  2. public static void main(String[] args) {
  3. String sr = new String();
  4. System.out.println("String是:"+sr);
  5. char[] ch = {'a','b','c'};
  6. String s2 = new String(ch);
  7. System.out.println("char是:"+s2);
  8. byte[] by = {60, 70, 80, 90};
  9. String s3 = new String(by);
  10. System.out.println("byte是:"+s3);
  11. String s4 = "abcd";
  12. System.out.println("String2是:"+s4);
  13. }
  14. }

三、String字符串的比较
image.png

  1. public class Demo {
  2. public static void main(String[] args) {
  3. char[] ch = {'a','b','c'};
  4. String s1 = new String(ch);
  5. System.out.println("s1是:"+s1);
  6. String s2 = new String(ch);
  7. System.out.println("s2是:"+s2);
  8. String s3 = "abc";
  9. System.out.println("String3是:"+s3);
  10. String s4 = "abc";
  11. System.out.println("String4是:"+s4);
  12. String print = "";
  13. for (int a=0;a<20;a++){
  14. print += "-";
  15. }
  16. System.out.println(print);
  17. System.out.println(s1 == s2);
  18. System.out.println(s3 == s4);
  19. System.out.println(s1 == s3);
  20. System.out.println(s1.equals(s2));
  21. System.out.println(s3.equals(s4));
  22. System.out.println(s1.equals(s3));
  23. }
  24. }

1. 用户登录判断案例

  1. import java.util.Scanner;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. String name = "cao";
  5. String pwd = "123";
  6. Scanner sc = new Scanner(System.in);
  7. for (int i = 0;i < 3;i++){
  8. System.out.println("请输入用户名");
  9. String input_name = sc.nextLine();
  10. System.out.println("请输入密码");
  11. String input_pwd = sc.nextLine();
  12. if (name.equals(input_name) && pwd.equals(input_pwd)){
  13. System.out.println("登录成功");
  14. break;
  15. }else if(2-i == 0){
  16. System.out.println("登录失败,账户被锁定");
  17. break;
  18. }else{
  19. System.out.print("登陆失败,你还有" + (2 - i) + "次机会");
  20. }
  21. }
  22. }
  23. }

2. 遍历字符串

image.png

  1. import java.util.Scanner;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. System.out.println("请输入一个字符串:");
  6. String text = sc.nextLine();
  7. for (int i=0;i<text.length();i++){
  8. System.out.println(text.charAt(i));
  9. }
  10. }
  11. }

3. 统计字符次数

image.png

  1. import java.util.Scanner;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. System.out.println("请输入一个字符串:");
  6. String text = sc.nextLine();
  7. int big=0;
  8. int small=0;
  9. int number=0;
  10. for (int i = 0;i<text.length();i++){
  11. if (text.charAt(i)>='a' && text.charAt(i)<='z'){
  12. small++;
  13. }else if(text.charAt(i)>='A' && text.charAt(i)<='Z'){
  14. big++;
  15. }else if (text.charAt(i)>='0' && text.charAt(i)<='9'){
  16. number++;
  17. }
  18. }
  19. System.out.println("big数量:"+big);
  20. System.out.println("small数量:"+small);
  21. System.out.println("number数量:"+number);
  22. }
  23. }

4. 字符串拼接

image.png

  1. public class Demo {
  2. public static void main(String[] args) {
  3. int[] arr = {1, 2, 3};
  4. String a = arr_join(arr);
  5. System.out.println(a);
  6. }
  7. public static String arr_join(int[] arr){
  8. String s = "";
  9. s += "[";
  10. for (int a=0;a<arr.length;a++){
  11. if (a == arr.length-1){
  12. s += arr[a];
  13. s += ']';
  14. }else{
  15. s += arr[a];
  16. s += ", ";
  17. }
  18. }
  19. return s;
  20. }
  21. }

三、StringBuilder和String

1. 相互转换

image.png

  1. import java.util.Scanner;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. String s = "hello";
  5. StringBuilder st = new StringBuilder(s);
  6. System.out.println(st);
  7. StringBuilder st2 = new StringBuilder();
  8. st2.append("world");
  9. String s2 = st2.toString();
  10. System.out.println(s2);
  11. }
  12. }

2. 字符串的拼接

image.png

  1. import java.util.Scanner;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. int[] arr = {1,2,3,4};
  5. String s = arrayToString(arr);
  6. System.out.println(s);
  7. }
  8. public static String arrayToString(int[] arr){
  9. StringBuilder st = new StringBuilder();
  10. st.append("[");
  11. for (int i=0;i<arr.length;i++){
  12. if (i == arr.length-1){
  13. st.append(arr[i]);
  14. }else {
  15. st.append(arr[i]).append(", ");
  16. }
  17. }
  18. st.append("]");
  19. String s = st.toString();
  20. return s;
  21. }
  22. }

3. 字符串反转

image-20210523094842880.png

  1. import java.util.Scanner;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. System.out.println("请输入一个字符:");
  6. String line = sc.nextLine();
  7. String result = myReversal(line);
  8. System.out.println(result);
  9. }
  10. public static String myReversal(String s){
  11. StringBuilder st = new StringBuilder(s);
  12. st.reverse();
  13. String s2 = st.toString();
  14. return s2;
  15. }
  16. }

四、ArrayList集合

1. 集合概述

image.png

2. ArrayList方法添加和使用

image.png

  1. import java.util.ArrayList;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. ArrayList<String> ar = new ArrayList<>();
  5. ar.add("hello");
  6. ar.add("world");
  7. ar.add("java");
  8. ar.add(1,"javaee");
  9. System.out.println(ar);
  10. }
  11. }

3. ArrayList常用方法

image.png

4. ArrayList存储和遍历

image.png

  1. import java.util.ArrayList;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. ArrayList<String> arr = new ArrayList<>();
  5. for (int i=0;i<5;i++){
  6. arr.add("这是集合"+i);
  7. }
  8. System.out.println("该数组集合长度是:"+arr.size());
  9. for(int i = 0;i<arr.size();i++){
  10. String s = arr.get(i);
  11. System.out.println("数组"+i+"是:"+s);
  12. }
  13. }
  14. }

5. 案例学生存储遍历

image.png

  1. --------------------------------------------------------Student
  2. package com;
  3. public class Student {
  4. private String gender;
  5. private String age;
  6. public Student(){}
  7. public Student(String gender, String age){
  8. this.gender = gender;
  9. this.age = age;
  10. }
  11. public void setgender(String gender) {
  12. this.gender = gender;
  13. }
  14. public void setAge(String age) {
  15. this.age = age;
  16. }
  17. public String getAge() {
  18. return age;
  19. }
  20. public String getgender() {
  21. return gender;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Student{" +
  26. "gender='" + gender + '\'' +
  27. ", age=" + age +
  28. '}';
  29. }
  30. }
  31. --------------------------------------------------------Demo
  32. package com;
  33. import java.util.ArrayList;
  34. import java.util.Scanner;
  35. public class Demo {
  36. public static void main(String[] args) {
  37. ArrayList<Student> arr = new ArrayList<>();
  38. for (int i = 0; i < 3; i++) {
  39. Student s1 = new Student();
  40. Scanner sc = new Scanner(System.in);
  41. System.out.println("请输入集合" + i + "的性别:");
  42. s1.setgender(sc.nextLine());
  43. System.out.println("请输入集合" + i + "的年龄:");
  44. s1.setAge(sc.nextLine());
  45. arr.add(s1);
  46. }
  47. for (int i = 0; i < arr.size(); i++) {
  48. String s = arr.get(i).toString();
  49. System.out.println(s);
  50. }
  51. }
  52. }

五、Math

1. Math的常用方法

image.png

六、System

1. System的常用方法

image.png
image.png

七、Object类

1. toString

image.png

2. equals

3. 冒泡排序

  1. import java.util.Arrays;
  2. public class test {
  3. public static void main(String[] args) {
  4. int[] arr = {24,60,80,30,100,10};
  5. System.out.println("排序前:" + Arrays.toString(arr));
  6. for(int a=1; a<arr.length; a++){
  7. for(int i=0;i<arr.length-a;i++){
  8. if(arr[i]>arr[i+1]){
  9. int temp = arr[i];
  10. arr[i] = arr[i+1];
  11. arr[i+1] = temp;
  12. }
  13. }
  14. System.out.println("第" + a + "次排序:" + Arrays.toString(arr));
  15. }
  16. }
  17. }

4. Arrays类

image.png

八、基本类型包装类

1. 描述

image.png

项目

1. 案例学生管理系统

image.png

  1. -----------------------------------------------------------------------Student
  2. package com;
  3. import java.util.Scanner;
  4. public class Student {
  5. private String name;
  6. private String address;
  7. private int age;
  8. private String sid;
  9. public Student(){}
  10. public Student(String name, String address, String sid, int age){
  11. this.name = name;
  12. this.address = address;
  13. this.sid = sid;
  14. this.age = age;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public int getAge() {
  20. return age;
  21. }
  22. public String getAddress() {
  23. return address;
  24. }
  25. public String getSid() {
  26. return sid;
  27. }
  28. public void setAge(int age) {
  29. this.age = age;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public void setAddress(String address) {
  35. this.address = address;
  36. }
  37. public void setSid(String sid) {
  38. this.sid = sid;
  39. }
  40. }
  41. -----------------------------------------------------------------------Demo
  42. package com;
  43. import java.util.ArrayList;
  44. import java.util.Scanner;
  45. public class Demo {
  46. public static void main(String[] args) {
  47. ArrayList<Student> arr = new ArrayList<>();
  48. while (true) {
  49. System.out.println("--------欢迎来到学生管理系统--------");
  50. System.out.println("1.添加学生");
  51. System.out.println("2.删除学生");
  52. System.out.println("3.修改学生");
  53. System.out.println("4.查看所有学生");
  54. System.out.println("5.退出");
  55. System.out.println("请输入你的选择:");
  56. Scanner sc = new Scanner(System.in);
  57. String line = sc.nextLine();
  58. switch (line) {
  59. case "1":
  60. addStudent(arr);
  61. break;
  62. case "2":
  63. deleteStudent(arr);
  64. break;
  65. case "3":
  66. amendStudent(arr);
  67. break;
  68. case "4":
  69. checkStudent(arr);
  70. break;
  71. case "5":
  72. System.out.println("谢谢使用");
  73. System.exit(0);
  74. }
  75. }
  76. }
  77. public static void addStudent(ArrayList<Student> array) {
  78. int index = -1;
  79. Scanner sc = new Scanner(System.in);
  80. System.out.println("请输入学生学号:");
  81. String sid = sc.nextLine();
  82. for (int i=0;i<array.size();i++){
  83. if (array.get(i).getSid().equals(sid)){
  84. index = i;
  85. break;
  86. }
  87. }
  88. if (index != -1){
  89. System.out.println("你输入的学号存在,请重新输入");
  90. addStudent(array);
  91. }
  92. System.out.println("请输入学生姓名:");
  93. String name = sc.nextLine();
  94. System.out.println("请输入学生年龄:");
  95. int age = Integer.parseInt(sc.nextLine());
  96. System.out.println("请输入学生居住地:");
  97. String address = sc.nextLine();
  98. Student s = new Student();
  99. s.setSid(sid);
  100. s.setName(name);
  101. s.setAge(age);
  102. s.setAddress(address);
  103. array.add(s);
  104. System.out.println("添加学生成功");
  105. }
  106. public static void checkStudent(ArrayList<Student> array) {
  107. if (array.size() == 0){
  108. System.out.println("暂无成员,请先添加数据!");
  109. return;
  110. }
  111. System.out.println("学号\t\t\t姓名\t\t年龄\t\t住址");
  112. for (int i = 0; i < array.size(); i++) {
  113. String sid = array.get(i).getSid();
  114. String name = array.get(i).getName();
  115. int age = array.get(i).getAge();
  116. String address = array.get(i).getAddress();
  117. System.out.println(sid + "\t" + name + "\t" + age + "岁\t" + address);
  118. }
  119. }
  120. public static void deleteStudent(ArrayList<Student> array) {
  121. Scanner sc = new Scanner(System.in);
  122. System.out.println("请输入要删除的学号:");
  123. String sid = sc.nextLine();
  124. int index = -1;
  125. for (int i=0;i< array.size();i++){
  126. if (array.get(i).getSid().equals(sid)){
  127. index = i;
  128. break;
  129. }
  130. }
  131. if (index == -1){
  132. System.out.println("学号不存在,请重新输入");
  133. deleteStudent(array);
  134. }else{
  135. array.remove(index);
  136. System.out.println("成功删除一条信息!");
  137. checkStudent(array);
  138. }
  139. }
  140. public static void amendStudent(ArrayList<Student> array) {
  141. System.out.println("请输入要修改的学生的学号:");
  142. Scanner sc = new Scanner(System.in);
  143. String sid = sc.nextLine();
  144. int index = -1;
  145. for (int i=0;i< array.size();i++){
  146. if (array.get(i).getSid().equals(sid)){
  147. index = i;
  148. break;
  149. }
  150. }
  151. if (index == -1){
  152. System.out.println("学号不存在,请重新输入");
  153. amendStudent(array);
  154. }else{
  155. System.out.println("请输入学生的新学号");
  156. sid = sc.nextLine();
  157. array.get(index).setSid(sid);
  158. System.out.println("请输入学生的新姓名");
  159. String name = sc.nextLine();
  160. array.get(index).setName(name);
  161. System.out.println("请输入学生的新年龄");
  162. int age = Integer.parseInt(sc.nextLine());
  163. array.get(index).setAge(age);
  164. System.out.println("请输入学生的新地址");
  165. String address = sc.nextLine();
  166. array.get(index).setAddress(address);
  167. System.out.println("修改完毕");
  168. checkStudent(array);
  169. }
  170. }
  171. }