How to retrieve the element dynamically using List generics

Posted by Adityavarma999 under C# on 1/8/2014 | Points: 10 | Views : 638 | Status : [Member] | Replies : 2
Here i want to retrieve elements in array during runtime and to sort the integer array and to print the resultant array using List <int> ListInt=new List <int> ListInt{} generics. Please help me to this sample code.

Thank You


Responses

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Try this Code:

int[] arr = {5, 4, 2, 1, 7, 6 };

int temp = 0;
List<int> oInt = new List<int>();
for (int write = 0; write < arr.Length; write++)
{
int sort = 0;
for (; sort < arr.Length - 1; sort++)
{
if (arr[sort] > arr[sort + 1])
{
temp = arr[sort + 1];
arr[sort + 1] = arr[sort];
arr[sort] = temp;
}
}

}
oInt.AddRange(arr);
foreach (int item in oInt)
{
Console.WriteLine(item);
}


Thank you,
Govind

Adityavarma999, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Here is the simple way:

int[] arr = {5, 4, 2, 1, 7, 6 };
Array.Sort(arr);
List<int> oInt = new List<int>();
oInt.AddRange(arr);
foreach (int item in oInt)
{
Console.WriteLine(item);
}



Thank you,
Govind

Adityavarma999, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response