做网站的技术关键,wordpress资讯cms主题,泰安网站建设招聘,网站开发的开题报告引言1.集合
ArrayList 集合和数组的优势对比#xff1a;
长度可变
添加数据的时候不需要考虑索引#xff0c;默认将数据添加到末尾 package com.itheima;import java.util.ArrayList;/*public boolean add(要添加的元素) | 将指定的元素追加到此集合的末尾 |
| p…1.集合
ArrayList 集合和数组的优势对比
长度可变
添加数据的时候不需要考虑索引默认将数据添加到末尾 package com.itheima;import java.util.ArrayList;/*public boolean add(要添加的元素) | 将指定的元素追加到此集合的末尾 |
| public boolean remove(要删除的元素) | 删除指定元素,返回值表示是否删除成功
| public E remove(int index) | 删除指定索引处的元素返回被删除的元素
| public E set(int index,E element) | 修改指定索引处的元素返回被修改的元素
| public E get(int index) | 返回指定索引处的元素
| public int size() | 返回集合中的元素的个数*/
public class ArrayListDemo2 {public static void main(String[] args) {//1.创建一个集合对象ArrayListString list new ArrayListString();//2.添加元素list.add(aaa);list.add(bbb);list.add(ccc);list.add(ddd);//3.删除元素boolean result list.remove(aaa);System.out.println(list);//删除失败返回flast//根据索引删除元素返回被删除的元素String str list.remove(2);System.out.println(str);System.out.println(list);//4.修该元素String str2 list.set(0, aaa);System.out.println(str2);//返回被覆盖的元素System.out.println(list);//5.查寻元素String str3 list.get(0);System.out.println(str3);//返回指定索引处的元素System.out.println(list);//6.遍历集合for (int i 0; i list.size(); i) {//list.get(i)元素System.out.println(list.get(i));}}
}package com.itheima;
import java.util.ArrayList;
import java.util.List;
//定义一个方法,讲价格低于3000的手机信息打印出啦
public class PhoneTest {public static void main(String[] args) {//1.创建集合ArrayListPhone list new ArrayListPhone();//2.创建对象Phone p1 new Phone(苹果,1000);Phone p2 new Phone(小米,2200);Phone p3 new Phone(锤子,3200);//3.添加数据list.add(p1);list.add(p2);list.add(p3);//4.调用方法ArrayListPhone phoneInfolist getPhoneInfo(list);//5.遍历集合for (int i 0; i phoneInfolist.size(); i) {Phone p phoneInfolist.get(i);System.out.println(p.getBrand() p.getPrice());}}//如果说我们要返回多个数据,可以把这些数据先放到一个容器当中,再把容器返回//集合,数组public static ArrayListPhone getPhoneInfo( ArrayListPhone list ) {ArrayListPhone resultlistnew ArrayList();for (int i 0; i list.size(); i) {Phone p list.get(i);int price p.getPrice();if(price3000){resultlist.add(p);//手机对象p存再集合resultlist里}}return resultlist;}
}package com.itheima;
import java.util.ArrayList;public class ArrayListDemo7Case {public static void main(String[] args) {//1.创建集合ArrayListUser list new ArrayListUser();//2.创建三个用户对象User u1new User(001,zhngsan,123);User u2new User(002,li,456);User u3new User(003,zhngsan,789);//3.添加元素list.add(u1);list.add(u2);list.add(u3);//4.调用方法看id是否存在int flag contains(list, 002);if(flag0){User u list.get(flag);System.out.println(u.getId());}else{System.out.println(不存在);}}public static int contains(ArrayListUser list,String id){for (int i 0; i list.size(); i) {if(list.get(i).getId().equals(id)){return i;}}return -1;}
}package com.itheima;import java.util.ArrayList;
import java.util.Scanner;public class ArrayListDemo6 {public static void main(String[] args) {//1.创建集合ArrayListStudent listnew ArrayList();//长度为0//2.键盘录入学生的信息并添加到集合当中Scanner scnew Scanner(System.in);for(int i0;i3;i){Student snew Student();System.out.println(请输入学生姓名);String namesc.next();System.out.println(请输入学生的年龄);int agesc.nextInt();//把name和age赋值给学生对象s.setName(name);s.setAge(age);//把学生对象添加在集合中list.add(s);}//遍历集合for (int i 0; i list.size(); i) {Student stulist.get(i);System.out.println(stu.getName() stu.getAge());}}
}/如果说我们要返回多个数据,可以把这些数据先放到一个容器当中,再把容器返回
//集合,数组
package com.itheima;
import java.util.ArrayList;
import java.util.List;
//定义一个方法,讲价格低于3000的手机信息打印出啦
public class PhoneTest {public static void main(String[] args) {//1.创建集合ArrayListPhone list new ArrayListPhone();//2.创建对象Phone p1 new Phone(苹果,1000);Phone p2 new Phone(小米,2200);Phone p3 new Phone(锤子,3200);//3.添加数据list.add(p1);list.add(p2);list.add(p3);//4.调用方法ArrayListPhone phoneInfolist getPhoneInfo(list);//5.遍历集合for (int i 0; i phoneInfolist.size(); i) {Phone p phoneInfolist.get(i);System.out.println(p.getBrand() p.getPrice());}}//如果说我们要返回多个数据,可以把这些数据先放到一个容器当中,再把容器返回//集合,数组public static ArrayListPhone getPhoneInfo( ArrayListPhone list ) {ArrayListPhone resultlistnew ArrayList();for (int i 0; i list.size(); i) {Phone p list.get(i);int price p.getPrice();if(price3000){//如果当前手机的价格低于3000,那么就把手机对象添加在resultlist当中resultlist.add(p);//手机对象p存再集合resultlist里}}return resultlist;}
} 文章转载自: http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.zrjzc.cn.gov.cn.zrjzc.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.wdskl.cn.gov.cn.wdskl.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.yghlr.cn.gov.cn.yghlr.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.gwzfj.cn.gov.cn.gwzfj.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.nzhzt.cn.gov.cn.nzhzt.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.wwdlg.cn.gov.cn.wwdlg.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.lftpl.cn.gov.cn.lftpl.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.qkqhr.cn.gov.cn.qkqhr.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn