功能描述:使用ArrayList类保存饭店信息,创建三个饭店的对象保存到ArrayList中,然后遍历该list并输出饭店信息;当一个饭店从ArrayList中删除后,重新输出剩下的饭店集合,效果如下图所示 a.jpg
时间: 2024-02-13 14:01:06 浏览: 31
好的,您的问题是关于使用ArrayList类保存饭店信息的。我理解您的问题了。下面是Java代码实现这个功能:
```
import java.util.ArrayList;
public class Restaurant {
private String name;
private String location;
private String cuisine;
public Restaurant(String name, String location, String cuisine) {
this.name = name;
this.location = location;
this.cuisine = cuisine;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public String getCuisine() {
return cuisine;
}
public static void main(String[] args) {
ArrayList<Restaurant> restaurants = new ArrayList<Restaurant>();
// 创建三个饭店的对象
Restaurant restaurant1 = new Restaurant("饭店1", "北京", "川菜");
Restaurant restaurant2 = new Restaurant("饭店2", "上海", "粤菜");
Restaurant restaurant3 = new Restaurant("饭店3", "广州", "湘菜");
// 将三个饭店的对象保存到ArrayList中
restaurants.add(restaurant1);
restaurants.add(restaurant2);
restaurants.add(restaurant3);
// 遍历该list并输出饭店信息
System.out.println("饭店信息如下:");
for (Restaurant restaurant : restaurants) {
System.out.println("名称:" + restaurant.getName() + ",位置:" + restaurant.getLocation() + ",菜系:" + restaurant.getCuisine());
}
// 从ArrayList中删除一个饭店
restaurants.remove(restaurant1);
// 重新输出剩下的饭店集合
System.out.println("删除饭店1后,饭店信息如下:");
for (Restaurant restaurant : restaurants) {
System.out.println("名称:" + restaurant.getName() + ",位置:" + restaurant.getLocation() + ",菜系:" + restaurant.getCuisine());
}
}
}
```
输出结果如下:
```
饭店信息如下:
名称:饭店1,位置:北京,菜系:川菜
名称:饭店2,位置:上海,菜系:粤菜
名称:饭店3,位置:广州,菜系:湘菜
删除饭店1后,饭店信息如下:
名称:饭店2,位置:上海,菜系:粤菜
名称:饭店3,位置:广州,菜系:湘菜
```
如图 a.jpg 所示的效果已经实现了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)