Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > อยากทราบการเขียน Iterator จาก java code เป็น C# code ครับ


 

[.NET] อยากทราบการเขียน Iterator จาก java code เป็น C# code ครับ

 
Topic : 054444



โพสกระทู้ ( 156 )
บทความ ( 0 )



สถานะออฟไลน์



ถ้าผมมี code java แบบนี้
Code (PHP)
01.private Iterator<Integer> iter;     //Iterator for indexList OR lineList (depends on the call)
02.//Iterator for index list. Return iterator's hasNext for index list
03.    public bool hasNext()
04.    {
05.        if (!iter.hasNext())
06.            return false;
07.        return true;
08.    }
09.//Return iterator's next index
10.    public int next()
11.    {
12.        return((int)iter.next()).intValue();
13.    }


ผมจะแปลงเป็น C# ยังไงครับ ตอนนี้ที่มัน error คือ ตรง .hasNext(), กับ .next() ครับ



Tag : .NET, C#, VS 2010 (.NET 4.x)

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-01-13 18:04:53 By : nottp106 View : 1663 Reply : 2
 

 

No. 1



โพสกระทู้ ( 1,603 )
บทความ ( 1 )



สถานะออฟไลน์


กรณี ใช้ Iterator feature กับ class ที่เป็น collection ในตัวอย่างจะเป็น Generic type
[Any object ,any data type]
Code (C#)
001.using System.Collections;
002.using System.Collections.Generic;
003. 
004.namespace GenericIteratorExample
005.{
006.    public class Stack<T> : IEnumerable<T>
007.    {
008.        private T[] values = new T[100];
009.        private int top = 0;
010. 
011.        public void Push(T t) { values[top++] = t; }
012.        public T Pop() { return values[--top]; }
013. 
014.        // These make Stack<T> implement IEnumerable<T> allowing
015.        // a stack to be used in a foreach statement.
016.        public IEnumerator<T> GetEnumerator()
017.        {
018.            for (int i = top; --i >= 0; )
019.            {
020.                yield return values[i];
021.            }
022.        }
023. 
024.        IEnumerator IEnumerable.GetEnumerator()
025.        {
026.            return GetEnumerator();
027.        }
028. 
029.        // Iterate from top to bottom.
030.        public IEnumerable<T> TopToBottom
031.        {
032.            get
033.            {
034.                // Since we implement IEnumerable<T>
035.                // and the default iteration is top to bottom,
036.                // just return the object.
037.                return this;
038.            }
039.        }
040. 
041.        // Iterate from bottom to top.
042.        public IEnumerable<T> BottomToTop
043.        {
044.            get
045.            {
046.                for (int i = 0; i < top; i++)
047.                {
048.                    yield return values[i];
049.                }
050.            }
051.        }
052. 
053.        //A parameterized iterator that return n items from the top
054.        public IEnumerable<T> TopN(int n)
055.        {
056.            // in this example we return less than N if necessary
057.            int j = n >= top ? 0 : top - n;
058. 
059.            for (int i = top; --i >= j; )
060.            {
061.                yield return values[i];
062.            }
063.        }
064.    }
065. 
066.    //This code uses a stack and the TopToBottom and BottomToTop properties
067.    //to enumerate the elements of the stack.
068.    class Test
069.    {
070.        static void Main()
071.        {
072.            Stack<int> s = new Stack<int>();
073.            for (int i = 0; i < 10; i++)
074.            {
075.                s.Push(i);
076.            }
077. 
078.            // Prints: 9 8 7 6 5 4 3 2 1 0
079.            // Foreach legal since s implements IEnumerable<int>
080.            foreach (int n in s)
081.            {
082.                System.Console.Write("{0} ", n);
083.            }
084.            System.Console.WriteLine();
085. 
086.            // Prints: 9 8 7 6 5 4 3 2 1 0
087.            // Foreach legal since s.TopToBottom returns IEnumerable<int>
088.            foreach (int n in s.TopToBottom)
089.            {
090.                System.Console.Write("{0} ", n);
091.            }
092.            System.Console.WriteLine();
093. 
094.            // Prints: 0 1 2 3 4 5 6 7 8 9
095.            // Foreach legal since s.BottomToTop returns IEnumerable<int>
096.            foreach (int n in s.BottomToTop)
097.            {
098.                System.Console.Write("{0} ", n);
099.            }
100.            System.Console.WriteLine();
101. 
102.            // Prints: 9 8 7 6 5 4 3
103.            // Foreach legal since s.TopN returns IEnumerable<int>
104.            foreach (int n in s.TopN(7))
105.            {
106.                System.Console.Write("{0} ", n);
107.            }
108.            System.Console.WriteLine();
109.        }
110.    }
111.}


กรณี implement Iterator feature ลงใน class ใดๆ
Code (C#)
001.using System;
002.using System.Collections;
003. 
004.public class Person
005.{
006.    public Person(string fName, string lName)
007.    {
008.        this.firstName = fName;
009.        this.lastName = lName;
010.    }
011. 
012.    public string firstName;
013.    public string lastName;
014.}
015. 
016.public class People : IEnumerable
017.{
018.    private Person[] _people;
019.    public People(Person[] pArray)
020.    {
021.        _people = new Person[pArray.Length];
022. 
023.        for (int i = 0; i < pArray.Length; i++)
024.        {
025.            _people[i] = pArray[i];
026.        }
027.    }
028. 
029.    IEnumerator IEnumerable.GetEnumerator()
030.    {
031.       return (IEnumerator) GetEnumerator();
032.    }
033. 
034.    public PeopleEnum GetEnumerator()
035.    {
036.        return new PeopleEnum(_people);
037.    }
038.}
039. 
040.public class PeopleEnum : IEnumerator
041.{
042.    public Person[] _people;
043. 
044.    // Enumerators are positioned before the first element
045.    // until the first MoveNext() call.
046.    int position = -1;
047. 
048.    public PeopleEnum(Person[] list)
049.    {
050.        _people = list;
051.    }
052. 
053.    public bool MoveNext()
054.    {
055.        position++;
056.        return (position < _people.Length);
057.    }
058. 
059.    public void Reset()
060.    {
061.        position = -1;
062.    }
063. 
064.    object IEnumerator.Current
065.    {
066.        get
067.        {
068.            return Current;
069.        }
070.    }
071. 
072.    public Person Current
073.    {
074.        get
075.        {
076.            try
077.            {
078.                return _people[position];
079.            }
080.            catch (IndexOutOfRangeException)
081.            {
082.                throw new InvalidOperationException();
083.            }
084.        }
085.    }
086.}
087. 
088.class App
089.{
090.    static void Main()
091.    {
092.        Person[] peopleArray = new Person[3]
093.        {
094.            new Person("John", "Smith"),
095.            new Person("Jim", "Johnson"),
096.            new Person("Sue", "Rabon"),
097.        };
098. 
099.        People peopleList = new People(peopleArray);
100.        foreach (Person p in peopleList)
101.            Console.WriteLine(p.firstName + " " + p.lastName);
102. 
103.    }
104.}


แต่ถ้าจะสะดวกในการใช้งานน่าจะ inherit จาก data structure ที่ใกล้เคียงกับงานของคุณจะดีกว่าเช่น
List< int > หรือ Hastable<int>


ประวัติการแก้ไข
2011-01-13 19:52:31
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-01-13 19:46:51 By : blurEyes
 

 

No. 2



โพสกระทู้ ( 156 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณครับ :)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-01-14 15:02:20 By : nottp106
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : อยากทราบการเขียน Iterator จาก java code เป็น C# code ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่