java學(xué)習(xí)

          java學(xué)習(xí)

           

          設(shè)計模式之Iterator

          public interface Iterator {
          Object next();
          boolean hasNext();
          }
          public interface Collection {
          void add(Object data);
          int size();
          Iterator iterator();
          }
          //集合
          public class MyArrayList implements Collection{
          private Object[] array = new Object[10];
          private int index=0;//數(shù)值當(dāng)前可以放值的下標(biāo)
          public void add( Object obj){
          if(index==array.length){
          Object[] newArray = new Object[array.length*2];
          System.arraycopy(array, 0, newArray, 0, index);
          array=newArray;
          }
          array[index]=obj;
          index++;
          }
          public int size(){
          return index;
          }
          @Override
          public Iterator iterator() {
          return new Aaa();
          }
          private class Aaa implements Iterator{
          private int c=0;
          @Override
          public boolean hasNext() {
          if(c<index){
          return true;
          }else{
          return false;
          }
          }
          @Override
          public Object next() {
          Object object = array[c];
          c++;
          return object;
          }
          }
          public static void main(String[] args) {
          MyArrayList arr = new MyArrayList();
          for (int i = 0; i < 20; i++) {
          arr.add(new Object());
          }
          System.out.println(arr.size());
          Iterator iterator = arr.iterator();
          while(iterator.hasNext()){
          System.out.println(iterator.next());
          }
          }
          }
          //鏈表
          public class MyNode {
          private Object data;
          private MyNode nextNode;
          public MyNode(Object data,MyNode nextNode){
          this.data=data;
          this.nextNode=nextNode;
          }
          public Object getData() {
          return data;
          }
          public void setData(Object data) {
          this.data = data;
          }
          public MyNode getNextNode() {
          return nextNode;
          }
          public void setNextNode(MyNode nextNode) {
          this.nextNode = nextNode;
          }
          }
          public class MyLinkTable implements Collection{
          private MyNode head;
          private MyNode tail;
          private int size = 0;
          public void add(Object data) {
          MyNode n = new MyNode(data, null);
          if (head == null) {
          head = n;
          tail = n;
          }
          tail.setNextNode(n);
          tail = n;
          size++;
          }
          public int size() {
          return size;
          }
          public MyNode getHead() {
          return head;
          }
          public void setHead(MyNode head) {
          this.head = head;
          }
          public MyNode getTail() {
          return tail;
          }
          public void setTail(MyNode tail) {
          this.tail = tail;
          }
          @Override
          public Iterator iterator() {
          return new Aaa();
          }
          private class Aaa implements Iterator{
          private int c=0;
          MyNode node=null;
          @Override
          public boolean hasNext() {
          for (int i = 0; i <=c; i++) {
          if(i==0){
          node = head;
          }else{
          node = node.getNextNode();
          }
          }
          if(node==null){
          return false;
          }else{
          return true;
          }
          }
          @Override
          public Object next() {
          for (int i = 0; i <=c; i++) {
          if(i==0){
          node = head;
          }else{
          node = node.getNextNode();
          }
          }
          c++;
          return node;
          }
          }
          public static void main(String[] args) {
          MyLinkTable node = new MyLinkTable();
          node.add(1);
          node.add(2);
          node.add(3);
          Iterator iterator = node.iterator();
          while(iterator.hasNext()){
          MyNode node1= (MyNode) iterator.next();
          System.out.println(node1.getData());
          }
          }
          }

          posted on 2017-09-12 15:54 楊軍威 閱讀(116) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 河北区| 岑巩县| 闻喜县| 桃园市| 山阴县| 普兰县| 葫芦岛市| 朝阳市| 金昌市| 黎城县| 南汇区| 临江市| 北海市| 大渡口区| 慈利县| 浑源县| 怀柔区| 佛教| 大理市| 浏阳市| 嘉善县| 贺兰县| 玉田县| 平武县| 尉氏县| 内丘县| 吴川市| 绥芬河市| 黄平县| 通榆县| 平乐县| 鄂托克旗| 施秉县| 泽普县| 黑山县| 黑水县| 和政县| 突泉县| 商丘市| 井研县| 浦北县|