容器操作:增删改查
package com.lzk.test;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(1);//可以添加任意元素
System.out.println(list);
ArrayList<String> list1 = new ArrayList();
list1.add("hello");//只能添加String类型元素
System.out.println(list1);
//往索引为1的位置添加元素
list.add(1, "world");
System.out.println(list);
//获取索引为1的元素
String str = (String) list.get(1);
System.out.println(str);
//删除索引为1的元素
list.remove(1);
System.out.println(list);
//获取list的大小
int size = list.size();
System.out.println(size);
//删除指定的元素,并返回是否删除成功,有多个相同元素时,默认删除第一次出现的元素
boolean isRemove = list.remove("hello");
System.out.println(isRemove);
//修改某个位置出的元素
list.set(0, "hello world");
System.out.println(list);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
案例:购物车中存在以下数据:java。樱桃,大樱桃,超级大樱桃,小樱桃,樱桃小丸子,Java从入门到入土。请删除包含樱桃的物品。
重点是:因为删除了一个元素,所以索引要减一
package com.lzk.test;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("java");
list.add("樱桃");
list.add("大樱桃");
list.add("超级大樱桃");
list.add("小樱桃");
list.add("樱桃小丸子");
list.add("Java从入门到入土");
System.out.println(list);
//删除带有"樱桃"的元素
// for (int i = 0; i < list.size(); i++) {
// String str = (String) list.get(i);
// if (str.contains("樱桃")) {
// list.remove(i);
// i--;// 因为删除了一个元素,所以索引要减一
//
// }
//方法二,倒着遍历,删除带有"樱桃"的元素
for (int i = list.size() - 1; i >= 0; i--) {
String str = (String) list.get(i);
if (str.contains("樱桃")) {
list.remove(i);
}
}System.out.println(list);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
案例2:饭店菜单操作系统
test 主函数
构造器
package com.lzk.test;
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;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
操作函数
package com.lzk.test;
import java.util.ArrayList;
import java.util.Scanner;
public class FoodOperator {
private ArrayList<Food> foodlist = new ArrayList<>();//定义一个ArrayList对象,用于存储食物信息
public void addFood(String food) {//添加食物信息
Food f = new Food();
Scanner sc = new Scanner(System.in);
System.out.println("请输入食物名称:");
f.setName(sc.next());
System.out.println("请输入食物价格:");
f.setPrice(sc.nextDouble());
System.out.println("请输入食物描述:");
f.setDescription(sc.next());
foodlist.add(f);
System.out.println("添加成功!");
}
public void ShowFoodList() {//显示食物列表
if (foodlist.size() == 0) {
System.out.println("当前没有任何食物信息!");
return;
}
System.out.println("食物列表:");
for (Food f : foodlist) {
System.out.println("名称:" + f.getName());
System.out.println("价格:" + f.getPrice());
System.out.println("描述:" + f.getDescription()); // 打印食物信息
System.out.println("-------------");
}
}
public void start() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("");
System.out.println("欢迎使用食物管理系统");
System.out.println("1.添加食物信息");
System.out.println("2.显示食物列表");
System.out.println("3.退出");
System.out.println("请输入您的选择:");
int choice = sc.nextInt();
switch (choice) {
case 1:
addFood("");
break;
case 2:
ShowFoodList();
break;
case 3:
System.out.println("退出成功!");
return;//使用return结束start方法
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.