ArrayList
常用方法
来自黑马程序员学习视频
删除ArrayList中包含指定文字的数据项
String ele = list.get(int n);//获取list中n索引的数据
代码
package ArrayList;
import java.util.ArrayList;
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-07
*/
public class Demo2 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("白菜");
list.add("Java");
list.add("油麦菜");
list.add("油麦菜");
list.add("芹菜");
list.add("黑豆");
list.add("红豆");
list.add("绿豆");
System.out.println(list);
// list.remove(0);
//删除包含“菜”的数据项
//方法一:要i--,自己手写一边就知道什么意思
for (int i = 0; i < list.size(); i++) {
String ele = list.get(i);//这句很重要
if (ele.contains("菜")) {
list.remove(i);
i--;
}
}
System.out.println(list);
//方法二:倒序遍历就不用i--
for (int i = list.size()-1; i > 0; i--) {
String ele = list.get(i);
if(ele.contains("豆")){
list.remove(i);
}
}
System.out.println(list);
}
}
运行结果
案例 :上菜
完成菜品上架及菜品查看的功能
代码
Food.java
package ArrayList;
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-07
*/
public class Food {
private String name;
private double price;
private String description;
public Food() {
}
public Food(String name, double price, String description) {
this.name = name;
this.price = price;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Food{" +
"name='" + name + '\'' +
", price=" + price +
", description='" + description + '\'' +
'}';
}
}
FoodOperator.java
package ArrayList;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-07
*/
public class FoodOperator {
Scanner sc = new Scanner(System.in);
Food foodList = new Food();
ArrayList<Food> foodOperatorList = new ArrayList<>();
public void addFood() {
//ArrayList<String> foodList = new ArrayList<>();
System.out.println("请输入菜品名称:");
//foodList.name = sc.next(); 因为name是private,所以只能用set方法写
foodList.setName(sc.next());
System.out.println("请输入菜品价格:");
foodList.setPrice(sc.nextDouble());
System.out.println("请输入菜品描述:");
foodList.setDescription(sc.next());
foodOperatorList.add(foodList);
// System.out.println(foodOperatorList);
}
public void showFoods(){
System.out.println(foodOperatorList);
}
public void start(){
while(true) {
System.out.println("请选择功能:");
System.out.println("1.添加菜品");
System.out.println("2.展示菜品");
System.out.println("3.退出程序");
switch(sc.nextInt()){
case 1:
addFood();
break;
case 2:
if(foodOperatorList.size()==0){
System.out.println("列表现在还没有菜品,快去添加吧~");
}else {
showFoods();
}
break;
case 3:
System.out.println("退出程序");
return;
default:
System.out.println("选择功能有误,请重新选择~~");
break;
}
}
}
}
Test.java
package ArrayList;
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-07
* 完成上菜以及查看功能
*/
public class Test {
public static void main(String[] args) {
FoodOperator operator = new FoodOperator();
operator.start();
}
}
运行结果
反思
主要就是 food 和foodOperator关系的问题,要想清楚