package demo1;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
System.out.println("hellowrold"+n);
}
}
如果case==1则执行
end.foi == for(int i=0;i<end;i++)
arr.fori == for(int i=0;i<arr.lenght;i++)
输入字符·
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
char c = scanner.next().charAt(0);
判断字符串是否相同
String a=new String("abc");
String b=new String("abc");
a.equals(b);
输入字符串
Scanner in = new Scanner(System.in);
String s = in.nextLine();
package com.itheima.test;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
/* 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。
按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。*/
//分析:
//1.键盘录入机票原价、月份、头等舱或经济舱
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票的原价");
int ticket = sc.nextInt();
System.out.println("请输入当前的月份");
int month = sc.nextInt();
System.out.println("请输入当前购买的舱位 0 头等舱 1 经济舱");
int seat = sc.nextInt();
//2.先判断月份是旺季还是淡季
//ctrl + alt + M 自动抽取方法
if (month >= 5 && month <= 10) {
//旺季 //3.继续判断当前机票是经济舱还是头等舱
//ticket = getPrice(ticket, seat, 0.9, 0.85);
ticket = getTicket(ticket, seat, 0.9, 0.85);
} else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) {
//淡季
//ticket = getPrice(ticket, seat, 0.7, 0.65);
ticket = getTicket(ticket, seat, 0.7, 0.65);
} else {
//表示键盘录入的月份是一个非法数据
System.out.println("键盘录入的月份不合法");
}
System.out.println(ticket);
}
public static int getTicket(int ticket, int seat, double v, double v2) {
if (seat == 0) {
//头等舱
ticket = (int) (ticket * v);
} else if (seat == 1) {
//经济舱
ticket = (int) (ticket * v2);
} else {
System.out.println("没有这个舱位");
}
return ticket;
}
//1.我要干嘛?根据舱位和折扣来计算最终的票价
//2.我干这件事,需要什么才能完成?原价 舱位 头等舱的折扣 经济舱的折扣
//3.方法的调用处是否需要继续使用这个结果 需要
/* public static int getPrice(int ticket, int seat, double v0, double v1) {
if (seat == 0) {
//头等舱
ticket = (int) (ticket * v0);
} else if (seat == 1) {
//经济舱
ticket = (int) (ticket * v1);
} else {
System.out.println("没有这个舱位");
}
return ticket;
}*/
}
package com.itheima.test;
import java.util.Random;
public class Test3 {
public static void main(String[] args) {
/* 需求:
定义方法实现随机产生一个5位的验证码
验证码格式:
长度为5
前四位是大写字母或者小写字母
最后一位是数字
*/
//方法:
//在以后如果我们要在一堆没有什么规律的数据中随机抽取
//可以先把这些数据放到数组当中
//再随机抽取一个索引
//分析:
//1.大写字母和小写字母都放到数组当中
char[] chs = new char[52];
for (int i = 0; i < chs.length; i++) {
//ASCII码表
if(i <= 25){
//添加小写字母
chs[i] = (char)(97 + i);
}else{//27
//添加大写字母
// A --- 65
chs[i] = (char)(65 + i - 26);
}
}
//定义一个字符串类型的变量,用来记录最终的结果
String result = "";
//2.随机抽取4次
//随机抽取数组中的索引
Random r = new Random();
for (int i = 0; i < 4; i++) {
int randomIndex = r.nextInt(chs.length);
//利用随机索引,获取对应的元素
//System.out.println(chs[randomIndex]);
result = result + chs[randomIndex];
}
//System.out.println(result);
//3.随机抽取一个数字0~9
int number = r.nextInt(10);
//生成最终的结果
result = result + number;
//打印最终结果
System.out.println(result);
}
}
shift + f6 改一堆
package com.itheima.test;
import java.util.Random;
public class Test9 {
public static void main(String[] args) {
/* 需求:
一个大V直播抽奖,奖品是现金红包,分别有{2, 588 , 888, 1000, 10000}五个奖金。
请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。
打印效果如下:(随机顺序,不一定是下面的顺序)
888元的奖金被抽出
588元的奖金被抽出
10000元的奖金被抽出
1000元的奖金被抽出
2元的奖金被抽出
*/
//分析:
//1.定义数组表示奖池
int[] arr = {2, 588, 888, 1000, 10000};
//2.定义新数组用于存储抽奖的结果
int[] newArr = new int[arr.length];
//3.抽奖
Random r = new Random();
//因为有5个奖项,所以这里要循环5次
for (int i = 0; i < 5; ) {
//获取随机索引
int randomIndex = r.nextInt(arr.length);
//获取奖项
int prize = arr[randomIndex];
//判断当前的奖项是否存在,如果存在则重新抽取,如果不存在,就表示是有效奖项
boolean flag = contains(newArr, prize);
if(!flag){
//把当前抽取到的奖项添加到newArr当中
newArr[i] = prize;
//添加完毕之后,移动索引
i++;
}
}
//4.遍历newArr
for (int i = 0; i < newArr.length; i++) {
System.out.println(newArr[i]);
}
}
//判断prize在数组当中是否存在
//存在:true
//不存在:false
public static boolean contains(int[] arr,int prize){
for (int i = 0; i < arr.length; i++) {
if(arr[i] == prize){
return true;
}
}
return false;
}
}
package com.itheima.test;
import java.util.Random;
public class Test10 {
public static void main(String[] args) {
/* 需求:
一个大V直播抽奖,奖品是现金红包,分别有{2, 588 , 888, 1000, 10000}五个奖金。
请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。
打印效果如下:(随机顺序,不一定是下面的顺序)
888元的奖金被抽出
588元的奖金被抽出
10000元的奖金被抽出
1000元的奖金被抽出
2元的奖金被抽出
*/
//1.把奖池里面的所有奖项打乱顺序
int[] arr = {2, 588, 888, 1000, 10000};
Random r = new Random();
for (int i = 0; i < arr.length; i++) {
//获取随机索引
int randomIndex = r.nextInt(arr.length);
//拿着i跟随机索引randomIndex上的值进行交换
int temp = arr[i];
arr[i] = arr[randomIndex];
arr[randomIndex] = temp;
}
//2.遍历奖池,从0索引开始获取每一个奖项
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
package com.itheima.test;
import java.util.Random;
import java.util.Scanner;
public class Test11 {
public static void main(String[] args) {
//1.生成中奖号码
int[] arr = createNumber(); // 123456 7
System.out.println("=======================");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("=======================");
//2.用户输入彩票号码(红球 + 蓝球)//654321
int[] userInputArr = userInputNumber();
//3.判断用户的中奖情况
//红球 蓝球
int redCount = 0;
int blueCount = 0;
//判断红球
for (int i = 0; i < userInputArr.length - 1; i++) {
int redNumber = userInputArr[i];
for (int j = 0; j < arr.length - 1; j++) {
if(redNumber == arr[j]){
redCount++;
//如果找到了,那么后面的数字就没有必要继续比较了
//跳出内循环,继续判断下一个红球号码是否中奖
break;
}
}
}
//判断蓝球
int blueNumber = userInputArr[userInputArr.length-1];
if(blueNumber == arr[arr.length - 1]){
blueCount++;
}
//根据红球的个数以及蓝球的个数来判断中奖情况
if(redCount == 6 && blueCount == 1){
System.out.println("恭喜你,中奖1000万");
}else if(redCount == 6 && blueCount == 0){
System.out.println("恭喜你,中奖500万");
}else if(redCount == 5 && blueCount == 1){
System.out.println("恭喜你,中奖3000");
}else if((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)){
System.out.println("恭喜你,中奖200");
}else if((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)){
System.out.println("恭喜你,中奖10");
}else if((redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1)|| (redCount == 0 && blueCount == 1)){
System.out.println("恭喜你,中奖5");
}else{
System.out.println("谢谢参与,谢谢惠顾");
}
}
public static int[] userInputNumber() {
//1.创建数组用于添加用户购买的彩票号码
//6个红球 1个蓝球 数组长度:7
int[] arr = new int[7];
//2.利用键盘录入让用输入
Scanner sc = new Scanner(System.in);
//让用户输入红球号码
for (int i = 0; i < 6; ) {
System.out.println("请输入第" + (i + 1) + "个红球号码");
int redNumber = sc.nextInt();
//redNumber 在1~33 唯一不重复
if (redNumber >= 1 && redNumber <= 33) {
boolean flag = contains(arr, redNumber);
if (!flag) {
//不存在
//有效的,可以添加到数组当中
arr[i] = redNumber;
i++;
} else {
//存在
System.out.println("当前红球号码已经存在,请重新输入");
}
} else {
System.out.println("当前红球号码超出范围");
}
}
//让用户输入篮球号码
System.out.println("请输入篮球号码");
//1~16
while (true) {
int blueNumber = sc.nextInt();
if (blueNumber >= 1 && blueNumber <= 16) {
arr[arr.length - 1] = blueNumber;
break;
} else {
System.out.println("当前篮球号码超出范围");
}
}
return arr;
}
public static int[] createNumber() {
//1.创建数组用于添加中奖号码
//6个红球 1个蓝球 数组长度:7
int[] arr = new int[7];
//2.随机生成号码并添加到数组当中
//红球:不能重复的 1 2 3 4 5 6
//蓝球:可以跟红球号码重复 5
//生成红球号码并添加到数组当中
Random r = new Random();
for (int i = 0; i < 6; ) {
//获取红球号码
int redNumber = r.nextInt(33) + 1;
boolean flag = contains(arr, redNumber);
if (!flag) {
//把红球号码添加到数组当中
arr[i] = redNumber;
i++;
}
}
//生成蓝球号码并添加到数组当中
int blueNumber = r.nextInt(16) + 1;
arr[arr.length - 1] = blueNumber;
return arr;
}
//用于判断数组在数组中是否存在
public static boolean contains(int[] arr, int number) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == number) {
return true;
}
}
return false;
}
}
System.arraycopy(a, 2, b, 1, 3);
从a的第二个元素开始(从1开始),复制长度为3个元素到从b的第1个位置之后(从0开始)
按鼠标滚轮快塑选取
alt +fn +insert生成构造函数
两下tab一次回车 默认构造 全选全参构造
第4个函数
使用ptg to javabean
按shift+下选择,tab跳过选择
ctrl+p方法参数
ctrl+B查看
.forr倒着遍历
package com.itheima.stringdemo;
import java.util.Scanner;
public class StringDemo9 {
public static void main(String[] args) {
//1.键盘录入一个金额
Scanner sc = new Scanner(System.in);
int money;
while (true) {
System.out.println("请录入一个金额");
money = sc.nextInt();
if (money >= 0 && money <= 9999999) {
break;
} else {
System.out.println("金额无效");
}
}
//定义一个变量用来表示钱的大写
String moneyStr = "";
//2.得到money里面的每一位数字,再转成中文
while (true) {//2135
//从右往左获取数据,因为右侧是数据的个位
int ge = money % 10;
String capitalNumber = getCapitalNumber(ge);
//把转换之后的大写拼接到moneyStr当中
moneyStr = capitalNumber + moneyStr;
//第一次循环 : "伍" + "" = "伍"
//第二次循环 : "叁" + "伍" = "叁伍"
//去掉刚刚获取的数据
money = money / 10;
//如果数字上的每一位全部获取到了,那么money记录的就是0,此时循环结束
if (money == 0) {
break;
}
}
//3.在前面补0,补齐7位
int count = 7 - moneyStr.length();
for (int i = 0; i < count; i++) {
moneyStr = "零" + moneyStr;
}
System.out.println(moneyStr);//零零零贰壹叁伍
//4.插入单位
//定义一个数组表示单位
String[] arr = {"佰","拾","万","仟","佰","拾","元"};
// 零 零 零 贰 壹 叁 伍
//遍历moneyStr,依次得到 零 零 零 贰 壹 叁 伍
//然后把arr的单位插入进去
String result = "";
for (int i = 0; i < moneyStr.length(); i++) {
char c = moneyStr.charAt(i);
//把大写数字和单位拼接到result当中
result = result + c + arr[i];
}
//5.打印最终结果
System.out.println(result);
}
//定义一个方法把数字变成大写的中文
//1 -- 壹
public static String getCapitalNumber(int number) {
//定义数组,让数字跟大写的中文产生一个对应关系
String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
//返回结果
return arr[number];
}
}
package com.itheima.stringbuilderdemo;
public class StringBuilderDemo7 {
public static void main(String[] args) {
//1.定义数组
int[] arr = {1,2,3};
//2.调用方法把数组变成字符串
String str = arrToString(arr);
System.out.println(str);
}
public static String arrToString(int[] arr){
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if(i == arr.length - 1){
sb.append(arr[i]);
}else{
sb.append(arr[i]).append(", ");
}
}
sb.append("]");
return sb.toString();
}
}
ctrl +L+T 给一段代码加循环
package com.itheima.test;
import java.util.Scanner;
public class Test1Case2 {
public static void main(String[] args) {
/* 键盘录入一个字符串,
要求1:长度为小于等于9
要求2:只能是数字
将内容变成罗马数字
下面是阿拉伯数字跟罗马数字的对比关系:
Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9
注意点:
罗马数字里面是没有0的
如果键盘录入的数字包含0,可以变成""(长度为0的字符串)*/
//1.键盘录入一个字符串
//书写Scanner的代码
Scanner sc = new Scanner(System.in);
String str;
while (true) {
System.out.println("请输入一个字符串");
str = sc.next();
//2.校验字符串是否满足规则
boolean flag = checkStr(str);
if (flag) {
break;
} else {
System.out.println("当前的字符串不符合规则,请重新输入");
continue;
}
}
//将内容变成罗马数字
//下面是阿拉伯数字跟罗马数字的对比关系:
//Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9
//查表法:数字跟数据产生一个对应关系
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
String s = changeLuoMa(c);
sb.append(s);
}
System.out.println(sb);
}
//利用switch进行匹配
public static String changeLuoMa(char number) {
String str = switch (number) {
case '0' -> "";
case '1' -> "Ⅰ";
case '2' -> "Ⅱ";
case '3' -> "Ⅲ";
case '4' -> "Ⅳ";
case '5' -> "Ⅴ";
case '6' -> "Ⅵ";
case '7' -> "Ⅶ";
case '8' -> "Ⅷ";
case '9' -> "Ⅸ";
default -> str = "";
};
return str;
}
public static boolean checkStr(String str) {//123456
//要求1:长度为小于等于9
if (str.length() > 9) {
return false;
}
//要求2:只能是数字
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);//0~9
if (c < '0' || c > '9') {
return false;
}
}
//只有当字符串里面所有的字符全都判断完毕了,我才能认为当前的字符串是符合规则
return true;
}
}
package com.itheima.test;
import java.util.Scanner;
public class Test1Case1 {
public static void main(String[] args) {
/* 键盘录入一个字符串,
要求1:长度为小于等于9
要求2:只能是数字
将内容变成罗马数字
下面是阿拉伯数字跟罗马数字的对比关系:
Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9
注意点:
罗马数字里面是没有0的
如果键盘录入的数字包含0,可以变成""(长度为0的字符串)*/
//1.键盘录入一个字符串
//书写Scanner的代码
Scanner sc = new Scanner(System.in);
String str;
while (true) {
System.out.println("请输入一个字符串");
str = sc.next();
//2.校验字符串是否满足规则
boolean flag = checkStr(str);
if (flag) {
break;
} else {
System.out.println("当前的字符串不符合规则,请重新输入");
continue;
}
}
//将内容变成罗马数字
//下面是阿拉伯数字跟罗马数字的对比关系:
//Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9
//查表法:数字跟数据产生一个对应关系
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
int number = c - 48; // 1 2 3 4 5
String s = changeLuoMa(number);
sb.append(s);
}
System.out.println(sb);
}
public static String changeLuoMa(int number) {
//定义一个数组,让索引跟罗马数字产生一个对应关系
String[] arr = {"", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"};
return arr[number];
}
public static boolean checkStr(String str) {//123456
//要求1:长度为小于等于9
if (str.length() > 9) {
return false;
}
//要求2:只能是数字
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);//0~9
if (c < '0' || c > '9') {
return false;
}
}
//只有当字符串里面所有的字符全都判断完毕了,我才能认为当前的字符串是符合规则
return true;
}
}
字符反转:
package com.itheima.test;
public class Test2Case1 {
public static void main(String[] args) {
/* 给定两个字符串, A和B。
A的旋转操作就是将A 最左边的字符移动到最右边。
例如, 若A = 'abcde',在移动一次之后结果就是'bcdea'
如果在若干次调整操作之后,A能变成B,那么返回True。
如果不能匹配成功,则返回false*/
//1.定义两个字符串
String strA = "abcde";
String strB = "ABC";
//2.调用方法进行比较
boolean result = check(strA, strB);
//3.输出
System.out.println(result);
}
public static boolean check(String strA, String strB) {
for (int i = 0; i < strA.length(); i++) {
strA = rotate(strA);
if(strA.equals(strB)){
return true;
}
}
//所有的情况都比较完毕了,还不一样那么直接返回false
return false;
}
//作用:旋转字符串,把左侧的字符移动到右侧去
//形参:旋转前的字符串
//返回值:旋转后的字符串
public static String rotate(String str) {
//套路:
//如果我们看到要修改字符串的内容
//可以有两个办法:
//1.用subString进行截取,把左边的字符截取出来拼接到右侧去
//2.可以把字符串先变成一个字符数组,然后调整字符数组里面数据,最后再把字符数组变成字符串。
//截取思路
//获取最左侧那个字符
char first = str.charAt(0);
//获取剩余的字符
String end = str.substring(1);
return end + first;
}
}
package com.itheima.test;
public class Test2Case2 {
public static void main(String[] args) {
/* 给定两个字符串, A和B。
A的旋转操作就是将A 最左边的字符移动到最右边。
例如, 若A = 'abcde',在移动一次之后结果就是'bcdea'
如果在若干次调整操作之后,A能变成B,那么返回True。
如果不能匹配成功,则返回false*/
//1.定义两个字符串
String strA = "abcde";
String strB = "ABC";
//2.调用方法进行比较
boolean result = check(strA, strB);
//3.输出
System.out.println(result);
}
public static boolean check(String strA, String strB) {
for (int i = 0; i < strA.length(); i++) {
strA = rotate(strA);
if (strA.equals(strB)) {
return true;
}
}
//所有的情况都比较完毕了,还不一样那么直接返回false
return false;
}
//作用:旋转字符串,把左侧的字符移动到右侧去
//形参:旋转前的字符串
//返回值:旋转后的字符串
public static String rotate(String str) {
//套路:
//如果我们看到要修改字符串的内容
//可以有两个办法:
//1.用subString进行截取,把左边的字符截取出来拼接到右侧去
//2.可以把字符串先变成一个字符数组,然后调整字符数组里面数据,最后再把字符数组变成字符串。
//可以把字符串先变成一个字符数组,然后调整字符数组里面数据,最后再把字符数组变成字符串。
//"ABC" ['A','B','C'] ['B','C','A'] new String(字符数组);
char[] arr = str.toCharArray();
//拿到0索引上的字符
char first = arr[0];
//把剩余的字符依次往前挪一个位置
for (int i = 1; i < arr.length; i++) {
arr[i - 1] = arr[i];
}
//把原来0索引上的字符放到最后一个索引
arr[arr.length - 1] = first;
//利用字符数组创建一个字符串对象
String result = new String(arr);
return result;
}
}
package com.itheima.listdemo;
import java.util.ArrayList;
/*
boolean add(E e) 添加
boolean remove(E e) 删除
E remove(int index)
E set(int index,E e) 修改
E get(int index) 查询
int size() 获取长度
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
//1.创建一个集合
ArrayList<String> list = new ArrayList<>();
//2.添加元素
list.add("aaa");
list.add("aaa");
list.add("bbb");
list.add("ccc");
//3.删除元素
/* boolean result1 = list.remove("aaa");
System.out.println(result1);
boolean result2 = list.remove("ddd");
System.out.println(result2);
String str = list.remove(2);
System.out.println(str);*/
//修改元素
/*String result = list.set(1, "ddd");
System.out.println(result);*/
//查询元素
/* String s = list.get(0);
System.out.println(s);*/
//遍历
for (int i = 0; i < list.size(); i++) {
//i 索引
//list.get(i) 元素
String str = list.get(i);
System.out.println(str);
}
}
}
学生对象:
package com.itheima.test;
public class Student {
//1.私有化成员变量
//2.空参构造方法
//3.带全部参数的构造方法
//4.get/set方法
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.itheima.test;
import java.util.ArrayList;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
//1.创建集合
ArrayList<Student> list = new ArrayList<>();
//长度为0
//2.键盘录入学生的信息并添加到集合当中
Scanner sc = new Scanner(System.in);
Student s = new Student();
for (int i = 0; i < 3; i++) {
System.out.println("请输入学生的姓名");
String name = sc.next();
System.out.println("请输入学生的年龄");
int age = sc.nextInt();
//把name和age赋值给学生对象
s.setName(name);
s.setAge(age);
//把学生对象添加到集合当中
list.add(s);
}
//3.遍历
for (int i = 0; i < list.size(); i++) {
//i 索引 list.get(i) 元素/学生对象
Student stu = list.get(i);
System.out.println(stu.getName() + ", " + stu.getAge());
}
}
}
ctrl +p 提示
查找用户:
package OOPtext;
import java.util.ArrayList;
public class Text6 {
public static void main(String[] args) {
//1.创建集合
ArrayList<User> list = new ArrayList<>();
//2.创建三个用户对象
User u1 = new User("heima001","zhangsan","123456");
User u2 = new User("heima002","lisi","12345678");
User u3 = new User("heima003","wangwu","1234qwer");
//3.把用户对象添加到集合当中
list.add(u1);
list.add(u2);
list.add(u3);
//4.调用方法查看id是否存在
boolean flag = contains(list, "heima0010");
//5.打印结果
System.out.println(flag);
}
//1.我要干嘛? 根据id查找用户
//2.我干这件事需要什么才能完成? list id
//3.调用处是否需要使用方法的结果? 返回
public static boolean contains(ArrayList<User> list, String id){
/* for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
String uid = u.getId();
if(uid.equals(id)){
//如果找到了直接返回true
return true;
}
}
//当循环结束表示集合里面所有的元素都已经比较完毕,还没有一样的,那么返回false就可以了
return false;*/
return getIndex(list,id) >= 0;
}
public static int getIndex(ArrayList<User> list, String id) {
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
String uid = u.getId();
if(uid.equals(id)){
return i;
}
}
return -1;
}
}
package OOPtext;
public class User {
private String id;
private String username;
private String password;
public User() {
}
public User(String id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
手机查询:
package com.itheima.test;
public class Phone {
//Phone属性:品牌,价格。
private String brand;
private int price;
public Phone() {
}
public Phone(String brand, int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
package com.itheima.test;
/*
需求:
定义Javabean类:Phone
Phone属性:品牌,价格。
main方法中定义一个集合,存入三个手机对象。
分别为:小米,1000。苹果,8000。锤子 2999。
定义一个方法,将价格低于3000的手机信息返回。
*/
import java.util.ArrayList;
public class Test8 {
public static void main(String[] args) {
//1.创建集合对象
ArrayList<Phone> list = new ArrayList<>();
//2.创建手机的对象
Phone p1 = new Phone("小米",1000);
Phone p2 = new Phone("苹果",8000);
Phone p3 = new Phone("锤子",2999);
//3.添加数据
list.add(p1);
list.add(p2);
list.add(p3);
//4.调用方法
ArrayList<Phone> phoneInfoList = getPhoneInfo(list);
//5.遍历集合
for (int i = 0; i < phoneInfoList.size(); i++) {
Phone phone = phoneInfoList.get(i);
System.out.println(phone.getBrand() + ", " + phone.getPrice());
}
}
//1.我要干嘛? 查询手机信息
//2.我干这件事情,需要什么才能完成? 集合
//3.我干完了,方法的调用处是否需要继续使用结果? 返回
//技巧:
//如果我们要返回多个数据,可以把这些数据先放到一个容器当中,再把容器返回
//集合 数组
public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> list){
//定义一个集合用于存储价格低于3000的手机对象
ArrayList<Phone> resultList = new ArrayList<>();
//遍历集合
for (int i = 0; i < list.size(); i++) {
Phone p = list.get(i);
int price = p.getPrice();
//如果当前手机的价格低于3000,那么就把手机对象添加到resultList中
if(price < 3000){
resultList.add(p);
}
}
//返回resultList
return resultList;
}
}