Braintique.com header
Left Navigation Bar

Creating an Array

Let’s start with single-dimensional arrays. The process of creating an array is a three-step dance (although these steps can be combined, as we’ll see in a moment):

  • The array must be declared.
  • Next, it is instantiated.
  • Finally, it is initialized with values.

To declare an array, follow a type with square brackets and continue with the array’s identifier. For example, you could declare an integer array numbers and a string array names as follows:

int [] numbers; // declares integer array
string [] names; // declares string array

To instantiate the array, as you’d expect, the new keyword is used. The statement

numbers = new int [3];

instantiates a new three-element, zero-based array with the previously declared variable numbers. (The elements are numbers[0], numbers[1], and numbers[2].).

The two statements can be combined into one, so that you can instantiate while you declare:

int [] numbers = new int[3];

At this point, you should know that the three elements of the integer array numbers have been initialized to 0. (Arrays of reference-type elements are initialized to a null reference.)

As it turns out, you can initialize the array at the same time as you declare and instantiate it by placing the initial values within curly braces. Here are one-step examples that declare, instantiate, and initialize an integer array and a string array:

int [] numbers = new int[] {3,1,4};
string [] names = new string[] {"Tom", "Dick", "Harry"};

If you really prefer to be terse, you can also leave off the new part of the statement (once again, assuming you’ve provided initial values). The compiler is smart enough to know that it is implied. So here’s the shortest way to declare, instantiate and initialize these two arrays:

int [] numbers = {3,1,4};
string [] names = {"Tom", "Dick", "Harry"}

Moving on, let’s try a little example of creating and using an array. Let’s suppose we want an array to store the first seven numbers in the Fibonacci series, which comes up in art, nature, mathematics, and mysticism. Here’s the shorthand way to create that array and stuff the right values into it:

int [] fibArray = new int[7] {1,1,2,3,5,8,13};

Let’s say, instead, that we are fond of iteration. As you probably know, the first two elements of the Fibonacci series are 1; after that, the element n in the series is equal to the sum of the elements (n – 1) and (n – 2).

First, we can declare and instantiate the array with seven zero-based elements:

int [] fibArray = new int[7];

Next, we can assign the first two values in the series.

fibArray[0] = fibArray[1] = 1;

Finally, we can use iteration to assign the rest of the values in the array:

for (int i = 2; i < 7; ++i)
fibArray[i] = fibArray[i - 1] + fibArray[i - 2];

You can use a message box to display an element of the array to make sure that this has all worked correctly, as shown in the listing below.

Listing: Creating an Integer Array and Displaying an Element

private void btnCreate_Click(object sender, System.EventArgs e) {
// int [] fibArray = new int[7] {1,1,2,3,5,8,13};
int [] fibArray = new int[7];
fibArray[0] = fibArray[1] = 1;
for (int i = 2; i < 7; ++i)
fibArray[i] = fibArray[i - 1] + fibArray[i - 2];
string fifthFib = fibArray[4].ToString();
MessageBox.Show
("The fifth number in the Fibonacci series is " +
fifthFib, "Arrays", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
Arrays

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write