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

  Test plan for both unit testing and integration testing

Do a test plan for both unit testing and integration testing, justification why you need this test creating the test case for each individual test must use java language in netbeans IDE to perform each test, screen capture one example to run the ..

  Conduct an internet search for javadb tutorial you might

please write a 200-400 word response to the following questionq1. conduct an internet search for javadb tutorial. you

  Write a program that reads a series of test scores

Write a program that reads a series of test scores from a recent test (integers in the range 0 to 100) until a sentinel value is read. A sentinel value is the value used to determine when to stop. The program then prints the following statistics f..

  Prepare demo program that implements all of above method

Specify, design, and implement a class that can be used in a program that simulates a combination lock.

  Assume that you are developing a java program which uses

suppose that youre developing a java program that uses decimal numbers and youd like to control the formatting of your

  Explain gui technique message dialog box

Write an application called Circle.java that inputs from the user the radius of a circle as an integer and prints the circle's diameter, circumference, and area. Use the example program and GUI technique message dialog box shown in the Week 1 Lect..

  Java program on eclipse

Using a while( true ) loop,and using the upper case alphabeth from Z to A,print the lower case alphabeth and its corresponding ascii values.You must terminate/exit/break this loop once you process the last letter (A).

  This project mainly focuses on explaining your

this project focuses on demonstrating your understanding of java collections. before attempting this project be sure

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  What are the concerns about how java implements arrays

What are the concerns about how Java implements arrays? What are your concerns? In Java, what is the starting index of an array

  Setting up the form page

Download and save the attached comment CGI mailer script form-mail2.pl to your server's cgi-bin directory, and change the permissions on the script to make it executable (not writable).

  Demonstrate the singleton pattern

Write a Java program (non-GUI preferred) to demonstrate the Singleton pattern. The key parts of the singleton pattern are: A private static variable to store the single instance called the singleton A public static method for callers to get a referen..

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