Here are my code snippets regarding arrays, hope they will help u:
One-dimensional Arrays:
CODE
int[] array1 = { 1, 2, 3, 4, 5 };
double[] array2 = { 2.0, 3.0, 4.5 };
char[] array3 = { 'a', 'b', 'c' };
string[] friendNames = {"Robert Barwell", "Mike Parry",
"Jeremy Beacock"};
int[] myIntArray = new int[5];
int[] myIntArray = new int[5] {5, 9, 10, 2, 99};
const int arraySize = 5;
int[] myIntArray = new int[arraySize] {5, 9, 10, 2, 99};
string[] Friends = {"David", "Donna","Jim"};
for (int i = 0; i < Friends.Length; i++)
{
Console.WriteLine(friendNames[i]);
}
string[] Friends = {"David", "Steave", "Jim"};
foreach (string friendName in Friends)
{
Console.WriteLine(friendName);
}
Multi-dimensional Arrays:
CODE
double[,] hillHeight = new double[3,4];
double[,] hillHeight = {{1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}};
Arrays of Arrays:
CODE
jaggedIntArray = {{1, 2, 3}, {1}, {1, 2}};
jaggedIntArray = {new int[] {1, 2, 3}, new int[] {1}, new int[] {1, 2}};
int[][] array_of_arrays = {new int[] {1},
new int[] {1, 2},
new int[] {1, 3},
new int[] {1, 2, 4},
new int[] {1, 5},
new int[] {1, 2, 3, 6},
new int[] {1, 7},
new int[] {1, 2, 4, 8},
new int[] {1, 3, 9},
new int[] {1, 2, 5, 10}};
foreach (int[] GeneralArray in array_of_arrays)
{
foreach (int element in GeneralArray)
{
Console.WriteLine(element);
}
}
CODE
// The private array of Card objects, which we'll call cards:
private Card[] cards;
double[][] shapes = new double[4][];
shapes[0] = new double[1] {10};
shapes[1] = new double[4] {3, 4, 3, 4};
shapes[2] = new double[3] {3, 4, 5};
shapes[3] = new double[5] {5, 5, 5, 5, 5};
CODE
// arrays sorting
using System;
class arraySort
{
// array of integers to hold values
private int[] a = new int[100];
// number of elements in array
private int x;
// Bubble Sort Algorithm
public void sortArray()
{
int i;
int j;
int temp;
for (i = (x - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (a[j - 1] > a[j])
{
temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
}
}
}
public static void Main()
{
// Instantiate an instance of the class
arraySort mySort = new arraySort();
// Get the number of elements to store in the array
Console.Write("Number of elements in the array (less than 100) : ");
string s = Console.ReadLine();
mySort.x = Int32.Parse(s);
// Array header
Console.WriteLine("");
Console.WriteLine("-----------------------");
Console.WriteLine(" Enter array elements ");
Console.WriteLine("-----------------------");
// Get array elements
for (int i = 0; i < mySort.x; i++)
{
Console.Write("<{0}> ", i + 1);
string s1 = Console.ReadLine();
mySort.a[i] = Int32.Parse(s1);
}
// Sort the array
mySort.sortArray();
// Output sorted array
Console.WriteLine("");
Console.WriteLine("-----------------------");
Console.WriteLine(" Sorted array elements ");
Console.WriteLine("-----------------------");
for (int j = 0; j < mySort.x; j++)
{
Console.WriteLine(mySort.a[j]);
}
// Here to stop app from closing
Console.WriteLine("\n\nPress Return to exit.");
Console.Read();
}
}
Another program to sort arrays:
CODE
using System;
namespace SortInterface
{
interface IDisplayable
{
string GetString();
}
class Student : IComparable, IDisplayable
{
private string sName;
private double dGrade = 0.0;
public Student(string sName, double dGrade)
{
this.sName = sName;
this.dGrade = dGrade;
}
static string[] sNames = { "Homer", "Marge", "Bart", "Lisa", "Maggie" };
static double[] dGrades = { 0, 85, 50, 100, 30 };
public static Student[] CreateStudentList()
{
Student[] sArray = new Student[sNames.Length];
for (int i = 0; i < sNames.Length; i++)
{
sArray[i] = new Student(sNames[i], dGrades[i]);
}
return sArray;
}
public string Name
{
get { return sName; }
}
public double Grade
{
get { return dGrade; }
}
public int CompareTo(object rightObject)
{
Student leftStudent = this;
if (!(rightObject is Student))
{
Console.WriteLine("Compare method passed a nonStudent");
return 0;
}
Student rightStudent = (Student)rightObject;
if (rightStudent.Grade < leftStudent.Grade)
{
return -1;
}
if (rightStudent.Grade > leftStudent.Grade)
{
return 1;
}
return 0;
}
public string GetString()
{
string sPadName = Name.PadRight(9);
string s = String.Format("{0}:{1:N0}", sPadName, Grade);
return s;
}
}
class Bird : IComparable, IDisplayable
{
private string sName;
public Bird(string sName)
{
this.sName = sName;
}
static string[] sBirdNames = { "Oriole", "Hawk", "Robin", "Cardinal", "Blue jay", "Finch", "Sparrow" };
public static Bird[] CreateBirdList()
{
Bird[] birds = new Bird[sBirdNames.Length];
for (int i = 0; i < sBirdNames.Length; i++)
{
birds[i] = new Bird(sBirdNames[i]);
}
return birds;
}
public string Name
{
get { return sName; }
}
public int CompareTo(object rightObject)
{
Bird leftBird = this;
Bird rightBird = (Bird)rightObject;
return String.Compare(leftBird.Name, rightBird.Name);
}
public string GetString()
{
return Name;
}
}
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Sorting the list of students: ");
Student[] students = Student.CreateStudentList();
IComparable[] comparableObjects = (IComparable[])students;
Array.Sort(comparableObjects);
IDisplayable[] displayableObjects = (IDisplayable[])students;
DisplayArray(displayableObjects);
Console.WriteLine("\nSorting the list of birds: ");
Bird[] birds = Bird.CreateBirdList();
Array.Sort(birds);
DisplayArray(birds);
Console.WriteLine("Press Enter to terminate...");
Console.Read();
}
public static void DisplayArray(IDisplayable[] displayables)
{
int length = displayables.Length;
for (int index = 0; index < length; index++)
{
IDisplayable displayable = displayables[index];
Console.WriteLine("{0}", displayable.GetString());
}
}
}
}
IMHO, the thread priority that u setup is not correct. This is just a beginner's question. Simply google for it.
This post has been edited by davegeek: 26 Feb, 2008 - 02:27 AM