So what is an Array anyways? Well, an array is simply a set of numbered elements, where each element contains some data. An easy way to think of an array is to think of it as a list of indexed, or numbered, elements. An array has a name, which is used to refer to that list of elements. Each element in that list has a number (also referred to as an index), which identifies the position of that item within the array.
This is referred to as a one-dimensional array. In a one-dimensional array, each element can contain a single piece of information. Arrays are useful for storing information of the same type in various elements. For example, you could store the names of each person in our class in an array, so each element in our array would hold the name of a different student. Our array would be a collection of student names.
var arrStudents = new Array();
The above statement will create an array with no elements (has a length of 0). This is useful if you do not know ahead of time how many items will be stored in that array. For example, if you have a page where you are allowing the user to enter the names of students, and each time they do, you add that student’s name to the array. You do not know ahead of time whether the user will enter 0, 5, or 30+ student names, so you would just create an array with no elements, and you can add the elements as needed.
Now, if you know ahead of time exactly how many elements you will have in your array, then you can dimension an array to have exactly that many items. For example, if you knew ahead of time that the user was going to enter 10 students, and you wanted to create an array to hold the names of all the students in the class, then you would do the following:
var arrStudents = new Array(10);
The above statement would create an array that has 10 elements, where each element would contain some data. Now, if you wanted to put the names of the students into the respective elements of the array, then for the first student, you would need to reference the first element in the array and put the student’s name in that element. You would do the same for all the other students and their corresponding elements. It is very important that you remember that since arrays are 0-based (meaning that the first element has an index of 0), you need to reference the first item in the array by using an index of 0. Here is how you would fill that 10 element array with student names:
arrStudents = ‘Student A’;
arrStudents = ‘Student B’;
arrStudents = ‘Student C’;
arrStudents = ‘Student D’;
arrStudents = ‘Student E’;
arrStudents = ‘Student F’;
arrStudents = ‘Student G’;
arrStudents = ‘Student H’;
arrStudents = ‘Student I’;
arrStudents = ‘Student J’;
Notice how the tenth student has an index of 9, since the array begins at 0. A common mistake that people make in looking at an array like this is to say that it contains 9 elements, however, it really contains 10.
Also take notice at how we are referencing each individual element of the array. After the array name we enclose the element index in brackets, like this:
arrStudents = ‘Student A’;
That statement just stored the string Student A in the first element of the arrStudents array.
var arrStudents = new Array(‘Student A’, ‘Student B’, ‘Student C’, ‘Student D’, ‘Student E’, ‘Student F’, ‘Student G’, ‘Student H’, ‘Student I’,
This just created an array of strings containing 10 elements, where each element contains the student’s name. Let’s say that you wanted to display in a message box, the third student in the array, how would you do that? You would just need to reference the third element in the array, as follows:
Note again that the statement above is using 2 to reference the third student, because the array is 0-based, so the first student is at index 0, the second student is at index 1, and the third student is at index 2.
The examples above created arrays of strings. However, we could just as easily create an array of integers by doing the following:
var arrAges = new Array(29, 33, 40, 25, 38, 52, 44, 37, 31, 24);
You can find out the number of items in the array by looking at the array’s length property.
alert(‘There are ‘ + arrStudents.length + ‘ elements in the array’);
That statement would display a message box, that displays the following message:
There are 10 elements in the array
Now, you’re probably wondering what happens if you do not specify the size(length) of the array when you declare it, OR even if you do, what happens if you end up needing more elements. For example, maybe you have a textbox on your page (inside of a of course), and an button. This means that the user could add as many students to the array as they want to.
So how would you dimension this array if you don’t know how many elements you will need? Sure, you could dimension the array to be very large arrStudents, but that would be a big waste, especially if you ended up only needing 5 elements, so that is not a good solution. The right approach would be to use something that is referred to as Dynamic Arrays. As you know, the word “Dynamic” means “changing”.
Therefore, what is meant by the name “Dynamic Array” is that the size of the array will change, or grow, as needed in your program. For example, if we had 5 students in an array, and then the user clicked the button, then we would re-dimension the array to add one more element to the array, and then store the name of the student in that new element
arrStudents[arrStudents.length] = ‘John Doe’;
Notice how instead of displaying an actual number inside of the brackets, I used arrStudents.length. Since arrStudents.length returns a number, I can use that in place of a number. The length of an array will always be equal to one more than the last element index (because remember, arrays are 0-based). So using the arrStudents.length in this case works out perfect because the next new element’s index would be the same number as the array’s current length.
Another thing that is useful to know when working with arrays, is how to loop through an array and display the data that is stored in each element. One way to loop through an array is by using a For/Next loop as follows:
// Display the name of the currently indexed student
The above statement is just using a simply For loop that loops through all of the elements in the array, one at a time. Each time through the loop, the counter (x) is incremented by 1. Looking at the alert() function, you can see that it is displaying the array element that is located in spot x.
- Posted: 4 years ago
- Due: 12/02/2016
- Budget: $5