Java集合类学习(二)
范例1. 用List集合传递信息
import java.awt.BorderLayout; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; public class ClassInfo extends JFrame { JFrame jf=new JFrame(); JTable table; public ClassInfo() { jf.setTitle("\u7528List\u96C6\u5408\u4F20\u9012\u5B66\u751F\u4FE1\u606F"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setBounds(300, 350, 450, 250); JPanel contentPane=new JPanel(); contentPane.setBorder(new EmptyBorder(5,5,5,5)); contentPane.setLayout(new BorderLayout(0,0)); jf.setContentPane(contentPane); JScrollPane scrollPane=new JScrollPane(); contentPane.add(scrollPane,BorderLayout.CENTER); scrollPane.setViewportView(getTable()); jf.setVisible(true); } private JTable getTable() { if(table==null) { table=new JTable(); //创建表格控件 table.setRowHeight(23); //设置行高度 String[] columns= {"姓名","性别","出生日期"}; //创建列名数组 DefaultTableModel model=new DefaultTableModel(columns,0); //创建表格模型 table.setModel(model); //设置表格模型 List<String> students=getStudents(); //调用方法传递List集合对象 for(String info:students) { //遍历学生集合对象 String[] args=info.split(","); //把学生信息拆分为数组 model.addRow(args); //把学生信息添加到表格行 } } return table; } private List<String> getStudents(){ List<String> list=new ArrayList<String>(); //创建List集合对象 list.add("小李,男,1981-1-1"); //添加数据到集合对象 list.add("小陈,女,1981-1-1"); list.add("小刘,男,1981-1-1"); //添加数据到集合对象 list.add("小张,女,1981-1-1"); list.add("小董,男,1981-1-1"); //添加数据到集合对象 list.add("小王,女,1981-1-1"); return list; } public static void main(String[] args) { // TODO Auto-generated method stub new ClassInfo(); } }
执行结果显示如下图所示:
范例2. 集合类接口的实现
2.1. List接口的实现类
要使用List集合,通常情况下需要声明为List类型,然后通过List接口的实现类来对集合进行实例化。List接口的实现类常用的有ArrayList和LinkedList。
ArrayList类实现了可变数组,允许所有元素,包括null。可以根据索引位置对集合进行快速的随机访问。缺点是向指定的索引位置插入对象或者删除对象的速度较慢。
语法格式为:List<String> list=new ArrayList<String>(); 而LinkedList类,它是采用链表结构保存对象。这种结构的优点是便于向集合中插入和删除对象,经常向
集合中插入、删除对象时,使用LinkedList类实现的List集合的效率较好。但对于随机访问集合中的对象,使用LinkedList类实现List集合的效率较慢。
其语法格式是:List<String> list=new LinkedList<String>();
示例代码如下:
import java.util.ArrayList; import java.util.List; public class Gather { public static void main(String[] args) { // TODO Auto-generated method stub List list=new ArrayList(); //创建集合对象 int i=(int)(Math.random()*(list.size()-1)); //获得0~2之间的随机数 list.add("张三"); //向集合中添加元素 list.add("李四"); list.add("王二"); System.out.println("随机获取数组中的元素:"+list.get(i)); list.remove(2); //将指定索引位置的元素从集合中移除 System.out.println("将索引是2的元素从数组中移除后,数组中的元素是:"); for(int j=0;j<list.size();j++) { //循环遍历集合 System.out.print(list.get(j)+"\t"); } } }
执行其结果为:
随机获取数组中的元素:张三
将索引是2的元素从数组中移除后,数组中的元素是:
张三 李四
2.2. Set接口的实现类
要使用Set集合,通常情况下需要声明Set类型,然后通过Set接口的实现类来实例化。Set接口的实现类常用的有HashSet和TreeSet类。语法格式如下:
Set<String> set=new HashSet<String>();
Set<String> set=new TreeSet<String>();
Set集合中的对象是无序的,遍历Set集合中的结果和插入Set集合中的顺序并不相同。
示例代码如下:
import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class SetPeople { private String name; //定义职员姓名的类成员变量 private long id_card; //定义长整型的成员变量,表示职员卡号 public SetPeople(String name,long id_card) { this.name=name; this.id_card=id_card; } public long getId_card() { return id_card; } public void setId_card(long id_card) { this.id_card=id_card; } public String getName() { return name; } public void setName(String name) { this.name=name; } public static void main(String[] args) { // TODO Auto-generated method stub Set<SetPeople> hashSet=new HashSet<SetPeople>(); //创建Set集合对象 hashSet.add(new SetPeople("张三",123400001)); //向集合中添加对象 hashSet.add(new SetPeople("李四",123400002)); hashSet.add(new SetPeople("王二",123400003)); Iterator<SetPeople> it=hashSet.iterator(); //创建集合迭代器 System.out.println("集合中的元素是:"); while(it.hasNext()) { //遍历迭代器 SetPeople people=it.next(); System.out.println(people.getName()+" "+people.getId_card()); } } }
执行结果如下:
集合中的元素是: 张三 123400001 王二 123400003 李四 123400002
2.3. Map接口的实现类
Map接口常用的实现类有HashMap和TreeMap。相对来说HashMap类实现Map集合对于添加和删除映射关系的效率更高。
示例代码:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class MapDemo { public static void main(String[] args) { Map map=new HashMap(); //由HashMap实现的Map对象 Employee emp=new Employee("00001","张三"); //创建Employee对象 Employee emp2=new Employee("00002","李四"); Employee emp3=new Employee("00003","王二"); map.put(emp.getE_id(),emp.getE_name()); //将对象添加到集合中 map.put(emp2.getE_id(), emp2.getE_name()); map.put(emp3.getE_id(), emp3.getE_name()); Set set=map.keySet(); //获取Map集合中的key对象集合 Iterator it=set.iterator(); //创建集合迭代器 System.out.println("HashMap类实现的Map集合,无序:"); while(it.hasNext()) { //遍历Map集合 String str=(String)it.next(); String name=(String)map.get(str); System.out.println(str+" "+ name); } TreeMap treemap=new TreeMap(); //创建TreeMap集合对象 treemap.putAll(map); //向集合添加对象 Iterator it2=treemap.keySet().iterator(); System.out.println("TreeMap类实现的Map集合,键对象升序:"); while(it2.hasNext()) { //遍历TreeMap集合对象 String str=(String)it2.next(); //获取集合中的所有key对象 String name=(String)map.get(str); //获取集合中所有values值 System.out.println(str+" "+name); } } }
执行其结果是:
HashMap类实现的Map集合,无序: 00002 李四 00003 王二 00001 张三 TreeMap类实现的Map集合,键对象升序: 00001 张三 00002 李四 00003 王二

浙公网安备 33010602011771号