Welcome to Dream.In.Code
Become a C# Expert!

Join 150,390 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,063 people online right now. Registration is fast and FREE... Join Now!




What is an array?

 
Reply to this topicStart new topic

What is an array?, Array

kaviraj_01
25 Feb, 2008 - 07:56 PM
Post #1

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 5

what is array?
User is offlineProfile CardPM
+Quote Post

Nykc
RE: What Is An Array?
25 Feb, 2008 - 08:06 PM
Post #2

That Just Happened!
Group Icon

Joined: 14 Sep, 2007
Posts: 4,509



Thanked: 18 times
Dream Kudos: 275
My Contributions
An array is a data structure that contains a set number of variables of the same type. Arrays are declared with a type.
csharp
type[] arrayName;


for more information regarding arrays: Microsoft C#

Hope this helps! biggrin.gif

This post has been edited by Nykc: 25 Feb, 2008 - 08:09 PM
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: What Is An Array?
25 Feb, 2008 - 08:20 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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

csharp

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

csharp

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

csharp

//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 smile.gif
User is online!Profile CardPM
+Quote Post

kaviraj_01
RE: What Is An Array?
25 Feb, 2008 - 08:31 PM
Post #4

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 5

What is Array?

User is offlineProfile CardPM
+Quote Post

Nykc
RE: What Is An Array?
25 Feb, 2008 - 08:32 PM
Post #5

That Just Happened!
Group Icon

Joined: 14 Sep, 2007
Posts: 4,509



Thanked: 18 times
Dream Kudos: 275
My Contributions
Read your last post.
No need to post twice.
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: What Is An Array?
25 Feb, 2008 - 09:21 PM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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
User is online!Profile CardPM
+Quote Post

javagoutom
RE: What Is An Array?
26 Feb, 2008 - 12:29 AM
Post #7

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 25


My Contributions
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....
User is offlineProfile CardPM
+Quote Post

davegeek
RE: What Is An Array?
26 Feb, 2008 - 02:22 AM
Post #8

D.I.C Head
Group Icon

Joined: 30 Jan, 2008
Posts: 81



Thanked: 2 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

kaviraj_01
RE: What Is An Array?
2 Mar, 2008 - 08:10 PM
Post #9

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 5

hai,
this is raj here i have problem in array i am not getting clear that what is array?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: What Is An Array?
2 Mar, 2008 - 08:15 PM
Post #10

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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?
User is online!Profile CardPM
+Quote Post

Jayman
RE: What Is An Array?
2 Mar, 2008 - 08:21 PM
Post #11

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Topics merged.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: What Is An Array?
2 Mar, 2008 - 08:24 PM
Post #12

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Thanks, I knew I was forgetting to do something lol blush.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:43PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month