mirror of https://gitee.com/y_project/RuoYi.git
Pre Merge pull request !528 from CZHXX/master
commit
fff4594d9f
|
@ -0,0 +1,35 @@
|
|||
public class ForDemo1 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("----3次Hello World----");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (i == 1) {
|
||||
break; // 当i等于1时,跳出循环
|
||||
}
|
||||
System.out.println("Hello World");
|
||||
}
|
||||
|
||||
System.out.println("----4次Hello MySQL----");
|
||||
for (int i = 1; i < 5; i++) {
|
||||
if (i == 3) {
|
||||
continue; // 当i等于3时,跳过当前迭代
|
||||
}
|
||||
System.out.println("Hello MySQL");
|
||||
}
|
||||
|
||||
System.out.println("----5次Hello Spring----");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
if (i == 3) {
|
||||
continue; // 当i等于3时,跳过当前迭代
|
||||
}
|
||||
System.out.println("Hello Spring");
|
||||
}
|
||||
|
||||
System.out.println("----3次Hello Redis----");
|
||||
for (int i = 1; i <= 5; i += 2) {
|
||||
if (i == 3) {
|
||||
break; // 当i等于3时,跳出循环
|
||||
}
|
||||
System.out.println("Hello Redis");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
public class ForDemo2 {
|
||||
public static void main(String[] args){
|
||||
int [] numbers = {10, 20, 30, 40, 50};
|
||||
for(int x : numbers ){
|
||||
if (x == 30) {
|
||||
break; // 当x等于30时,跳出循环
|
||||
}
|
||||
System.out.print( x );
|
||||
System.out.print(",");
|
||||
}
|
||||
System.out.println();
|
||||
String [] names ={"James", "Larry", "Tom", "Lacy"};
|
||||
for( String name : names ) {
|
||||
if (name.equals("Tom")) {
|
||||
continue; // 当name等于"Tom"时,跳过当前循环,继续下一个循环
|
||||
}
|
||||
System.out.print( name );
|
||||
System.out.print(",");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
public class ForDemo3 {
|
||||
public static void main(String[] args) {
|
||||
// 使用break
|
||||
System.out.println("----使用break----");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
if (i == 3) {
|
||||
break;
|
||||
}
|
||||
System.out.println("Hello Break");
|
||||
}
|
||||
|
||||
// 使用continue
|
||||
System.out.println("----使用continue----");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
if (i == 3) {
|
||||
continue;
|
||||
}
|
||||
System.out.println("Hello Continue");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
public class IfDemo1 {
|
||||
public static void main(String[] args) {
|
||||
// 格式3: if(条件表达式){ 代码... } else if(条件表达式){ 代码... } ... else{ 代码... }
|
||||
int score = 95;
|
||||
// 绩效:0-60 C,60-80 B,80-90 A,90-100 A+
|
||||
if (score >= 0 && score < 60) {
|
||||
System.out.println("分数是:" + score + ",绩效是:C");
|
||||
} else if (score >= 60 && score < 80) {
|
||||
System.out.println("分数是:" + score + ",绩效是:B");
|
||||
} else if (score >= 80 && score < 90) {
|
||||
System.out.println("分数是:" + score + ",绩效是:A");
|
||||
} else if (score >= 90 && score <= 100) {
|
||||
System.out.println("分数是:" + score + ",绩效是:A+");
|
||||
} else {
|
||||
System.out.println("录入的分数有毛病!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
public class IfDemo2 {
|
||||
public static void main(String[] args) {
|
||||
// 嵌套的 if…else 语句
|
||||
int x = 30;
|
||||
int y = 10;
|
||||
if (x == 30) {
|
||||
if (y == 20) {
|
||||
System.out.print("X = 30 and Y = 20");
|
||||
} else {
|
||||
System.out.println("X = 30 and Y != 20");
|
||||
}
|
||||
} else {
|
||||
System.out.println("X != 30");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
public class Student {
|
||||
// 1.局部变量:在方法、构造函数或块内部声明的变量。
|
||||
public void run() {
|
||||
String weight = "100斤"; // 局部变量weight,必须有初始值。
|
||||
// 因为实例变量age没赋值,所以默认是0
|
||||
System.out.println(name + "现在" + age + "岁"); // 小明现在0岁
|
||||
System.out.println(name + weight + ",跑得很快"); // 小明100斤,跑得很快
|
||||
}
|
||||
|
||||
// 2.实例变量:在类中声明,但在方法、构造函数或块之外。
|
||||
public String name = "小明"; // 公有实例变量name对子类可见
|
||||
private int age; // 私有实例变量age,仅在该类可见,不赋值,默认是0
|
||||
|
||||
// main方法,程序入口
|
||||
public static void main(String[] args) {
|
||||
Student.onLineNumber++; // 22
|
||||
System.out.println(onLineNumber);
|
||||
Student student = new Student();
|
||||
student.run();
|
||||
student.eat("方便面");
|
||||
student.study();
|
||||
}
|
||||
|
||||
// 3.类变量:在类中用 static 关键字声明的变量,它们属于类而不是实例。
|
||||
public static int onLineNumber = 21;
|
||||
private String major = "计算机科学";
|
||||
|
||||
// 4.参数变量:是方法或构造函数声明中的变量
|
||||
public void eat(String food) { // 参数变量food
|
||||
System.out.println(name + "喜欢吃" + food); // 小明喜欢吃方便面
|
||||
}
|
||||
|
||||
// 自定义方法
|
||||
public void study() {
|
||||
System.out.println(name + "正在学习" + major);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
public class da {
|
||||
public static void main(String[] args) {
|
||||
int n = 7;
|
||||
if (n >= 0 && n < 10) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("He" + n + n +"o" + " Wor" + n + "d");
|
||||
}
|
||||
} else {
|
||||
System.out.println("n>10");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
public class da {
|
||||
public static void main(String[] args) {
|
||||
int n = 7;
|
||||
if (n >= 0 && n < 10) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("He" + n + n +"o" + " Wor" + n + "d");
|
||||
}
|
||||
} else {
|
||||
System.out.println("n>10");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ spring:
|
|||
master:
|
||||
url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||
username: root
|
||||
password: password
|
||||
password: root
|
||||
# 从库数据源
|
||||
slave:
|
||||
# 从数据源开关/默认关闭
|
||||
|
|
|
@ -16,7 +16,7 @@ ruoyi:
|
|||
# 开发环境配置
|
||||
server:
|
||||
# 服务器的HTTP端口,默认为80
|
||||
port: 80
|
||||
port: 5555
|
||||
servlet:
|
||||
# 应用的访问路径
|
||||
context-path: /
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ArrayToList {
|
||||
public static void main(String[] args) {
|
||||
String[] array = {"A", "B", "C", "D"};
|
||||
List<String> list = Arrays.asList(array);
|
||||
System.out.println("列表内容: " + list);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
public class ForDemo1 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("----3次Hello World----");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (i == 1) {
|
||||
break; // 当i等于1时,跳出循环
|
||||
}
|
||||
System.out.println("Hello World");
|
||||
}
|
||||
|
||||
System.out.println("----4次Hello MySQL----");
|
||||
for (int i = 1; i < 5; i++) {
|
||||
if (i == 3) {
|
||||
continue; // 当i等于3时,跳过当前迭代
|
||||
}
|
||||
System.out.println("Hello MySQL");
|
||||
}
|
||||
|
||||
System.out.println("----5次Hello Spring----");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
if (i == 3) {
|
||||
continue; // 当i等于3时,跳过当前迭代
|
||||
}
|
||||
System.out.println("Hello Spring");
|
||||
}
|
||||
|
||||
System.out.println("----3次Hello Redis----");
|
||||
for (int i = 1; i <= 5; i += 2) {
|
||||
if (i == 3) {
|
||||
break; // 当i等于3时,跳出循环
|
||||
}
|
||||
System.out.println("Hello Redis");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
public class ForDemo2 {
|
||||
public static void main(String[] args){
|
||||
int [] numbers = {10, 20, 30, 40, 50};
|
||||
for(int x : numbers ){
|
||||
if (x == 30) {
|
||||
break; // 当x等于30时,跳出循环
|
||||
}
|
||||
System.out.print( x );
|
||||
System.out.print(",");
|
||||
}
|
||||
System.out.println();
|
||||
String [] names ={"James", "Larry", "Tom", "Lacy"};
|
||||
for( String name : names ) {
|
||||
if (name.equals("Tom")) {
|
||||
continue; // 当name等于"Tom"时,跳过当前循环,继续下一个循环
|
||||
}
|
||||
System.out.print( name );
|
||||
System.out.print(",");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
public class ListToArray {
|
||||
public static void main(String[] args) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("A");
|
||||
list.add("B");
|
||||
list.add("C");
|
||||
// 列表转为数组
|
||||
String[] array = list.toArray(new String[0]);
|
||||
System.out.println("数组内容: " + list.toString());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
public class NewStudent {
|
||||
/**
|
||||
* 学生
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String age;//定义
|
||||
|
||||
/**
|
||||
* 年龄
|
||||
* @param inputName
|
||||
* @param inputAge
|
||||
*/
|
||||
public NewStudent(String inputName,String inputAge){
|
||||
this.name = inputName;
|
||||
this.age = inputAge;
|
||||
}
|
||||
public void eat(){
|
||||
System.out.println("吃饭");
|
||||
}
|
||||
|
||||
/**
|
||||
* 吃饭
|
||||
* @param food
|
||||
*/
|
||||
|
||||
public void eat(String food){
|
||||
System.out.println("学生吃的"+food);
|
||||
}
|
||||
public String eat(String food,String time){
|
||||
System.out.println("学生在" + time + "吃的" + food);
|
||||
return"吃饱了";//定义吃饭的方法
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
public class Student {
|
||||
// 1.局部变量:在方法、构造函数或块内部声明的变量。
|
||||
public void run() {
|
||||
String weight = "100斤"; // 局部变量weight,必须有初始值。
|
||||
// 因为实例变量age没赋值,所以默认是0
|
||||
System.out.println(name + "现在" + age + "岁"); // 小明现在0岁
|
||||
System.out.println(name + weight + ",跑得很快"); // 小明100斤,跑得很快
|
||||
}
|
||||
|
||||
// 2.实例变量:在类中声明,但在方法、构造函数或块之外。
|
||||
public String name = "小明"; // 公有实例变量name对子类可见
|
||||
private int age; // 私有实例变量age,仅在该类可见,不赋值,默认是0
|
||||
|
||||
// main方法,程序入口
|
||||
public static void main(String[] args) {
|
||||
Student.onLineNumber++; // 22
|
||||
System.out.println(onLineNumber);
|
||||
Student student = new Student();
|
||||
student.run();
|
||||
student.eat("方便面");
|
||||
student.study();
|
||||
}
|
||||
|
||||
// 3.类变量:在类中用 static 关键字声明的变量,它们属于类而不是实例。
|
||||
public static int onLineNumber = 21;
|
||||
private String major = "计算机科学";
|
||||
|
||||
// 4.参数变量:是方法或构造函数声明中的变量
|
||||
public void eat(String food) { // 参数变量food
|
||||
System.out.println(name + "喜欢吃" + food); // 小明喜欢吃方便面
|
||||
}
|
||||
|
||||
// 自定义方法
|
||||
public void study() {
|
||||
System.out.println(name + "正在学习" + major);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
public class NewStudent {
|
||||
private String name;
|
||||
private int age;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
public class TestArray {
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// double类型的数组
|
||||
double[] scores = new double[]{99.5, 88.0, 75.5};
|
||||
double[] scores1 = {99.5, 88.0, 75.5}; // 简化写法
|
||||
|
||||
// int类型的数组
|
||||
int[] ages = new int[]{12, 24, 36};
|
||||
int[] ages1 = {12, 24, 36}; // 简化写法
|
||||
|
||||
int size = 5; // 设置数组大小是10
|
||||
String[] names = new String[size]; // 定义数组
|
||||
names[0] = "孙悟空";
|
||||
names[1] = "贝吉塔";
|
||||
names[2] = "弗利萨";
|
||||
names[3] = "沙鲁";
|
||||
names[4] = "魔人布欧";
|
||||
// 编译不报错,运行报错,因为数组大小是5,这是数组里第6个元素,数组越界ArrayIndexOutOfBoundsException
|
||||
// names[5] = "布尔玛";
|
||||
// 数组索引从 0 开始,所以索引值从 0 到 arrayRefVar.length-1
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
System.out.print("索引:" + i + names[i] + " ");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
// 数组变量名存储的是数组在内存种的地址,数组是引用类型
|
||||
// [ 指的是 数组
|
||||
// D 指的是 数据类型double
|
||||
// @ 指的是 在哪里
|
||||
System.out.println(scores); // [D@776ec8df,指的是数组的十六进制地址
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
public class TestStudent {
|
||||
public static void main(String[] args){
|
||||
NewStudent student = new NewStudent("czh","19");
|
||||
student.eat();
|
||||
student.eat("sda");
|
||||
student.eat("sda");
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
public class da {
|
||||
public static void main(String[] args) {
|
||||
int n = 7;
|
||||
if (n >= 0 && n < 10) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("He" + n + n +"o" + " Wor" + n + "d");
|
||||
}
|
||||
} else {
|
||||
System.out.println("n>10");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
public class ss {
|
||||
public static void main(String[] args) {
|
||||
String studentId = "20233011082";
|
||||
char lastDigit1 = studentId.charAt(studentId.length() - 2);
|
||||
char lastDigit2 = studentId.charAt(studentId.length() - 1);
|
||||
char[] lastTwoDigits = {lastDigit1, lastDigit2};
|
||||
for (char c : lastTwoDigits) {
|
||||
System.out.print(c + " ");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
|
||||
/**
|
||||
* 定义一个累
|
||||
*/
|
||||
public class Java {
|
||||
/**
|
||||
* 三个与类名相关的属性
|
||||
*/
|
||||
private String name1;
|
||||
private String name2;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param name2
|
||||
*/
|
||||
public Java(String name, String name2) {
|
||||
this.name1 = name1;
|
||||
this.name2 = name2;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* main函数创建对象并调用两个方法
|
||||
*/
|
||||
|
||||
public void java() {
|
||||
System.out.println(name1 + "cc");
|
||||
}
|
||||
public void sha() {
|
||||
System.out.println(name2 + "xx");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* A级教练实现
|
||||
*/
|
||||
public class ACoach implements Coach{
|
||||
@Override
|
||||
public void command(){
|
||||
System.out.println("我是A级证书教练");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* A级教练工厂实现
|
||||
*/
|
||||
public class ACoachFactory implements CoachFactory {
|
||||
@Override
|
||||
public Coach createCoach() {
|
||||
return new ACoach();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ArrayToList
|
||||
*/
|
||||
public class ArrayToList {
|
||||
/**
|
||||
* main方法
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String[] array = {"A", "B", "C", "D"};
|
||||
/**
|
||||
* 数组转为数列
|
||||
*/
|
||||
List<String> list = Arrays.asList(array);
|
||||
System.out.println("列表内容: " + list);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* 定义一个表示书本类型的接口
|
||||
*/
|
||||
public interface Book {
|
||||
String name();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* 定义一个表示书本类型的接口
|
||||
*/
|
||||
public interface BookInterface {
|
||||
String BookName();
|
||||
String BookAuthor();
|
||||
String BookISBN();
|
||||
void setBookInfo(String name, String author, String isbn);
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
public class Booking {
|
||||
public static void main(String[] args) {
|
||||
// 创建图书对象
|
||||
Books books = new Books();
|
||||
|
||||
// 设置图书信息
|
||||
books.setBookInfo("安德的游戏", "安德", "978-7-111-60453-3");
|
||||
|
||||
// 获取并打印图书名称
|
||||
System.out.println("名称: " + books.BookName());
|
||||
|
||||
// 获取并打印图书作者
|
||||
System.out.println("作者: " + books.BookAuthor());
|
||||
|
||||
// 获取并打印图书ISBN
|
||||
System.out.println("ISBN: " + books.BookISBN());
|
||||
|
||||
// 使用多态形式调用图书信息类的方法
|
||||
BookInterface bookInterface = new Books();
|
||||
bookInterface.setBookInfo("中华上下五千年", "袁堂欣", "978-0-13-468599-1");
|
||||
System.out.println("名称: " + bookInterface.BookName());
|
||||
System.out.println("作者: " + bookInterface.BookAuthor());
|
||||
System.out.println("ISBN: " + bookInterface.BookISBN());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
public class Books implements BookInterface {
|
||||
private String name;
|
||||
private String author;
|
||||
private String isbn;
|
||||
|
||||
@Override
|
||||
public String BookName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String BookAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String BookISBN() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBookInfo(String name, String author, String isbn) {
|
||||
this.name = name;
|
||||
this.author = author;
|
||||
this.isbn = isbn;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* C级教练实现
|
||||
*/
|
||||
public class CCoach implements Coach{
|
||||
@Override
|
||||
public void command() {
|
||||
System.out.println("我是C级证书教练");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* C级教练工厂实现
|
||||
*/
|
||||
public class CCoachFactory implements CoachFactory{
|
||||
@Override
|
||||
public Coach createCoach() {
|
||||
return new CCoach();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* 实现Shape接口的圆类
|
||||
*/
|
||||
public class Circle implements Shape {
|
||||
@Override
|
||||
public String name() {
|
||||
return "圆";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* 教练接口
|
||||
*/
|
||||
public interface Coach {
|
||||
void command();
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* 教练工厂接口
|
||||
*/
|
||||
public interface CoachFactory {
|
||||
Coach createCoach();
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
/**
|
||||
* 演示程序
|
||||
*/
|
||||
public class Demo {
|
||||
public static void create(CoachFactory factory) {
|
||||
factory.createCoach().command();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
create(new ACoachFactory());
|
||||
create(new CCoachFactory());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
public class ForDemo1 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("----3次Hello World----");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (i == 1) {
|
||||
|
||||
/**
|
||||
*当1等于3时,跳出当前循环
|
||||
*/
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
System.out.println("Hello World");
|
||||
}
|
||||
|
||||
System.out.println("----4次Hello MySQL----");
|
||||
for (int i = 1; i < 5; i++) {
|
||||
if (i == 3) {
|
||||
|
||||
/**
|
||||
*当1等于3时,跳出当前循环
|
||||
*/
|
||||
|
||||
continue;
|
||||
}
|
||||
System.out.println("Hello MySQL");
|
||||
}
|
||||
|
||||
System.out.println("----5次Hello Spring----");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
if (i == 3) {
|
||||
|
||||
/**
|
||||
*当i等于3时,跳过当前迭代
|
||||
*/
|
||||
|
||||
continue;
|
||||
}
|
||||
System.out.println("Hello Spring");
|
||||
}
|
||||
|
||||
System.out.println("----3次Hello Redis----");
|
||||
for (int i = 1; i <= 5; i += 2) {
|
||||
if (i == 3) {
|
||||
/**
|
||||
*当i等于3时,跳过当前迭代
|
||||
*/
|
||||
|
||||
break;
|
||||
}
|
||||
System.out.println("Hello Redis");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
public class ForDemo2 {
|
||||
public static void main(String[] args){
|
||||
int [] numbers = {10, 20, 30, 40, 50};
|
||||
for(int x : numbers ){
|
||||
if (x == 30) {
|
||||
|
||||
/**
|
||||
*当x等于30时,跳出循环
|
||||
*/
|
||||
|
||||
break;
|
||||
}
|
||||
System.out.print( x );
|
||||
System.out.print(",");
|
||||
}
|
||||
System.out.println();
|
||||
String [] names ={"James", "Larry", "Tom", "Lacy"};
|
||||
for( String name : names ) {
|
||||
if (name.equals("Tom")) {
|
||||
|
||||
/**
|
||||
*当name等于"Tom"时,跳过当前循环,继续下一个循环
|
||||
*/
|
||||
|
||||
continue;
|
||||
}
|
||||
System.out.print( name );
|
||||
System.out.print(",");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
public class ForDemo3 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
/**
|
||||
* 使用break
|
||||
*/
|
||||
|
||||
System.out.println("----使用break----");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
if (i == 3) {
|
||||
break;
|
||||
}
|
||||
System.out.println("Hello Break");
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用continue
|
||||
*/
|
||||
|
||||
System.out.println("----使用continue----");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
if (i == 3) {
|
||||
continue;
|
||||
}
|
||||
System.out.println("Hello Continue");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
/**
|
||||
* 实现Book接口的历史类
|
||||
*/
|
||||
public class History implements Book {
|
||||
@Override
|
||||
public String name() {
|
||||
return "历史";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
public class IfDemo1 {
|
||||
public static void main(String[] args) {
|
||||
// 格式3: if(条件表达式){ 代码... } else if(条件表达式){ 代码... } ... else{ 代码... }
|
||||
int score = 95;
|
||||
// 绩效:0-60 C,60-80 B,80-90 A,90-100 A+
|
||||
if (score >= 0 && score < 60) {
|
||||
System.out.println("分数是:" + score + ",绩效是:C");
|
||||
} else if (score >= 60 && score < 80) {
|
||||
System.out.println("分数是:" + score + ",绩效是:B");
|
||||
} else if (score >= 80 && score < 90) {
|
||||
System.out.println("分数是:" + score + ",绩效是:A");
|
||||
} else if (score >= 90 && score <= 100) {
|
||||
System.out.println("分数是:" + score + ",绩效是:A+");
|
||||
} else {
|
||||
System.out.println("录入的分数有毛病!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
public class IfDemo2 {
|
||||
public static void main(String[] args) {
|
||||
// 嵌套的 if…else 语句
|
||||
int x = 30;
|
||||
int y = 10;
|
||||
if (x == 30) {
|
||||
if (y == 20) {
|
||||
System.out.print("X = 30 and Y = 20");
|
||||
} else {
|
||||
System.out.println("X = 30 and Y != 20");
|
||||
}
|
||||
} else {
|
||||
System.out.println("X != 30");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package test.java;
|
||||
|
||||
/**
|
||||
* 定义一个累
|
||||
*/
|
||||
|
||||
public class Java {
|
||||
|
||||
/**
|
||||
* 三个与类名相关的属性
|
||||
*/
|
||||
|
||||
private String name1;
|
||||
private String name2;
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param name2
|
||||
*/
|
||||
|
||||
|
||||
public Java(String name, String name2) {
|
||||
this.name1 = name1;
|
||||
this.name2 = name2;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* main函数创建对象并调用两个方法
|
||||
*/
|
||||
|
||||
public void java() {
|
||||
System.out.println(name1 + "cc");
|
||||
}
|
||||
public void sha() {
|
||||
System.out.println(name2 + "xx");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
* ListToArray
|
||||
*/
|
||||
public class ListToArray {
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("A");
|
||||
list.add("B");
|
||||
list.add("C");
|
||||
/**
|
||||
* 数组转为数列
|
||||
*/
|
||||
String[] array = list.toArray(new String[0]);
|
||||
System.out.println("数组内容: " + list.toString());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
/**
|
||||
* 实现Book接口的文学类
|
||||
*/
|
||||
public class Literature implements Book {
|
||||
@Override
|
||||
public String name() {
|
||||
return "文学";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
//字符与字符串查找
|
||||
public class New{
|
||||
public static void main(String args[]){
|
||||
String str = "How qi bocome handsome like qi ge"; //定义一个长字符串
|
||||
System.out.println("该字符串为:"+str);
|
||||
/***1、indexOf()方法查找字符首个出现位置格式1,2***/
|
||||
int index1 = str.indexOf(" "); //找到第一个空格所在的索引
|
||||
int index2 = str.indexOf(" ",4); //找到索引4以后的第一个空格所在索引
|
||||
System.out.println("第一个空格所在索引为:"+index1);
|
||||
System.out.println("索引4以后的第一个空格所在索引为:"+index2);
|
||||
System.out.println("*****************");
|
||||
/***2、lastIndexOf()方法查找字符最后出现位置格式1,2***/
|
||||
int index3 = str.lastIndexOf(" "); //找到最后一个空格所在的索引
|
||||
int index4 = str.lastIndexOf(" ",10);//找到索引10以后的第一个空格所在索引
|
||||
System.out.println("最后一个空格所在索引为:"+index3);
|
||||
System.out.println("索引10以前最后一个空格所在索引为:"+index4);
|
||||
System.out.println("*****************");
|
||||
/***3、indexOf()方法查找子字符串第一次出现位置格式1,2***/
|
||||
int index5 = str.indexOf("qi"); //找到"qi"子字符串第一次出现位置的索引
|
||||
int index6 = str.indexOf("qi",5);//找到索引5以后子字符串"qi"第一个出现位置所在索引
|
||||
System.out.println("子字符串qi第一次出现位置的索引号为:"+index5);
|
||||
System.out.println("索引5以后子字符串qi第一次出现位置的索引号为:"+index6);
|
||||
System.out.println("*****************");
|
||||
/***4、lastIndexOf()方法查找子字符串最后一次出现位置格式1,2***/
|
||||
int index7 = str.lastIndexOf("qi");
|
||||
int index8 = str.lastIndexOf("qi",5);
|
||||
System.out.println("子字符串qi最后一次出现位置的索引号为:"+index7);
|
||||
System.out.println("索引号5以后子字符串qi最后一次出现位置的索引号为:"+index8);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
public class NewStudent {
|
||||
/**
|
||||
* 学生
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String age;//定义
|
||||
|
||||
/**
|
||||
* 年龄
|
||||
* @param inputName
|
||||
* @param inputAge
|
||||
*/
|
||||
public NewStudent(String inputName,String inputAge){
|
||||
this.name = inputName;
|
||||
this.age = inputAge;
|
||||
}
|
||||
public void eat(){
|
||||
System.out.println("吃饭");
|
||||
}
|
||||
|
||||
/**
|
||||
* 吃饭
|
||||
* @param food
|
||||
*/
|
||||
|
||||
public void eat(String food){
|
||||
System.out.println("学生吃的"+food);
|
||||
}
|
||||
public String eat(String food,String time){
|
||||
System.out.println("学生在" + time + "吃的" + food);
|
||||
return"吃饱了";//定义吃饭的方法
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* 定义一个表示形状的接口
|
||||
*/
|
||||
public interface Shape {
|
||||
String name();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* 实现Shape接口的正方形类
|
||||
*/
|
||||
public class Square implements Shape {
|
||||
@Override
|
||||
public String name() {
|
||||
return "正方形";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
/**
|
||||
* @ClassName: StringBufferDemo1
|
||||
* @Description: StringBuffer 拼接字符串
|
||||
* 多线程操作大量数据,用StringBuffer。
|
||||
* StringBuffer 对方法加了synchronized(同步锁)或者对调用的方法加了同步锁,所以是线程安全的。
|
||||
* StringBuffer和StringBuilder这两个可变类的原理和操作基本相同。
|
||||
* @author: Zh
|
||||
* @date: 2024/4/8 16:56
|
||||
*/
|
||||
public class StringBufferDemo1 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
/**
|
||||
* 创建StringBuffer对象
|
||||
*/
|
||||
|
||||
StringBuffer sb = new StringBuffer("孙悟空");
|
||||
|
||||
/**
|
||||
* 在字符串后面追加新的字符串
|
||||
*/
|
||||
|
||||
sb.append("会龟派气功");
|
||||
|
||||
/**
|
||||
* StringBuffer对象:孙悟空会龟派气功
|
||||
*/
|
||||
|
||||
System.out.println("StringBuffer对象:" + sb);
|
||||
|
||||
/**
|
||||
* 删除指定位置上的字符串,从指定的下标开始和结束,下标从0开始
|
||||
*/
|
||||
|
||||
/**
|
||||
* 0 孙 1悟 2空 3会 4龟 5派 6气 7功
|
||||
*/
|
||||
|
||||
/**
|
||||
* 2空 3会
|
||||
*/
|
||||
|
||||
sb.delete(2, 4);
|
||||
|
||||
/**
|
||||
* delete删除:孙悟龟派气功
|
||||
*/
|
||||
|
||||
System.out.println("delete删除:" + sb);
|
||||
|
||||
/**
|
||||
* 在指定下标位置上添加指定的字符串
|
||||
*/
|
||||
|
||||
sb.insert(2, "123");
|
||||
System.out.println("insert新增:" + sb); // insert新增:孙悟123龟派气功
|
||||
|
||||
/**
|
||||
* 将字符串翻转
|
||||
*/
|
||||
|
||||
sb.reverse();
|
||||
|
||||
/**
|
||||
* reverse反转:功气派龟321悟孙
|
||||
*/
|
||||
|
||||
System.out.println("reverse反转:" + sb);
|
||||
|
||||
|
||||
/**
|
||||
* 将StringBuffer转换成String类型
|
||||
*/
|
||||
|
||||
|
||||
String s = sb.toString();
|
||||
|
||||
/**
|
||||
* String类型:功气派龟321悟孙
|
||||
*/
|
||||
|
||||
check(s); //
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* 检查传入的参数是否是String类型
|
||||
|
||||
*
|
||||
|
||||
* @param abc
|
||||
|
||||
*/
|
||||
|
||||
public static void check(String abc) {
|
||||
|
||||
System.out.println("String类型:" + abc);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
/**
|
||||
* @ClassName: StringBuilderDemo1
|
||||
* @Description: StringBuilder 拼接字符串
|
||||
* 单线程操作大量数据,用StringBuilder。
|
||||
* StringBuilder 并没有对方法进行加synchronized(同步锁),所以是非线程安全的。
|
||||
* StringBuffer和StringBuilder这两个可变类的原理和操作基本相同。
|
||||
* @author: Zh
|
||||
* @date: 2024/4/8 16:41
|
||||
*/
|
||||
|
||||
public class StringBuilderDemo1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// StringBuilder的字符串拼接.append
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
/**
|
||||
* 字符
|
||||
*/
|
||||
|
||||
sb.append("a");
|
||||
|
||||
/**
|
||||
* 整数
|
||||
*/
|
||||
|
||||
sb.append(1);
|
||||
|
||||
/**
|
||||
* 布尔类型
|
||||
*/
|
||||
|
||||
sb.append(false);
|
||||
|
||||
/**
|
||||
* 小数
|
||||
*/
|
||||
|
||||
sb.append(3.3);
|
||||
|
||||
/**
|
||||
* 字符串
|
||||
*/
|
||||
|
||||
sb.append("abc");
|
||||
|
||||
/**
|
||||
* append的应用:a1false3.3abc
|
||||
*/
|
||||
|
||||
System.out.println("append的应用:" + sb);
|
||||
|
||||
StringBuilder sb1 = new StringBuilder();
|
||||
|
||||
/**
|
||||
* 支持链式编程
|
||||
*/
|
||||
|
||||
sb1.append("a").append("b").append("c").append("我爱你中国");
|
||||
|
||||
/**
|
||||
* append支持链式编程:abc我爱你中国
|
||||
*/
|
||||
|
||||
System.out.println("append支持链式编程:" + sb1);
|
||||
|
||||
/**
|
||||
* reverse反转
|
||||
*/
|
||||
|
||||
sb1.reverse();
|
||||
|
||||
/**
|
||||
* reverse反转:国中你爱我cba
|
||||
*/
|
||||
|
||||
System.out.println("reverse反转:" + sb1);
|
||||
|
||||
/**
|
||||
* 字符串长度length
|
||||
* 字符串长度length:8
|
||||
*/
|
||||
|
||||
System.out.println("字符串长度length:" + sb1.length());
|
||||
|
||||
/**
|
||||
* 注意:StringBuilder只是拼接字符串的手段:效率好。
|
||||
* 最终的目的还是要恢复成String类型。
|
||||
*/
|
||||
|
||||
StringBuilder sb2 = new StringBuilder();
|
||||
sb2.append("123").append("阿拉伯数字");
|
||||
|
||||
/**
|
||||
* check(sb2); // 报错,因为sb2不是String类型
|
||||
* 拼接字符串:123阿拉伯数字
|
||||
*/
|
||||
|
||||
System.out.println("拼接字符串:" + sb2);
|
||||
|
||||
/**
|
||||
* 恢复成String类型
|
||||
*/
|
||||
|
||||
String rs = sb2.toString();
|
||||
|
||||
/**
|
||||
* String类型:123阿拉伯数字
|
||||
*/
|
||||
|
||||
check(rs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查传入的参数是否是String类型
|
||||
*
|
||||
* @param abc
|
||||
*/
|
||||
|
||||
public static void check(String abc) {
|
||||
System.out.println("String类型:" + abc);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
public class Student {
|
||||
// 1.局部变量:在方法、构造函数或块内部声明的变量。
|
||||
public void run() {
|
||||
String weight = "100斤"; // 局部变量weight,必须有初始值。
|
||||
// 因为实例变量age没赋值,所以默认是0
|
||||
System.out.println(name + "现在" + age + "岁"); // 小明现在0岁
|
||||
System.out.println(name + weight + ",跑得很快"); // 小明100斤,跑得很快
|
||||
}
|
||||
|
||||
// 2.实例变量:在类中声明,但在方法、构造函数或块之外。
|
||||
public String name = "小明"; // 公有实例变量name对子类可见
|
||||
private int age; // 私有实例变量age,仅在该类可见,不赋值,默认是0
|
||||
|
||||
// main方法,程序入口
|
||||
public static void main(String[] args) {
|
||||
Student.onLineNumber++; // 22
|
||||
System.out.println(onLineNumber);
|
||||
Student student = new Student();
|
||||
student.run();
|
||||
student.eat("方便面");
|
||||
student.study();
|
||||
}
|
||||
|
||||
// 3.类变量:在类中用 static 关键字声明的变量,它们属于类而不是实例。
|
||||
public static int onLineNumber = 21;
|
||||
private String major = "计算机科学";
|
||||
|
||||
// 4.参数变量:是方法或构造函数声明中的变量
|
||||
public void eat(String food) { // 参数变量food
|
||||
System.out.println(name + "喜欢吃" + food); // 小明喜欢吃方便面
|
||||
}
|
||||
|
||||
// 自定义方法
|
||||
public void study() {
|
||||
System.out.println(name + "正在学习" + major);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
public class TestArray {
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// double类型的数组
|
||||
double[] scores = new double[]{99.5, 88.0, 75.5};
|
||||
double[] scores1 = {99.5, 88.0, 75.5}; // 简化写法
|
||||
|
||||
// int类型的数组
|
||||
int[] ages = new int[]{12, 24, 36};
|
||||
int[] ages1 = {12, 24, 36}; // 简化写法
|
||||
|
||||
int size = 5; // 设置数组大小是10
|
||||
String[] names = new String[size]; // 定义数组
|
||||
names[0] = "孙悟空";
|
||||
names[1] = "贝吉塔";
|
||||
names[2] = "弗利萨";
|
||||
names[3] = "沙鲁";
|
||||
names[4] = "魔人布欧";
|
||||
// 编译不报错,运行报错,因为数组大小是5,这是数组里第6个元素,数组越界ArrayIndexOutOfBoundsException
|
||||
// names[5] = "布尔玛";
|
||||
// 数组索引从 0 开始,所以索引值从 0 到 arrayRefVar.length-1
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
System.out.print("索引:" + i + names[i] + " ");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
// 数组变量名存储的是数组在内存种的地址,数组是引用类型
|
||||
// [ 指的是 数组
|
||||
// D 指的是 数据类型double
|
||||
// @ 指的是 在哪里
|
||||
System.out.println(scores); // [D@776ec8df,指的是数组的十六进制地址
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
* 测试多态性的类
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 测试类 演示多样性
|
||||
*/
|
||||
public class TestBook {
|
||||
public static void main (String[] args) {
|
||||
List<Book> books = new ArrayList<>();
|
||||
Book literatureBook = new Literature();
|
||||
Book historyBook = new History();
|
||||
books.add(literatureBook);
|
||||
books.add(historyBook);
|
||||
for (Book book : books){
|
||||
System.out.println(book.name());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
* 测试多态性的类
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 测试类 演示多样性
|
||||
*/
|
||||
public class TestShape {
|
||||
public static void main(String[] args) {
|
||||
List<Shape> shapes = new ArrayList<>();
|
||||
Shape circleShape = new Circle(); // 创建Circle
|
||||
Shape squareShape = new Square(); // 创建Square
|
||||
shapes.add(circleShape); // 将Circle添加到列表中
|
||||
shapes.add(squareShape); // 将Square添加到列表中
|
||||
for (Shape shape : shapes) { // 遍历列表中的每个Shape对象
|
||||
System.out.println(shape.name()); // 打印
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
public class TestTop {
|
||||
public static void main(String[] args){
|
||||
Top topPerson = new Top("xx","20","2");//创建top对象
|
||||
topPerson.name();
|
||||
topPerson.age();
|
||||
topPerson.ranking();//输出
|
||||
|
||||
Top topPer = new Top("cc","21","1");//创建top对象
|
||||
topPer.name();
|
||||
topPer.age();
|
||||
topPer.ranking();//输出
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
* 表示排名的类
|
||||
*/
|
||||
public class Top {
|
||||
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
*年龄
|
||||
*/
|
||||
private String age;
|
||||
|
||||
/**
|
||||
* 名次
|
||||
*/
|
||||
private String ranking;
|
||||
|
||||
/**
|
||||
* 构造法
|
||||
* @param name
|
||||
* @param age
|
||||
* @param ranking
|
||||
*/
|
||||
public Top(String name,String age,String ranking){
|
||||
this.name=name;
|
||||
this.age=age;
|
||||
this.ranking=ranking;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取名字
|
||||
*/
|
||||
public void name() {
|
||||
System.out.println("名字: " + name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取年龄
|
||||
*/
|
||||
public void age() {
|
||||
System.out.println("年龄: " + age);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取名次
|
||||
*/
|
||||
public void ranking() {
|
||||
System.out.println("名次: " + ranking);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取名字
|
||||
*/
|
||||
public void name1() {
|
||||
System.out.println("名字: " + name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取年龄
|
||||
*/
|
||||
public void age1() {
|
||||
System.out.println("年龄: " + age);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取名次
|
||||
*/
|
||||
public void ranking1() {
|
||||
System.out.println("名次: " + ranking);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
public class Word {
|
||||
public static void main(String[] args) {
|
||||
int n = 7;
|
||||
if (n >= 0 && n < 10) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println("He" + n + n +"o" + " Wor" + n + "d");
|
||||
}
|
||||
} else {
|
||||
System.out.println("n>10");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
public class Wount {
|
||||
public static void main(String[] args) {
|
||||
String studentId = "20233011082";
|
||||
char lastDigit1 = studentId.charAt(studentId.length() - 2);
|
||||
char lastDigit2 = studentId.charAt(studentId.length() - 1);
|
||||
char[] lastTwoDigits = {lastDigit1, lastDigit2};
|
||||
for (char c : lastTwoDigits) {
|
||||
System.out.print(c + " ");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
//字符与字符串查找
|
||||
public class da{
|
||||
public static void main(String args[]){
|
||||
String str = "How qi bocome handsome like qi ge"; //定义一个长字符串
|
||||
System.out.println("该字符串为:"+str);
|
||||
/***1、indexOf()方法查找字符首个出现位置格式1,2***/
|
||||
int index1 = str.indexOf(" "); //找到第一个空格所在的索引
|
||||
int index2 = str.indexOf(" ",4); //找到索引4以后的第一个空格所在索引
|
||||
System.out.println("第一个空格所在索引为:"+index1);
|
||||
System.out.println("索引4以后的第一个空格所在索引为:"+index2);
|
||||
System.out.println("*****************");
|
||||
/***2、lastIndexOf()方法查找字符最后出现位置格式1,2***/
|
||||
int index3 = str.lastIndexOf(" "); //找到最后一个空格所在的索引
|
||||
int index4 = str.lastIndexOf(" ",10);//找到索引10以后的第一个空格所在索引
|
||||
System.out.println("最后一个空格所在索引为:"+index3);
|
||||
System.out.println("索引10以前最后一个空格所在索引为:"+index4);
|
||||
System.out.println("*****************");
|
||||
/***3、indexOf()方法查找子字符串第一次出现位置格式1,2***/
|
||||
int index5 = str.indexOf("qi"); //找到"qi"子字符串第一次出现位置的索引
|
||||
int index6 = str.indexOf("qi",5);//找到索引5以后子字符串"qi"第一个出现位置所在索引
|
||||
System.out.println("子字符串qi第一次出现位置的索引号为:"+index5);
|
||||
System.out.println("索引5以后子字符串qi第一次出现位置的索引号为:"+index6);
|
||||
System.out.println("*****************");
|
||||
/***4、lastIndexOf()方法查找子字符串最后一次出现位置格式1,2***/
|
||||
int index7 = str.lastIndexOf("qi");
|
||||
int index8 = str.lastIndexOf("qi",5);
|
||||
System.out.println("子字符串qi最后一次出现位置的索引号为:"+index7);
|
||||
System.out.println("索引号5以后子字符串qi最后一次出现位置的索引号为:"+index8);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue