zoukankan      html  css  js  c++  java
  • 比较C#和Java中的迭代器同时也是自己的学习吧

    原因:

    平时写程序用到的是 C#中的foreach或者是Java中的增加for循环来遍历,今天看了Java的JDK文档于是自己思考了下,来写一篇博客,顺便也提供参考吧

    C#中的迭代

    要说到在C#中使用迭代就要用到IEnumerable接口,但同时呢IEnumerable接口包含一个GetEnumerator方法,返回值为IEnumerator的类型

    具体测试代码如下:

    首先创建一个Student类,随便重写下toString方法

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace TestDemo
     7 {
     8     public class Student
     9     {
    10         public int stuNum { get; set; }
    11         public string stuName { get; set; }
    12         public Student(int stuNum,string stuName)
    13         {
    14             this.stuName = stuName;
    15             this.stuNum = stuNum;
    16         }
    17         public override string ToString()
    18         {
    19             return this.stuNum + " " + stuName;
    20         }
    21     }
    22 }
    Student

    接下来就是测试代码了:

    public static void Main(string[] args)
            {
                List<Student> mystu = new List<Student> 
                {
                    new Student(1,"David"),
                    new Student(2,"Tome"),
                    new Student(3,"Zhang"),
                    new Student(4,"cnblog")
                };
                //IEnumerable<Student> result = mystu.Where(stu => stu.stuNum>=2);
                foreach(Student s in result)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("--------------");
                IEnumerator<Student> IES = result.GetEnumerator();
                //IES.Reset();
                while(IES.MoveNext())
                {
                    Console.WriteLine(IES.Current);
                }
                Console.ReadKey();
            }
    

    恩,好,接着我们来一起看下Java中的迭代器

    首先创建Student类,随便也重写下toString方法

     1 public class Student {
     2     
     3     public Student(){
     4         
     5     }
     6     public Student(int stuNum,String stuName){
     7         this.stuNum = stuNum;
     8         this.stuName = stuName;
     9     }
    10     public int stuNum;
    11     public String stuName;
    12     public int getStuNum() {
    13         return stuNum;
    14     }
    15     public void setStuNum(int stuNum) {
    16         this.stuNum = stuNum;
    17     }
    18     public String getStuName() {
    19         return stuName;
    20     }
    21     public void setStuName(String stuName) {
    22         this.stuName = stuName;
    23     }
    24     
    25     //重写toString()方法
    26     public String toString(){
    27         return this.stuNum+" "+stuName;
    28     }
    29 }
    View Code

    下面是测试代码:

    public static void main(String[] args) {
    		List<Student> Ls = new ArrayList<Student>();
    		Ls.add(new Student(1,"David Zhang"));
    		Ls.add(new Student(2,"Mr.Smart"));
    		Ls.add(new Student(3,"cnblog"));
    		
    		//一个增加for循环输出
    		for (Student student : Ls) {
    			System.out.println(student);
    		}
    		System.out.println("=---------=");
    		//使用迭代器迭代
    		Iterator<Student> Iter = Ls.iterator();
    		while(Iter.hasNext()){
    			Student stu = Iter.next();
    			System.out.println(stu);
    		}
    		
    	}
    

    恩,好了,到现在为止我们已经把所要测试比较的代码贴出来了,下面首先说说我对C#的思考:

    我们在C#中使用了foreach进行遍历,这就意味着这个foreach实现了IEnumerable和IEnumerator接口,这也进一步的说明了程序运行时编译器会到这个对象的类中去寻找GetEnumerator方法,找到这个方法后返回这个一个IEnumerator的对象,就其中的current(),Reset(),MoveNext()方法我就不再多说了,从字面就可以知道这个方法的作用了。

    关于java中的迭代自己的想法与思考

    首先呢要实现Iterator接口,具体的思路就是通过对象调用iterator方法,每个集合均可创建并返回一个实现Iterator的对象,并且呢将当前位置的概念在对象内存中存储下来。关于它的hasNext(),next(),remove(),方法在这我就不多说啥了,JDK有详细说明。

    注意点:

    这个注意点呢,就是在C#和Java中都有体现的,就是在C#中首先要对象调用MoveNext()方法获得第一个元素,在Java中的对象首先要调用next()方法获取第一个元素。不然会报错!

    在此我要说的就是这些了,有什么不足或者不对的地方,point out.  Thanks

  • 相关阅读:
    C算法编程题系列
    C算法编程题(七)购物
    C算法编程题(六)串的处理
    C算法编程题(五)“E”的变换
    C算法编程题(四)上三角
    C算法编程题(三)画表格
    C算法编程题(二)正螺旋
    C算法编程题(一)扑克牌发牌
    我的编程开始(C)
    T-Sql学习系列完结
  • 原文地址:https://www.cnblogs.com/struCoder/p/3474938.html
Copyright © 2011-2022 走看看