第三章:常用api
一、api的使用
快捷键的话必须先完整输入方法 例如:sc.nextLine()
二、String构造方法
public class Demo {
public static void main(String[] args) {
String sr = new String();
System.out.println("String是:"+sr);
char[] ch = {'a','b','c'};
String s2 = new String(ch);
System.out.println("char是:"+s2);
byte[] by = {60, 70, 80, 90};
String s3 = new String(by);
System.out.println("byte是:"+s3);
String s4 = "abcd";
System.out.println("String2是:"+s4);
}
}
三、String字符串的比较
public class Demo {
public static void main(String[] args) {
char[] ch = {'a','b','c'};
String s1 = new String(ch);
System.out.println("s1是:"+s1);
String s2 = new String(ch);
System.out.println("s2是:"+s2);
String s3 = "abc";
System.out.println("String3是:"+s3);
String s4 = "abc";
System.out.println("String4是:"+s4);
String print = "";
for (int a=0;a<20;a++){
print += "-";
}
System.out.println(print);
System.out.println(s1 == s2);
System.out.println(s3 == s4);
System.out.println(s1 == s3);
System.out.println(s1.equals(s2));
System.out.println(s3.equals(s4));
System.out.println(s1.equals(s3));
}
}
1. 用户登录判断案例
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
String name = "cao";
String pwd = "123";
Scanner sc = new Scanner(System.in);
for (int i = 0;i < 3;i++){
System.out.println("请输入用户名");
String input_name = sc.nextLine();
System.out.println("请输入密码");
String input_pwd = sc.nextLine();
if (name.equals(input_name) && pwd.equals(input_pwd)){
System.out.println("登录成功");
break;
}else if(2-i == 0){
System.out.println("登录失败,账户被锁定");
break;
}else{
System.out.print("登陆失败,你还有" + (2 - i) + "次机会");
}
}
}
}
2. 遍历字符串
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String text = sc.nextLine();
for (int i=0;i<text.length();i++){
System.out.println(text.charAt(i));
}
}
}
3. 统计字符次数
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String text = sc.nextLine();
int big=0;
int small=0;
int number=0;
for (int i = 0;i<text.length();i++){
if (text.charAt(i)>='a' && text.charAt(i)<='z'){
small++;
}else if(text.charAt(i)>='A' && text.charAt(i)<='Z'){
big++;
}else if (text.charAt(i)>='0' && text.charAt(i)<='9'){
number++;
}
}
System.out.println("big数量:"+big);
System.out.println("small数量:"+small);
System.out.println("number数量:"+number);
}
}
4. 字符串拼接
public class Demo {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String a = arr_join(arr);
System.out.println(a);
}
public static String arr_join(int[] arr){
String s = "";
s += "[";
for (int a=0;a<arr.length;a++){
if (a == arr.length-1){
s += arr[a];
s += ']';
}else{
s += arr[a];
s += ", ";
}
}
return s;
}
}
三、StringBuilder和String
1. 相互转换
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
String s = "hello";
StringBuilder st = new StringBuilder(s);
System.out.println(st);
StringBuilder st2 = new StringBuilder();
st2.append("world");
String s2 = st2.toString();
System.out.println(s2);
}
}
2. 字符串的拼接
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int[] arr = {1,2,3,4};
String s = arrayToString(arr);
System.out.println(s);
}
public static String arrayToString(int[] arr){
StringBuilder st = new StringBuilder();
st.append("[");
for (int i=0;i<arr.length;i++){
if (i == arr.length-1){
st.append(arr[i]);
}else {
st.append(arr[i]).append(", ");
}
}
st.append("]");
String s = st.toString();
return s;
}
}
3. 字符串反转
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符:");
String line = sc.nextLine();
String result = myReversal(line);
System.out.println(result);
}
public static String myReversal(String s){
StringBuilder st = new StringBuilder(s);
st.reverse();
String s2 = st.toString();
return s2;
}
}
四、ArrayList集合
1. 集合概述
2. ArrayList方法添加和使用
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
ArrayList<String> ar = new ArrayList<>();
ar.add("hello");
ar.add("world");
ar.add("java");
ar.add(1,"javaee");
System.out.println(ar);
}
}
3. ArrayList常用方法
4. ArrayList存储和遍历
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<>();
for (int i=0;i<5;i++){
arr.add("这是集合"+i);
}
System.out.println("该数组集合长度是:"+arr.size());
for(int i = 0;i<arr.size();i++){
String s = arr.get(i);
System.out.println("数组"+i+"是:"+s);
}
}
}
5. 案例学生存储遍历
--------------------------------------------------------Student类
package com;
public class Student {
private String gender;
private String age;
public Student(){}
public Student(String gender, String age){
this.gender = gender;
this.age = age;
}
public void setgender(String gender) {
this.gender = gender;
}
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return age;
}
public String getgender() {
return gender;
}
@Override
public String toString() {
return "Student{" +
"gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
--------------------------------------------------------Demo类
package com;
import java.util.ArrayList;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
ArrayList<Student> arr = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Student s1 = new Student();
Scanner sc = new Scanner(System.in);
System.out.println("请输入集合" + i + "的性别:");
s1.setgender(sc.nextLine());
System.out.println("请输入集合" + i + "的年龄:");
s1.setAge(sc.nextLine());
arr.add(s1);
}
for (int i = 0; i < arr.size(); i++) {
String s = arr.get(i).toString();
System.out.println(s);
}
}
}
五、Math
1. Math的常用方法
六、System
1. System的常用方法
七、Object类
1. toString
2. equals
3. 冒泡排序
import java.util.Arrays;
public class test {
public static void main(String[] args) {
int[] arr = {24,60,80,30,100,10};
System.out.println("排序前:" + Arrays.toString(arr));
for(int a=1; a<arr.length; a++){
for(int i=0;i<arr.length-a;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("第" + a + "次排序:" + Arrays.toString(arr));
}
}
}
4. Arrays类
八、基本类型包装类
1. 描述
项目
1. 案例学生管理系统
-----------------------------------------------------------------------Student类
package com;
import java.util.Scanner;
public class Student {
private String name;
private String address;
private int age;
private String sid;
public Student(){}
public Student(String name, String address, String sid, int age){
this.name = name;
this.address = address;
this.sid = sid;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public String getSid() {
return sid;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setSid(String sid) {
this.sid = sid;
}
}
-----------------------------------------------------------------------Demo类
package com;
import java.util.ArrayList;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
ArrayList<Student> arr = new ArrayList<>();
while (true) {
System.out.println("--------欢迎来到学生管理系统--------");
System.out.println("1.添加学生");
System.out.println("2.删除学生");
System.out.println("3.修改学生");
System.out.println("4.查看所有学生");
System.out.println("5.退出");
System.out.println("请输入你的选择:");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
switch (line) {
case "1":
addStudent(arr);
break;
case "2":
deleteStudent(arr);
break;
case "3":
amendStudent(arr);
break;
case "4":
checkStudent(arr);
break;
case "5":
System.out.println("谢谢使用");
System.exit(0);
}
}
}
public static void addStudent(ArrayList<Student> array) {
int index = -1;
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String sid = sc.nextLine();
for (int i=0;i<array.size();i++){
if (array.get(i).getSid().equals(sid)){
index = i;
break;
}
}
if (index != -1){
System.out.println("你输入的学号存在,请重新输入");
addStudent(array);
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
int age = Integer.parseInt(sc.nextLine());
System.out.println("请输入学生居住地:");
String address = sc.nextLine();
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
public static void checkStudent(ArrayList<Student> array) {
if (array.size() == 0){
System.out.println("暂无成员,请先添加数据!");
return;
}
System.out.println("学号\t\t\t姓名\t\t年龄\t\t住址");
for (int i = 0; i < array.size(); i++) {
String sid = array.get(i).getSid();
String name = array.get(i).getName();
int age = array.get(i).getAge();
String address = array.get(i).getAddress();
System.out.println(sid + "\t" + name + "\t" + age + "岁\t" + address);
}
}
public static void deleteStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学号:");
String sid = sc.nextLine();
int index = -1;
for (int i=0;i< array.size();i++){
if (array.get(i).getSid().equals(sid)){
index = i;
break;
}
}
if (index == -1){
System.out.println("学号不存在,请重新输入");
deleteStudent(array);
}else{
array.remove(index);
System.out.println("成功删除一条信息!");
checkStudent(array);
}
}
public static void amendStudent(ArrayList<Student> array) {
System.out.println("请输入要修改的学生的学号:");
Scanner sc = new Scanner(System.in);
String sid = sc.nextLine();
int index = -1;
for (int i=0;i< array.size();i++){
if (array.get(i).getSid().equals(sid)){
index = i;
break;
}
}
if (index == -1){
System.out.println("学号不存在,请重新输入");
amendStudent(array);
}else{
System.out.println("请输入学生的新学号");
sid = sc.nextLine();
array.get(index).setSid(sid);
System.out.println("请输入学生的新姓名");
String name = sc.nextLine();
array.get(index).setName(name);
System.out.println("请输入学生的新年龄");
int age = Integer.parseInt(sc.nextLine());
array.get(index).setAge(age);
System.out.println("请输入学生的新地址");
String address = sc.nextLine();
array.get(index).setAddress(address);
System.out.println("修改完毕");
checkStudent(array);
}
}
}