What is meant by the name dynamic array

Assignment Help JAVA Programming
Reference no: EM13973010

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.

As I mentioned earlier, the position of an element in an array is referred to as its index. An array has a lower bound and an upper bound. The lower bound is simply the index of the first element in the array. The upper bound is the index of the last element in the array. By default, the lower bound of an array is 0. Therefore, in the visual example of an array above, "Element 1" would have an index of 0, "Element 2" would have and index of 1, and so on up to "Element 10" which would have an index of 9. In this example, the lower bound of this array is 0, and the upper bound of this array is 9, and there are 10 elements in the array. In JavaScript, the number of elements in an array is referred to as its length.

To create an array in JavaScript, you need to use the new operator, and then assign the result to a variable, as follows:
var arrStudents = new Array();

Note: Remember that JavaScript is Case-Sensitive!

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[0] = 'Student A';
arrStudents[1] = 'Student B';
arrStudents[2] = 'Student C';
arrStudents[3] = 'Student D';
arrStudents[4] = 'Student E';
arrStudents[5] = 'Student F';
arrStudents[6] = 'Student G';
arrStudents[7] = 'Student H';
arrStudents[8] = 'Student I';
arrStudents[9] = '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[0] = 'Student A';
That statement just stored the string Student A in the first element of the arrStudents array.

If you know ahead of time, the values that you want to store in each element, JavaScript has a cool feature, which will allow you to pass, as parameters, the data to store in each element, and then it will automatically create and populate the array for you. Here is an example of that:
var arrStudents = new Array('Student A', 'Student B', 'Student C', 'Student D', 'Student E', 'Student F', 'Student G', 'Student H', 'Student I',
'Student J')

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:
alert(arrStudents[2]);

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 <form></form> of course), and an <Add Student to Array> 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[1000], 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 <Add Student to Array> 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:
for(x=0; x<arrStudents.length; x++)
{
// Display the name of the currently indexed student
alert(arrStudents[x]);
}

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.

Hopefully, this has given you a good overview of what an Array is, what it can be used for, and how to use it. Arrays are very simple to create and use, and they come in very handy when you start incorporating greater functionality into your pages. I have created an example that demonstrates the use of arrays. It is basically the scenario that I was using as an example in this lecture, where the user can enter a student's name, and then add it to the array. They can enter as many names as they want to, and then they can see a list of all the student names that they have entered. Please take a look at the source code, and see if you can follow along as to what the JavaScript is doing:

Reference no: EM13973010

Questions Cloud

What domain naming structure would you suggest : How would you design the logical structure of Active Directory for the Rough Country Miles of Alaska, and what domain naming structure would you suggest?
What domain naming structure would you suggest : How would you design the logical structure of Active Directory for the Rough Country Miles of Alaska, and what domain naming structure would you suggest?
What''s the relationship between a domain name and a web host : When I was learning HTML, I had a hard time understanding a lot of the terminology. I think this article does a good job of explaining web hosting, domain names and file names. http://support.hostgator.com/articles/hosting-guide/what-is-the-differ..
Explore at least three major disadvantages to individuals : Explore at least three major disadvantages to individuals or social groups of mobile and pervasive technology and their potential consequences.
What is meant by the name dynamic array : 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[1000], but that would be a big waste, especially if you ended up only needing 5 elements, so ..
What is the energy of the spring-mass system : What is the energy of the spring-mass system at the initial position of the mass? What is the energy of the spring-mass system when the mass first passes through the equilibrium position
Write a program that asks you to enter your height in inches : Write a program that asks you to enter your height in inches and then displays your height in centimeters. Or, if you prefer, ask for the height in centimeters and convert that to inches.
Analyze the types of digital criminals and hackers : Determine one (1) additional theory that a researcher could use to explain the cause of digital-crime and non-digital crime. Include one (1) example for each crime in question to support your response.
Calculate the right angled triangle : Write an alogorithm to calculate the right angled triangle

Reviews

Write a Review

JAVA Programming Questions & Answers

  Program that uses a library database of books

Write a Java program that uses a library database of books and patron data, as described in Exercise R22.2. Patrons should be able to check out and return books.

  Write and describe the definition of what composition is

write and explain the definition of what composition is and how it is useful in writing an object-oriented program.

  Need of arrays and list

why do we need arrays and list and why are they important?

  Dijikstra for undirected graph using simple scheme with

dijikstra for undirected graph using simple scheme with array and fibonacci heap and compare the performanceresults

  That uses a library database of books

Write a Java program that uses a library database of books and patron data, as described in Exercise R22.2. Patrons should be able to check out and return books

  Write an expression that concatenates the string variable

Write an expression that concatenates the string variable suffix onto the end of the string variable prefix.

  Write java class that contains 4 instance methods

Write an application class which only contains a main method. This class should create an instance of your CV class and should, through a sequence of method calls, cause your CV information to be displayed on the screen (see Figure 1).

  Product maintenance with a database

Use a class named ProductDB that's in the music.data package to add, update, and delete the products in the Product Maintenance application.

  Write a java program to perform matrix multiplication

In this project you need to write a java program called to perform matrix multiplication and

  How large a value can be stored in an integer variable

Most programming languages have a built-in integer data type. Normally this representation has a fixed size, thus placing a limit on how large a value can be stored in an integer variable

  Write a program called product1ton

Write a program called Product1ToN to compute the product of integers 1 to 10 (i.e., 1×2×3×...×10). Try computing the product from 1 to 11, 1 to 12, 1 to 13 and 1 to 14.

  Prepare a java program draws a chess board

Write a Java program "chess.java" that draws a chess board with 8 x 8 squares, half of the squares are filled black and are interleaved with the white squares.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd