473,490 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

When to use arrays and indexers

I'm totally new to programming and I am wondering; when would be a good time
to use an array or an indexer? I want to know what types of applications
would make good use of arrays or indexers. There seems to be other ways of
doing the jobs of the two and less confusing.

The books I read don't provide good examples of situations when I would need
an array or indexer. I don't really need a definition of them as I already
have that. I just need to know what which well known apps have uses for
arrays and indexer?
Nov 16 '05 #1
1 7556
Hiya. It's good to hear that you're interested in programming. It's loads of
fun, even when you have no idea what you're doing. (You usually get better
results if you know what's going on, though. ;-) )

Arrays are the most basic type of collection that exists. Arrays hold
multiple values, all of which are a single type. They're also the fastest
type of collection, so if speed is important, an array is usually a good
choice. The big disadvantage of arrays is that they can't be resized. Once
you pick the size of an array, you can't change it. So when would you use an
array?

(1) The most common situation for using an array arises when you need to
perform the same operation on a large amount of data, and you know in
advance how large the size of the data will be. For instance, let's suppose
you wanted to triple the value of every integer of a 5-number sequence. If
you'd never heard of arrays, you might decide to do it this way:

// Begin code.
int i1 = 45;
int i2 = 81;
int i3 = 18;
int i4 = 29;
int i5 = 10;

i1 *= 3;
i2 *= 3;
// etc.
// End code.

This is painful and tedious. Programmers hate pain and tedium. Hence,
arrays!

// Begin code.
int[] values = new int[] {45, 81, 18, 29, 10};
for (int i = 0; i < values.Length; i++)
{
values[i] *= 3;
}
// End code.

This is half as much code and much easier to change. For instance, if you
made a mistake in reading your project specifications, and you were really
supposed to multiply by 4, then you only need to change one line. In the
more complicated code, you'd need to change 5 lines.

When would be a bad idea to use an array? In general, any time you don't
know the size of the data is a good choice not to use arrays, or when you'll
need to change the size and add and remove a lot of stuff. Arrays are not
very dynamic.

As for indexers, that's a slightly more advanced topic. Basically, an
indexer is a special way to look up things for a collection. An array has an
integer indexer. Other collections have integer indexers too, and many have
special string indexers and things like that. Here's an example. Let's say
you wanted to make a list of some of the zip codes of the United States. You
want to store these zip codes:

45678 - Splitsville, IN
12345 - New York, NY
22936 - Charlottesville, VA
29130 - Foobar, FL
19381 - Sacramento, CA

If you'd only heard of arrays, you might try something like this:

// Begin code.
// Make space for every possible zip code.
string[] zipCodes = string[100000];
zipCodes[45678] = "Splitsville, IN";
zipCodes[12345] = "New York, NY";
// etc.
Console.WriteLine(zipCodes[45678]); // prints "Splitsville, IN"
// End code.

This works just fine, but notice that you've only used 5 out of the 100,000
elements in the array. The other 99,995 are totally wasted and taking up
memory for no good reason. Clearly an array isn't a great choice here.

A collection that could help you out here is the Hashtable; it has a string
indexer. When you put things in a Hashtable, you only take up as much space
as is needed for one of the items, so you don't have those huge gaps of
space like in the earlier array example. (Actually, that's not quite true,
but this is a simplistic explanation. =) ). Here's what a Hashtable in
action looks like:

Hashtable myHT = new Hashtable();
myHT["45678"] = "Splitsville, IN";
// etc.
Console.WriteLine((string)myHT["45678"]); // prints "Splitsville, IN"

You'll have to experiment to get a feel for the true power of C#. The best
way to learn is through experimentation yourself. Good luck!

- k^2
"mdub317" <md*****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications
would make good use of arrays or indexers. There seems to be other ways of doing the jobs of the two and less confusing.

The books I read don't provide good examples of situations when I would need an array or indexer. I don't really need a definition of them as I already have that. I just need to know what which well known apps have uses for
arrays and indexer?

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
24727
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
2294
by: DKode | last post by:
Ok, Consider the following example: School Class - StudentCollection Property StudentCollection Class : CollectionBase - Add - Item
2
1945
by: Jim | last post by:
How does one determine what indexers are available on a given object? The only way I have found is by looking at the Object Browser. But even then it only gives a simple signature like, this....
5
1909
by: bonk | last post by:
Hello, IL does not have indexers. Infact the c# compiler compiles indexers to Set_Item and Get_Item (or whatever name I choose via the IndexerNameAttribute ). So how does c# (compiler) know...
4
9852
by: tg.foobar | last post by:
i'd like to do the following, but i don't think it's possible. can you help me find a way to do this, or maybe a better way to write the code? I have a list of items that need to be modified...
2
1306
by: Raja Raman Sundararajan | last post by:
Hello guys, I was investigating how one can use the "text indexers" in python and I stumbled across several ones. eg., pylucene I wanted to know how the algorithm of indexers look like. I have...
3
1654
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its...
3
1417
by: daz_oldham | last post by:
Hi everyone I am just trying to find how to implement Indexers... or at least that is what I think I want to implement! Very basically, in my database I have a table of T_Hotels which has...
7
16515
by: Tom Dacon | last post by:
I'm using Reflection to iterate over the properties and fields of an arbitrary object. For non-indexed properties it's pi.GetValue(theObject, Nothing) for VB, or pi.GetValue(theObject, null) for...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.