Click here to Skip to main content
15,891,675 members

Survey Results

Should array indexing start at 0 or 1?   [Edit]

Survey period: 7 Mar 2011 to 14 Mar 2011

Old school VB devs and old school C devs know the answer. What's your feeling in this modern, enlightened era?

OptionVotes% 
01,11587.93
115312.07



 
GeneralRe: What is array? PinPopular
Trollslayer8-Mar-11 5:19
mentorTrollslayer8-Mar-11 5:19 
GeneralRe: What is array? Pin
567890123410-Mar-11 0:34
567890123410-Mar-11 0:34 
GeneralRe: What is array? Pin
Kamran Behzad10-Mar-11 12:21
Kamran Behzad10-Mar-11 12:21 
GeneralRe: What is array? Pin
Yusuf10-Mar-11 17:32
Yusuf10-Mar-11 17:32 
GeneralRe: What is array? Pin
Kamran Behzad10-Mar-11 17:35
Kamran Behzad10-Mar-11 17:35 
GeneralRe: What is array? Pin
Yusuf10-Mar-11 17:57
Yusuf10-Mar-11 17:57 
GeneralRe: What is array? Pin
567890123411-Mar-11 2:39
567890123411-Mar-11 2:39 
GeneralRe: What is array? Pin
Mwanzia_M11-Mar-11 1:39
Mwanzia_M11-Mar-11 1:39 
Don't leave this site coz its invaluable. Yes there mean people out there but they are sadly here to stay.
An array is just a variable that stores more than one value at the same time. I assume you know what a variable is so take an int variable as an example. An int variable can only store one value at any given time so it can't hold a collection of values, say the total number of students in each class of a school. This is where an array comes handy.
An array usually stores values of only one type so you can't mix integers with strings unless the array has been set to contain type Object.
An array stores these items in a column like manner. So we have the first item at the top, the second item follows and so on. Each item has index that indicates its position in the array. The first item usually occupies index 0, the second index 1, the third index 2 and the list continues depending on how many items the array has been set to hold.

To declare an array that stores five items of type integer in c# code proceed as follows;

int[] noOfStudents = new int[5];
we now have an array that explicitly stores only values that are of type integer (whole numbers only). To set the first item to hold 20 use the following code:

noOfStudents[0]=20;

since the array has been set to hold a maximum of 5 values, the last index (the position an item is found at) is therefore 4 since we start counting from index 0.
To get an item in the third index proceed as follows:

int value=noOfStudents[2];

remember we start counting from index 0. If you try to set a value at index 45 and the array has been set to hold 20 values then you will get an error/exception. Likewise if you try to set a value to an element that is beyond the maximum possible items that the array has been set to hold then you will get an error that may state (out of bounds exception). Read more on arrays coz this was just an introduction. Hope it helped you.
GeneralRe: What is array? Pin
Emilio Garavaglia13-Mar-11 21:52
Emilio Garavaglia13-Mar-11 21:52 
GeneralRe: What is array? Pin
Mwanzia_M17-Aug-13 8:28
Mwanzia_M17-Aug-13 8:28 
GeneralRe: What is array? Pin
Mwanzia_M17-Aug-13 8:33
Mwanzia_M17-Aug-13 8:33 
GeneralRe: What is array? Pin
Kamran Behzad10-Mar-11 12:25
Kamran Behzad10-Mar-11 12:25 
GeneralRe: What is array? Pin
#realJSOP8-Mar-11 10:09
mve#realJSOP8-Mar-11 10:09 
GeneralRe: What is array? PinPopular
567890123410-Mar-11 0:26
567890123410-Mar-11 0:26 
GeneralRe: What is array? Pin
Indivara8-Mar-11 13:44
professionalIndivara8-Mar-11 13:44 
GeneralRe: What is array? Pin
567890123410-Mar-11 0:37
567890123410-Mar-11 0:37 
GeneralRe: What is array? Pin
Indivara10-Mar-11 10:13
professionalIndivara10-Mar-11 10:13 
GeneralRe: What is array? Pin
L Viljoen8-Mar-11 20:28
professionalL Viljoen8-Mar-11 20:28 
GeneralRe: What is array? Pin
567890123410-Mar-11 0:29
567890123410-Mar-11 0:29 
AnswerRe: What is array? Pin
L Viljoen10-Mar-11 1:09
professionalL Viljoen10-Mar-11 1:09 
GeneralRe: What is array? Pin
567890123410-Mar-11 1:25
567890123410-Mar-11 1:25 
GeneralRe: What is array? Pin
L Viljoen10-Mar-11 1:18
professionalL Viljoen10-Mar-11 1:18 
GeneralRe: What is array? Pin
CPallini8-Mar-11 22:37
mveCPallini8-Mar-11 22:37 
GeneralRe: What is array? Pin
567890123410-Mar-11 0:32
567890123410-Mar-11 0:32 
GeneralRe: What is array? Pin
Pravin Patil, Mumbai10-Mar-11 20:52
Pravin Patil, Mumbai10-Mar-11 20:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.