importjava.util.ArrayList;
importjava.util.List;
publicclassapiArrayListDemo1{
publicstaticvoidmain(String[]args){
//常用api的使用
//1.创建一个空的集合对象
ArrayListList=newArrayList<>();List.add("黑马");List.add(5);
System.out.println(List);
//2.将指定元素添加到集合末尾
List.add("程序员");
System.out.println(List);
//3.在集合中指定位置插入指定的元素
List.add(1,"HHH");
System.out.println(List);
//4.返回指定索引处的元素
System.out.println(List.get(2));
//5.返回集合中元素的个数
System.out.println(List.size());
//6.删除指定索引处的元素,返回删除元素
System.out.println(List.remove(2));
System.out.println(List);
//7.删除指定索引处的元素,返回是否删除成功
System.out.println(List.remove("HHH"));
System.out.println(List);
//8.修改指定索引处的元素,返回被修改元素
System.out.println(List.set(1,"黑马"));
System.out.println(List);
}
}
packageapi;
importjava.awt.*;
importjava.util.ArrayList;
publicclassapiArrayListDemo2{
publicstaticvoidmain(String[]args){
//遍历并删除元素:去除低于80的数
//为集合初始化
ArrayList
numList.add(88);
numList.add(73);
numList.add(98);
numList.add(66);
numList.add(89);
numList.add(99);
numList.add(88);
numList.add(94);
System.out.println(numList);
System.out.println("----------------------");
for(inti=0;i
if(numList.get(i)<=80){
Integerrm=numList.get(i);
numList.remove(rm);i--;
}
}
System.out.println(numList);
}
}
案例:1
packageapi;
importjava.util.ArrayList;
publicclassapiArrayListDemo3{
publicstaticvoidmain(String[]args){
ArrayList
shopList.add("Java入门");
shopList.add("宁夏枸杞");
shopList.add("黑枸杞");
shopList.add("人字拖");
shopList.add("枸杞子");
/*for(inti=0;i
if(shopList.get(i).contains("枸杞")){
shopList.remove(shopList.get(i));i--;//此处如果不加i--,会出现bug
}
}*/
//方法二:倒着遍历数组
for(inti=shopList.size()-1;i>=0;i--){
if(shopList.get(i).contains("枸杞")){
shopList.remove(shopList.get(i));
}
}
System.out.println(shopList);
}
}
案例2:
packageapi;
publicclassfood{
privateStringname;
privateStringdesc;
privatedoubleprice;
publicfood(){
}
publicfood(Stringname,Stringdesc,doubleprice){
this.name=name;
this.desc=desc;
this.price=price;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetDesc(){
returndesc;
}
publicvoidsetDesc(Stringdesc){
this.desc=desc;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
if(price>0)
this.price=price;
}
}
packageapi;
importjava.util.ArrayList;
importjava.util.Scanner;
publicclassfoodOperate{
privateArrayList
//上架功能
publicvoidaddFood(){
Scannersc=newScanner(System.in);
foodf=newfood();
System.out.println("请输入菜名:");
f.setName(sc.next());
System.out.println("请输入价格:");
f.setPrice(sc.nextDouble());
System.out.println("请输入描述:");
f.setDesc(sc.next());
foodList.add(f);
System.out.println("上架完成~~");
}
//浏览功能
publicvoidlookFood(){
if(foodList.size()==0){
System.out.println("您还没有上架菜品哦,快去上架~~");
}
for(inti=0;i<foodList.size();i++){
System.out.println("菜名:"+foodList.get(i).getName());
System.out.println("价格:"+foodList.get(i).getPrice());
System.out.println("描述:"+foodList.get(i).getDesc());
System.out.println("-------------------------------");
}
}
//设计功能选择页
publicvoidstart(){
Scannersc=newScanner(System.in);
System.out.println("欢迎光临!");
while(true){
System.out.println("请输入您希望办理的业务编号:");
System.out.println("1.上架菜品");
System.out.println("2.浏览菜单信息");
System.out.println("3.退出");
Stringcommand=sc.next();
switch((command)){
case"1":
addFood();
break;
case"2":
lookFood();
break;
case"3":
System.out.println("欢迎下次光临!!");
return;
default:
System.out.println("您输入的信息有误,请检查后重新输入!");
}
}
}
}
importapi.foodOperate;
publicclassapiArrayListDemo4{
publicstaticvoidmain(String[]args){
//结合面向对象编程完成案例
foodOperateFood=newfoodOperate();
Food.start();;
}
}