11 Replies - 2230 Views - Last Post: 02 March 2008 - 09:24 PM Rate Topic: -----

#1 kaviraj_01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 08

What is an array?

Posted 25 February 2008 - 08:56 PM

what is array?
Is This A Good Question/Topic? 0
  • +

Replies To: What is an array?

#2 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: What is an array?

Posted 25 February 2008 - 09:06 PM

An array is a data structure that contains a set number of variables of the same type. Arrays are declared with a type.
type[] arrayName;


for more information regarding arrays: Microsoft C#

Hope this helps! :D

This post has been edited by Nykc: 25 February 2008 - 09:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: What is an array?

Posted 25 February 2008 - 09:20 PM

An array is a variable that holds multiple values of the same type. There are 2 types of arrays:

  • One Dimensional Array
  • 2 Dimensional Array
    • Rectangular Array

    • Jagged Array


Here is an example of the different types of arrays:

1 Dimensional Array

//Example of a 1 dimensional array

//Declare a 1 dimensional array that will hold 3 items
string[] people = new string[3];

'Declare a 1 dimensional array and add the values to it
string[] people = new string[] {"James", "Joe", "Sally"};




Multi Dimensional Array

C# supports two types of multidimensional arrays: rectangular and jagged. A rectangular array is a single array with more than one dimension, with the dimensions' sizes fixed in the array's declaration. Using jagged arrays, one can create multidimensional arrays with irregular dimensions. This flexibility derives from the fact that multidimensional arrays are implemented as arrays of arrays:

Rectangular Array

//2 Dimensional Array Examples

//Declare a 2 dimensional array that
//will hold peoples first & last name
string[,] people = new string[3,2];

//Declare a 2 dimensional Array and add values
string[,] people = new string[3,2] {{"John", "Smith"},{"Joe", "Brown"}, {"Sally", "Jones"}};




Jagged Array

//Declare a jagged array
string[][] people= new string[2][];
people[0] = new string[2];
people[1] = new string[2];
//Declare a Jagged array and add values
string[][] people= new string[][] {new string[] {"Joe", "John"}, new string[] {"Smith", "Brown"}};
people[1] = new string[6];




Hope that helps some :)
Was This Post Helpful? 0
  • +
  • -

#4 kaviraj_01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 08

Re: What is an array?

Post icon  Posted 25 February 2008 - 09:31 PM

What is Array?
Was This Post Helpful? 0
  • +
  • -

#5 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: What is an array?

Posted 25 February 2008 - 09:32 PM

Read your last post.
No need to post twice.
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: What is an array?

Posted 25 February 2008 - 10:21 PM

You have now posted the same exact question in 2 different threads. I am merging the 2 threads, do not open a new thread about the same question
Was This Post Helpful? 0
  • +
  • -

#7 javagoutom  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 20-September 07

Re: What is an array?

Posted 26 February 2008 - 01:29 AM

it is a collection of data of same datatype.... tht means it can hold many data of same type under same roof....... and can b retrieved by using the arrayname Succeeding with its index number....
Was This Post Helpful? 0
  • +
  • -

#8 davegeek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 30-January 08

Re: What is an array?

Posted 26 February 2008 - 03:22 AM

Here are my code snippets regarding arrays, hope they will help u:

One-dimensional Arrays:

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:
double[,] hillHeight = new double[3,4];
double[,] hillHeight = {{1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}};



Arrays of Arrays:
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);
				}
		}



// 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};




// 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:

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 February 2008 - 03:27 AM

Was This Post Helpful? 0
  • +
  • -

#9 kaviraj_01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 08

Re: What is an array?

Posted 02 March 2008 - 09:10 PM

hai,
this is raj here i have problem in array i am not getting clear that what is array?
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: What is an array?

Posted 02 March 2008 - 09:15 PM

You asked that question in this thread and I thought it was explained very well and nice examples were even provided. So what is it you're not understanding?
Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: What is an array?

Posted 02 March 2008 - 09:21 PM

Topics merged.
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: What is an array?

Posted 02 March 2008 - 09:24 PM

Thanks, I knew I was forgetting to do something lol :blush:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1