public class TestList { public static void main(String agrs[]) { List l1 = new LinkedList(); for (int i = 0; i <= 5; i++) { l1.add("a" + i); } System.out.PRintln(l1); l1.add(3, "a100"); System.out.println(l1); l1.set(6, "a200"); System.out.println(l1); System.out.print((String) l1.get(2) + " "); System.out.println(l1.indexOf("a3")); l1.remove(1); System.out.println(l1); }}结果是:
[a0, a1, a2, a3, a4, a5]
[a0, a1, a2, a100, a3, a4, a5][a0, a1, a2, a100, a3, a4, a200]a2 4[a0, a2, a100, a3, a4, a200]2.List常用算法类java.util.Collections提供了一些静态方法实现了基于List容器的一些常用算法。void sort(List) 对List容器内的元素排序void shuffle(List) 对List容器内的对象进行随机排序void reverse(List) 对List容器内的对象进行逆序排序void fill(List,Object) 用一个特定的对象重写整个List容器void copy(List dest,List src) 将src List容器内容拷贝到dest List容器int binarySearch(List,Object) 对于顺序的List容器,采用折半查找的方法查找特定对象算法举例:public class TestList { public static void main(String agrs[]) { List l1 = new LinkedList(); List l2 = new LinkedList(); for (int i = 0; i <= 9; i++) { l1.add("a" + i); } System.out.println(l1); Collections.shuffle(l1); //随机排序 System.out.println(l1); Collections.reverse(l1); //逆序 System.out.println(l1); Collections.sort(l1); //排序 System.out.println(l1); System.out.println(Collections.binarySearch(l1, "a5")); //折半查找 }}结果是:[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9][a6, a8, a9, a1, a2, a5, a0, a3, a4, a7][a7, a4, a3, a0, a5, a2, a1, a9, a8, a6][a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]53.Comparable接口所有可以“排序“的类都实现了java.lang.Comparable接口,comparable接口中只有一个方法public int compareTo(Object obj);该方法:返回0表示this==obj,正数this>obj,负数this实现了Comparable接口的类通常实现comparaTo方法从而确定该类对象的排序方式。将上述的Name类改写:class Name implements Comparable加入public int compareTo(Object o) {Name n = (Name)o;int lastCmp =c.compareTo(n.c);return(lastCmp!=0 ? lastCmp :s.compareTo(n.s));}然后运行:public class TestComparable { public static void main(String agrs[]) { List l1 = new LinkedList(); l1.add(new Name("Karl", "M")); l1.add(new Name("Steven", "Lee")); l1.add(new Name("John", "O")); l1.add(new Name("Tom", "M")); System.out.println(l1); Collections.sort(l1); System.out.println(l1); }}结果是:[Karl M, Steven Lee, John O, Tom M][Steven Lee, Karl M, Tom M, John O]
4.如何选择数据结构衡量标准:读的效率和改的效率Array读快改慢,Linked改快读慢,Hash两者之间
5.Map接口实现Map接口的类用来存储 键—值 对。Map接口的实现类有HashMap(哈希表)和TreeMap(二叉树)等。二叉树:在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作左子树(left subtree)和右子树(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。二叉树的每个结点至多只有二棵子树(不存在度大于2的结点)。二叉树的子树有左右之分,次序不能颠倒。二叉树的第i层至多有2^{i-1}个结点;深度为k的二叉树至多有2^k-1个结点;对任何一棵二叉树T,如果其终端结点数为n_0,度为2的结点数为n_2,则n_0=n_2+1。一棵深度为k,且有2^k-1个节点称之为满二叉树;深度为k,有n个节点的二叉树,当且仅当其每一个节点都与深度为k的满二叉树中,序号为1至n的节点对应时,称之为完全二叉树。哈希表:散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。给定表M,存在函数f(key),对任意给定的关键字值key,代入函数后若能得到包含该关键字的记录在表中的地址,则称表M为哈希(Hash)表,函数f(key)为哈希(Hash) 函数。Map类中存储的键—值对通过键来标识,所以键值不能重复。Object put(Object key,Object value); //放入一对东西,返回原key的valueObject get(Object key); //通过key找到valueObject remove(Object key); //去掉key对应的value,key随之也去掉boolean containsKey(object key); //是不是包含keyboolean containsKey(object value);int size(); 多少对boolean isEmpty()void putAll(Map t); 把另一个Map所有值加进来void clear();例:public class TestMap { public static void main(String agrs[]) { Map m1 = new HashMap(); Map m2 = new TreeMap(); m1.put("one", new Integer(1)); m1.put("two", new Integer(2)); m1.put("three", new Integer(3)); m2.put("A", new Integer(1)); m2.put("B", new Integer(2)); System.out.println(m1.size()); System.out.println(m1.containsKey("one")); System.out.println(m2.containsValue(new Integer(1))); if (m1.containsKey("two")) { int i = ((Integer) m1.get("two")).intValue();//get two返回的是object,强制转换成integer,然后获得int的值 System.out.println(i); } Map m3 = new HashMap(m1); m3.putAll(m2); System.out.println(m3); }}结果是:3truetrue2{A=1, B=2, two=2, three=3, one=1}6.Auto-boxing/unboxing在合适的时机自动打包,解包,自动将基础类型转换成对象,自动将对象转换成基础类型public class TestMap { public static void main(String agrs[]) { Map m1 = new HashMap(); Map m2 = new TreeMap(); m1.put("one", 1); //打包 m1.put("two", 2); m1.put("three", 3); m2.put("A", 1); m2.put("B", 2); System.out.println(m1.size()); System.out.println(m1.containsKey("one")); System.out.println(m2.containsValue(1)); if (m1.containsKey("two")) { int i = ((Integer) m1.get("two")); //Integer是不能去掉的,对象转换成Integer后,才能自动转换成int类型 System.out.println(i); } Map m3 = new HashMap(m1); m3.putAll(m2); System.out.println(m3); }}将TestMap加以修改,结果相同
7.泛型JDK1.4以前类型不明确:装入集合的类型都被当成Object对待从而失去自己的实际类型。解决办法:在定义集合的时候同时定义集合中对象的类型。示例:BasicGeneric,可以在定义Collection时定义,也可以在循环时用Iterator指定举例:public class BasicGeneric { public static void main(String agrs[]) { List c = new ArrayList(); c.add("aaa"); c.add("bbb"); c.add("ccc"); for (int i = 0; i < c.size(); i++) { String s = c.get(i).toString(); System.out.println(s); } Collection c2 = new HashSet(); c2.add("AAA"); c2.add("BBB"); c2.add("CCC"); for (Iterator it = c2.iterator(); it.hasNext();){ String s = it.next().toString(); System.out.println(s); } }}public class MyName implements Comparable<MyName>{//对MyName类用泛型实现comparable int age; public int compareTo(MyName nm) { if(this.age > nm.age) return 1; else if(this.age < nm.age) return -1; else return 0; }}结果是:aaa bbb ccc AAA CCC BBB泛型和打包综合利用:public class TestMap { public static void main(String agrs[]) { Map m1 = new HashMap(); m1.put("one", 1); m1.put("two", 2); m1.put("three", 3); System.out.println(m1.size()); System.out.println(m1.containsKey("one")); if (m1.containsKey("two")) { int i = (int) m1.get("two"); System.out.println(i); } }}结果是:3true2另一个例子:public class TestArgsWords { private static final int ONE = 1; public static void main(String args[]) { Map m = new HashMap(); for (int i = 0; i < args.length; i++) { if (!m.containsKey(args[i])) { m.put(args[i], ONE); } else { int freq = (int) m.get(args[i]); m.put(args[i], freq + 1); } } System.out.println (m.size() + " distinct words detected:"); System.out.println(m); }}结果:0 distinct words detected:{}
新闻热点
疑难解答