PDA

View Full Version : سوال: لیست در C#



root88
دوشنبه 27 دی 1389, 15:21 عصر
با سلام، من در c# مبتدیم یه مقدارc++ بلدم.

اگه ممکنه به سوالات من در این زمینه جواب بدید

من یه لیست دارم می خوام یه Iterator براش تعریف کنم و ببربمش جلو نمیدونم چطور msdn هم بررسی کردم اما چیزی نیافتم.

MortezaGity
دوشنبه 27 دی 1389, 15:44 عصر
دوست عزیز از منظورت از لیست همون GenericListکه من مد نظرمه خودش اینترفیس IEnumerable , IEnumarator رو پیاده سازی می کنه. این کدو ببین


int myint=0;
List<int> myList = new List<int>();
foreach(int element in mylist)
{
myint=element;
}

tooraj_azizi_1035
دوشنبه 27 دی 1389, 15:45 عصر
سلام،
لیستت باید از اینترفیس IEnumerator مشتق بشه. این یه مثال به همراه لینک در MSDN:
http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx

using System;
using System.Collections;

public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}

public string firstName;
public string lastName;
}

public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];

for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) GetEnumerator();
}

public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
}

public class PeopleEnum : IEnumerator
{
public Person[] _people;

// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;

public PeopleEnum(Person[] list)
{
_people = list;
}

public bool MoveNext()
{
position++;
return (position < _people.Length);
}

public void Reset()
{
position = -1;
}

object IEnumerator.Current
{
get
{
return Current;
}
}

public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}

class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};

People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);

}
}

/* This code produces output similar to the following:
*
* John Smith
* Jim Johnson
* Sue Rabon
*
*/

tooraj_azizi_1035
دوشنبه 27 دی 1389, 16:14 عصر
د رمثال بالا People مجموعه ای از Person ها است. در مورد لیست پیوندی شما هم هر گره حکم Person در مثال بالا و کل لیست که یک Collection است حکم همان People را دارد.
لینک:http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx

root88
سه شنبه 28 دی 1389, 08:16 صبح
سلام، ببخشید من چندتا سوال دیگه دارم.
1.Threadpool چیه و به چه دردی می خوره؟

shuriken
سه شنبه 28 دی 1389, 09:16 صبح
سلام
thredpool در واقع یک ساختار هست برای مدیریت thread هایی که شما در برنامه ازشون استفاده میکنید، اجرای غیر همزمان یا همون asynchronous
و اجرای task ها بکار میره.
پایین چند نمونه کد میذارم.


using System;
using System.Threading;
public class Example {
public static void Main() {
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

Console.WriteLine("Main thread does some work, then sleeps.");
// If you comment out the Sleep, the main thread exits before
// the thread pool task runs. The thread pool uses background
// threads, which do not keep the application running. (This
// is a simple example of a race condition.)
Thread.Sleep(1000);

Console.WriteLine("Main thread exits.");
}

// This thread procedure performs the task.
static void ThreadProc(Object stateInfo) {
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Console.WriteLine("Hello from the thread pool.");
}
}




using System;
using System.Threading;

public class Fibonacci
{
public Fibonacci(int n, ManualResetEvent doneEvent)
{
_n = n;
_doneEvent = doneEvent;
}

// Wrapper method for use with thread pool.
public void ThreadPoolCallback(Object threadContext)
{
int threadIndex = (int)threadContext;
Console.WriteLine("thread {0} started...", threadIndex);
_fibOfN = Calculate(_n);
Console.WriteLine("thread {0} result calculated...", threadIndex);
_doneEvent.Set();
}

// Recursive method that calculates the Nth Fibonacci number.
public int Calculate(int n)
{
if (n <= 1)
{
return n;
}

return Calculate(n - 1) + Calculate(n - 2);
}

public int N { get { return _n; } }
private int _n;

public int FibOfN { get { return _fibOfN; } }
private int _fibOfN;

private ManualResetEvent _doneEvent;
}

public class ThreadPoolExample
{
static void Main()
{
const int FibonacciCalculations = 10;

// One event is used for each Fibonacci object
ManualResetEvent[] doneEvents = new ManualResetEvent[FibonacciCalculations];
Fibonacci[] fibArray = new Fibonacci[FibonacciCalculations];
Random r = new Random();

// Configure and launch threads using ThreadPool:
Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
for (int i = 0; i < FibonacciCalculations; i++)
{
doneEvents[i] = new ManualResetEvent(false);
Fibonacci f = new Fibonacci(r.Next(20,40), doneEvents[i]);
fibArray[i] = f;
ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
}

// Wait for all threads in pool to calculation...
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("All calculations are complete.");

// Display the results...
for (int i= 0; i<FibonacciCalculations; i++)
{
Fibonacci f = fibArray[i];
Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN);
}
}
}

ای کدهارو از msdn پیدا کردم خودتم یسری بهش بزن.
اگه بازم سوالی بود در خدمتم.
موفق باشی

root88
سه شنبه 28 دی 1389, 20:56 عصر
سلام، آیا ممکنه یه ترد بتونه state یه ترد دیگه رو بررسی کنه، اگه ممکنه با چه دستوری؟ و اگه نه چیکار کنم؟

shuriken
چهارشنبه 29 دی 1389, 08:40 صبح
تا جایی که میدونم میتونین با استفاده از اسم یک thread به state اش دستزسی داشته باشین
این سایتم یه نگاه بندازین
http://msdn.microsoft.com/en-us/library/system.threading.thread.threadstate.aspx

root88
چهارشنبه 29 دی 1389, 08:45 صبح
خیلی ممنون، این مربوط به ترد جاری هست. مثلا من میخوام ترد A وضعیت ترد B رو بررسی کنه.

shuriken
چهارشنبه 29 دی 1389, 09:24 صبح
فقط کافیه یبار امتحان کنین
1 برنامه ساده بنویسین که اینه امتحان میکنه
البته تا جایی که میدونم بایش بشه. چون در واقع خود برنامه هم توسط یه thread دیگه اجرا میشه و وقتی
میشه تو اون اطلاعات سایر thread هارو بدست آورد تو بقیه هم میشه

root88
جمعه 01 بهمن 1389, 09:12 صبح
با سلام، من یه ماتریس تعریف کردم که عناصرش از نوع لیست اند. حالا که می خوام ازش استفاده کنم و عنصر به لیست یکی از سطرها اضافه کنم میگه نمونه ای از شی ساخته نشده. ممنون میشم راهنمایی کنید. این کد دستور هست.



static LinkedList<int>[,] FrmLST = new LinkedList<int> [N, 1];
.
.
.

FrmLST[2,0].AddLast(3); FrmLST[2,0].AddLast(5); FrmLST[3, 0].AddLast(6);

FrmLST[2,0].Concat(FrmLST[3,0]);

root88
جمعه 01 بهمن 1389, 11:45 صبح
لطفا یکی منو راهنمایی کنه...

shuriken
شنبه 02 بهمن 1389, 07:47 صبح
سلام
متاسفانه برخی مواقع علاوه بر صورتی که شما آرایتون رو new کردین باید کل آرایرم پیمایش کنید
و کلیه اعضارو new کنید. کدشم خیلی آسونه.


LinkedList<int>[,] FrmLST = new LinkedList<int> [N, 1];
for(int i=0;i<N;++i)
{
for(int j=0;j<1)
{
FrmLST[i,j]=new LinkedList<int>();
}
}