JavaCookbook-7.结构化数据

来源:互联网 发布:2017运动手环推荐知乎 编辑:程序博客网 时间:2024/05/29 17:10

      程序是由数据和算法构成的,数据一般有内存数据,硬盘文件或者数据库。下面总结Java中常用的数据结构:

      数组,数组元素可以是简单数据类型,也可以是符合数据类型。声明有两种格式。由于数组是定长的,所以在动态调正时可能需要使用System.arrayCopy功能。

      集合类,ArrayList是一个Java的类,并不是语法的一部分。另外集合类只能包含对象引用,不能用简单类型如int等。同数组相比,集合类效率稍微低一些,但是并没有太过影响性能。同时,由于允许多线程同时访问独享,对于单项称应用程序而言,采用Arraylist,其效率会有提升。

ArrayList<String>alA = new ArrayList<String>();alA.add("gongqingkui");System.out.println(alA.get(0));for (String s :alA) {         System.out.println(s);}System.out.println(alA.contains("gongqingkui")); Iterator<String>i = alA.iterator();while(i.hasNext()) {         System.out.println(i.next());}

      为了解决在不知道在集合类型的情况下查看集合元素的问题,我们需要定义元素实现一个Iterator接口。

      使用HashMap和HashTable可以实现一组对象到另一组对象的单向映射表(也叫散列表)。这种散列关系是一种键-值对的关系。

HashMap<String,String>hm = new HashMap<String,String>();hm.put("gongqingkui","shandongtengzhou");hm.put("zhangsan","zhejiangzhuji");hm.put("lisi","hebeizhengding");Iterator hmi1 =hm.keySet().iterator();while(hmi1.hasNext()) {         String s = (String) hmi1.next();         System.out.println(s+":"+hm.get(s));}

      使用Preferences类可以实现与操作系统相关的属性存取任务。Properties和HashMap比较相似,实际上他是由后者继承而来的。用于读取配置文件的信息,同时注意配置文件采用的是key-value格式。

Preferences p =Preferences.userNodeForPackage(p7structData.class);String text =p.get("textFont", "bold");System.out.println(text);p.put("textFont","Itaic");Properties pro =new Properties();try {         pro.load(newFileReader("a.txt"));         pro.setProperty("name","gongqingkui");         System.out.println(pro.getProperty("name"));         System.out.println(pro.getProperty("age"));         pro.list(System.out);} catch(FileNotFoundException e) {         e.printStackTrace();} catch(IOException e) {         e.printStackTrace();}

      排序问题要针对不同对象,数组可以使用数组的sort方法,集合可以使用集合的sort方法,如果单独定义,则需要单独实现类对象的comparator方法。实现comparator接口,必须实现它的方法compare。

public class p7comparator implements Comparator<String>{    public int compare(String o1, String o2) {        return o1.compareTo(o2);    }        public static void main(String[] args) {        String [] ss=new String[]{"c","b","a"};        Arrays.sort(ss,new p7comparator());        System.out.println(Arrays.toString(ss));    }}

      为了避免重复性排序,可以使用TreeSet或者TreeMap对象来进行元素组织,这种对象可以保持元素始终有序。TreeSet实现元素有序,TreeMap实现主键有序。其方法headSet或者headMap返回前面的有序序列,tailSet和tailMap实现后面的有序序列。

TreeSet<String>ts = new TreeSet<String>();ts.add("ccc");ts.add("aaa");ts.add("bbb");ts.add("ddd");ts.add("fff");Iterator<String>tsi = ts.iterator();while(tsi.hasNext()) {         System.out.println(tsi.next());}System.out.println(ts.headSet("ddd"));System.out.println(ts.tailSet("ddd"));

输出为:aaa

bbb

ccc

ddd

fff

[aaa, bbb, ccc]

[ddd, fff]

      HashSet可以实现元素的无重复值存放。

HashSet<String>hs = new HashSet<String>();hs.add("a");hs.add("a");hs.add("b");hs.add("b");System.out.println(hs);

输出为:[b, a]

      堆栈使用Java.util.Stack类来实现。

Stack<String>s = new Stack<String>();s.push("1");s.push("2");s.push("3");System.out.println(s.pop()+s.pop()+s.pop());

输出为:321