【Java】Java中的集合类
来源:互联网 发布:剑网三秀太捏脸数据 编辑:程序博客网 时间:2024/05/29 16:33
摘要:本文讲解了Java入门中的集合类,包括:List
、Map
、Set
、Queue
集合类的分类
List
结构集合类:ArrayList
LinkedList
Vector
Stack
Map
结构集合类:HashMap
HashTable
Set
结构集合类:HashSet
TreeSet
Queue
结构集合类和Queue
接口
用法实例
ArrayList
的使用
//基本用法ArrayList aa1=new ArrayList();//取大小aa1.size();//添加对象:XS xs1=new Xs("wukong",15,90);aa1.add(xs1);//ArrayList的遍历//基本思路:for循环遍历输出//用法:将类对象强转换后输出 XS show=(XS)aa1.get(i);show.getName()//ArrayList的查询及删除//删除:aa1.remove(i);//在任意位置插入对象aa1.add(i,xs1);//定义类对象class XS{private String name;private double id;private int grade;XS(String name,double id,int grade){this.name=name;this.id=id;this.grade=grade;}public String getName(){ return name;}public int getGrade(){ return grade;}public double getId(){ return id;}}
实例应用
//实例:简单的食品管理系统//代码:package cha04;import java.util.*;import java.io.*;public class L4_2 {public static void main(String[] args) throws Exception{Mag mag=new Mag();BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));while(true){System.out.println("请按提示选择以下功能");System.out.println("添加食品请按1");System.out.println("查找食品信息请按2");System.out.println("修改食品价格请按3");System.out.println("删除食品请按4");System.out.println("退出请按0");String str=sc.readLine();if(str.equals("1")){System.out.print("请输入食品编号: ");int num=Integer.parseInt(sc.readLine());System.out.print("请输入食品名称: ");String nam=sc.readLine();System.out.print("请输入食品价格: ");double pri=Double.parseDouble(sc.readLine());Sp sp=new Sp(nam,num,pri);mag.addSp(sp);}else if(str.equals("2")){System.out.print("请输入食品编号: ");int num=Integer.parseInt(sc.readLine());mag.spxx(num);}else if(str.equals("3")){System.out.print("请输入食品编号: ");int num=Integer.parseInt(sc.readLine());System.out.println("请输入新的价格: ");double pr=Double.parseDouble(sc.readLine());mag.xgjg(num, pr);}else if(str.equals("4")){System.out.print("请输入食品编号: ");int num=Integer.parseInt(sc.readLine());mag.delsp(num);}else if(str.equals("0")){System.out.println("感谢您的使用,再见!");System.exit(0);}else{System.out.println("输入有误!");}}}}//定义食品类class Sp{private String na; private int id; private double pr; Sp(String na,int id,double pr) { this.na=na; this.id=id; this.pr=pr; } public String getNa() {return na;} public void setNa(String nam) {this.na=nam;} public int getId() {return id;} public void setId(int id) {this.id=id;} public double getPr() {return pr;} public void setPr(double pr) {this.pr=pr;}}//食品管理类class Mag{ private ArrayList aa=null;Mag(){aa=new ArrayList();}public void addSp(Sp sp){aa.add(sp);System.out.println("添加食品成功!");}public void spxx(int id){int i;for(i=0;i{Sp sp=(Sp)aa.get(i);if(sp.getId()==id){System.out.println("食品信息为:");System.out.println("Id: "+sp.getId());System.out.println("Name: "+sp.getNa());System.out.println("Price: "+sp.getPr());}break;}if(i==aa.size()){System.out.println("输入错误!");}}public void xgjg(int id,double pr){int i;for(i=0;i{Sp sp=(Sp)aa.get(i);if(sp.getId()==id){sp.setPr(pr);System.out.println("食品价格修改成功!");}break;}if(i==aa.size()){System.out.println("没有找到相应的食品!");}}public void delsp(int bh){int i;if(aa.size()==0){System.out.println("对不起,仓库中已没有任何食品!");}for(i=0;i{Sp sp=(Sp)aa.get(i);if(sp.getId()==bh){aa.remove(i);System.out.println("删除食品成功!");break;} }if((i==aa.size()) && (aa.size()!=0)){System.out.println("对不起,没有该食品!");}}}
LinkedList
使用
//基本用法LinkedList bb=new LinkedList();bb.add(sp);//区别于ArrayList:bb.addFirst(sp0);bb.addFirst(sp1);//sp0先放入,位于最底层bb.addLast(sp0);bb.addLast(sp1);//sp0先放入,但位于最顶层//数据压栈,放入有先后顺序之别,先放进后取出;//取出时:bb.getFirst();//取出第一个bb.getLast();//取出最后一个
Vector & Stack
//基本用法Vector cc=new Vector();//基本同ArrayListStack dd=new Stack();//基本同ArrayList
Map
应用HashMap
的使用
//基本用法HashMap ee=new HashMap();ee.put("001",sp1);//001代表键值,键值唯一不可重复,输入采用的是put而非add//输出不需遍历,如下:if(ee.containsKey("002")){Sp sp=(Sp)ee.get("002");}//注:for循环无法对其进行遍历(因为键值为String类型)//遍历方法:Iterator it=ee.keySet().iterator();while(it.hasNext()){ String key=it.next().toString(); Sp sp=(SP)ee.get(key);}
集合类的比较
- 键值:是为了唯一标识事物而单独添加的属性。一定不要用事物本身的属性来做键值
- List没有键值 Map有键值
ArrayList
和HashMap
都是线程异步的,所以他们的特点是效率高但安全性低Vector
和Hashtable
都是线程同步的,所以他们的特点是效率低但是安全性高。
范型和反射机制
解释:由于类型转换的错误,java虚拟机在编译时不会报错,而在运行时报异常,所以存在安全隐患,为此需要引入泛型机制处理
反射机制是将泛型固定的类的所有方法和成员全部可以显示出来,以供程序员确定编写是否有误。
ArrayList xs=new ArrayList//泛型可将类型固定//反射机制class BH{private L l;BH(L l){ this.l=l;}public void lxmc{ System.out.println(l.getClass().getName()//获取类型名称); Method []a=l.getClass().getDeclaredMethods();//获取类中所有方法;}//主函数:BH<类名> bh=new BH<类名>(对象);}
阅读全文
0 0
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类 - -
- JAVA中的集合类
- java中的集合类
- Java中的集合类
- JAVA中的集合类
- Java中的集合类
- Java中的集合类
- java 中的集合类
- JAVA中的集合类
- java中的集合类
- Java中的集合类
- java中的集合类
- java 中的集合类
- ADMM算法
- 如何用 Python 从海量文本抽取主题?
- 浏览器渲染页面原理
- 使用字节流、客户端读取图片发送至服务端保存
- 单例模式实现的几种方式
- 【Java】Java中的集合类
- 关于jdk、tomcat、maven环境变量的配置总结
- 【Linux】1目录、系统资源查询
- 跨站请求伪造防御
- C#简易计算器
- 利用set对list进行合并去重
- [hihocoder1322] 树结构判定
- 对搜索引擎(SEO)的索引有帮助一些HTML标签
- 【二十四】Visual Studio 2013 好用的插件