JAVA API:ArrayList应用案例

package com.itheima.ArrayList;

import java.util.ArrayList;

public class Demo2 {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<>();
        list.add("Java入门");
        list.add("宁夏枸杞");
        list.add("黑枸杞");
        list.add("人字拖");
        list.add("特级枸杞");
        list.add("枸杞子");
        System.out.println(list);
//        for (int i = 0; i < list.size(); i++) {
//            String ele= list.get(i);
//            if(ele.contains("枸杞")){
//                list.remove(i);
//                i--;
//            }
//        }
//        System.out.println(list);

        //方式2:
        for (int i=list.size()-1;i>=0;i--) {
            String ele= list.get(i);
            if(ele.contains("枸杞")){
                list.remove(i);
            }
        }
        System.out.println(list);

    }
}

 

 

 

 

综合案例:

package com.itheima.ArrayList;

public class Food {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Food(String name, double price, String desc) {
        this.name = name;
        this.price = price;
        this.desc = desc;
    }

    public Food() {
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    private double price;
    private String desc;

}
package com.itheima.ArrayList;

import java.util.ArrayList;
import java.util.Scanner;

public class FoodOperater {
    private ArrayList<Food> foods=new ArrayList<>();
    public void addFood(){
        Food food=new Food();
        Scanner scanner=new Scanner(System.in);
        System.out.println("请您输入该菜品名称");
        String name=scanner.next();
        food.setName(name);



        System.out.println("请您输入该菜品价格");
        double price =scanner.nextInt();
        food.setPrice(price);

        System.out.println("请您输入该菜品描述");
        String desc =scanner.next();
        food.setDesc(desc);
        foods.add(food);
    }
    public void showAllFoods(){
        if(foods.size()==0){
            System.out.println("请上架商品");
        }
        for (int i = 0; i < foods.size(); i++) {
            Food food=new Food();
            food=foods.get(i);
            System.out.println(food.getName());
            System.out.println(food.getPrice());
            System.out.println(food.getDesc());
            System.out.println("------------------------------------");
        }
    }
    public void start(){
        while (true) {
            System.out.println("请选择功能:");
            System.out.println("1.上架商品");
            System.out.println("2.展示商品");
            System.out.println("3.退出");
            Scanner scanner=new Scanner(System.in);
            System.out.println("请选择您的操作:");
            int command= scanner.nextInt();
            switch (command){
                case 1:
                    addFood();
                    break;
                case 2:
                    showAllFoods();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("输入有误");
            }
        }
    }


}
package com.itheima.ArrayList;

import java.util.ArrayList;

public class Demo3 {
    public static void main(String[] args) {
        ArrayList<Food> foods=new ArrayList<>();
//        foods.add(new Food("", ,""))
        FoodOperater foodOperater=new FoodOperater();
        foodOperater.start();
    }
}

 

posted @ 2024-03-06 22:13  小彭先森  阅读(14)  评论(0编辑  收藏  举报