首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java编程思维学习笔记(三)

2012-08-26 
Java编程思想学习笔记(三)Java编程思想学习笔记(三)?(美) Bruce Eckel 著 《Java编程思想》部分学习日记(随

Java编程思想学习笔记(三)

Java编程思想学习笔记(三)

?

(美) Bruce Eckel 著 《Java编程思想》部分学习日记(随手记录的笔记)

?

interface Selector{ boolean end(); Object current(); void next();}public class Sequence { private Object[] items; private int next =0; public Sequence(int size) { items=new Object[size]; } public void add(Object x) { if (next<items.length) { items[next++]=x; } } public class SequenceSelector implements Selector{ private int i=0; @Override public Object current() { return items[i]; } @Override public boolean end() { return i==items.length; } @Override public void next() { if (i<items.length) { i++; } } } public static void main(String[] args) { Sequence sequence =new Sequence(10); for (int i = 0; i < 10; i++) { sequence.add(Integer.toString(i)); } Selector selector =sequence.new SequenceSelector(); while(!selector.end()){ System.out.println(selector.current()+""); selector.next(); } }}

??其中:Selector selector =sequence.new SequenceSelector();
?? 为创建内部类对象。
?? 如果需要生成对外部类对象的引用,可以使用外部类的名字后面紧跟圆点和this。

public class DotThis {      void f(){System.out.println("DotThis.f()");}      public class Inner{            public DotThis outer() {                  return DotThis.this;            }      }      public Inner inner() {            return new Inner();                  }      public static void main(String[] args) {            DotThis dotThis =new DotThis();            DotThis.Inner dtInner =dotThis.inner();            dtInner.outer().f();      }}

?

?

interface Contents{ int value();}public class Parcel7 { public Contents contents() { return new Contents() { private int i =11; public int value() { return i; } }; } public static void main(String[] args) { Parcel7 parcel7 =new Parcel7(); Contents contents= parcel7.contents(); }}等价于:package zengxiao;interface Contents{ int value();}public class Parcel7 { class MyContent implements Contents{ @Override public int value() { return 0; } }; public Contents contents() { return new MyContent(); } public static void main(String[] args) { Parcel7 parcel7 =new Parcel7(); Contents contents= parcel7.contents(); }}?

?

?

public class Statistics { public static void main(String[] args) { Random rand =new Random(47); Map<Integer, Integer> map =new HashMap<Integer, Integer>(); for (int i = 0; i < 10000; i++) { int r =rand.nextInt(20); Integer freq =map.get(r); map.put(r, freq==null?1:freq+1); } System.out.println(map); }}?

??

import java.util.LinkedList;public class Stack<T> { private LinkedList<T> storage =new LinkedList<T>(); public void push(T v){storage.addFirst(v);} public T peek() { return storage.getFirst(); } public T pop() { return storage.removeFirst(); } public Boolean empty() { return storage.isEmpty(); } public String toString() { return storage.toString(); }}public class StackTest { public static void main(String[] args) { Stack<String> stack = new Stack<String>(); for (String s : "my dog has fleas".split(" ")) { stack.push(s); } while (!stack.empty()) { System.out.println(stack.pop()+""); } }}?? Set
?? 不保存重复的元素。查找成为了set最重要的操作。,hashSet专门对快速查找进行了优化。如果想对结果进行排序的话建议使用TreeSet来代替HashSet

?? Queue
队列是一个典型的先进先出(FIFO)的容器。
?

import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;public class zeng { public static void main(String[] args) { List<Integer> colList = new ArrayList<Integer>(); Integer[] moreInts = { 1, 2, 3, 4, 5 }; Collections.addAll(colList, moreInts); for (Integer integer : moreInts) { System.out.println(integer); } List<Integer> list = Arrays.asList(moreInts); for (Integer integer : list) { System.out.println(integer); } }}?

热点排行