Skip to main content
Monday is APCS after school work/makeup day!

APCS

Go Search
Linda Noss
APCS
Java
VGP
Web Page Design
Consumer Math
Desktop Publishing
See your grade
  

Monday is APCS after school work/makeup day!

Arrays

A set of contiguous memory locations with the same name. Each memory location is accessed via index or subscript number. This is the number in the []. Arrays can hold both primative and object data. The elements in the array are numbered from 0 to n-1 where n is the number of elements in the array.

When working with an array of objects the array itself is an Object so:

  • if the array is an array of primative the array is a reference or a pointer to the first memory location of the contiguous set of memory locations
  • if the array is an array of objects then the array is a list of references to each element of the array or a list of pointers  

Declaring arrays can happen in several ways:

  • dataType[] varName = new dataType[number of elements];
  • dataType varName[] = new dataType[number of elements];
  • datType[] varName = {list the elements seperated by commas};

Once an array is declared the number of elements is that array is fixed or constant. It cannot be changed. To access the  number of elements in an array use the length constant. This gives the number of elements in an array. Remember that the value of length is always one more than the last index of the array. To use the length constant simple append .length the name of the array as in varName.length .

Use the length to avoid ArrayIndexOutOfBounds error. This error happens when you try to acccess an index value less than zero or greater than n - 1 where n is the length of the array.

Arrays of Strings - remember that Strings themselves are arrays of type char. Strings can be transversed one character at a time.

Methods

Pass by Value - this means that a copy of the actual parameter is passed or a new memory location is created with the same value in it. This means that if the value of the parameter is changed in the method than you are changing the copy, not the actual parameter or to say it another way, the memory location of the copy is changed not the original memory location. All parameters, whether primatives or objects are passed into an method by value.

Now we have to remember that Objects are just references. That is to say objects just point to a memory location. They are not in themselves the actual memory location. So, when you are passing an object into a method you are making a copy of the reference or pointer and using this pointer in the method. If the pointer is redirected to another memory location within the method the original pointer, outside the method is not effected.

 Arrays are objects and therefore when using an array as a parameter to a method it is a copy that is passed to the method.

Parallel arrays

Parallel arrays are often used in databases where each column of information is an array. For instance, you may have a form to fill out online that includes your last name, first name, number of children in your family and zip code. Each type of information is an array. So, in this example there would be 4 arrays, LName, FName, Children, zip. Notice that all these arrays will be Strings except for Children, which will be an array of integers. Each array stores the information for everyone that filled in the form, so, LName will be everyone's last name that filled in the form and FName will be everyone's FName that filled in the form. Likewise for Children and Zip.

This group of arrays is called parallel because they will often work in tandem with each other. When I work with the data I will want not just the persons last name but also their first name, number of children and zip. The data for one person will be the same index across the group of four arrays. So if my last name is stored at index location 5 in the LName array then my first name will be also at index 5 in the FName array. Likewise with the Children and Zip array.

Two dimensional arrays

One dimensional arrays can be also called lists becasue it is one column of data. A two dimensional array is often called a table because it is often drawn as a two dimensional object with columns and rows. When referencing one element in a 2-D array you will need two sets of [] after the name of the array. The first pertains to the row and the second pertains to the column. 

Declaring a 2-D array is similar to declaring a one-dimensional array:

int[][] test = new array[5][4];

int test[][] = new array[5][4];

int[][] test = {  {1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20} };

The length constant will give you the value of the number of rows so test.length = 4

The length[0] constant will give you the number of columns so test.length[0] = 5

Technically the computer looks at a two dimesional array as an array of arrays. So, the first reference to the first element in the first array points to an entire array.

What's the difference?

Parallel arrays are seperate one-dimensional arrays that can be of different types. One tends to work with these in a single loop.

A 2-dimensional array is one array all of the same type. We tend to work with these in two loops, one inside the other (called embedded or nested).

 

Hints:

  • If a and b are array a = b does not copy elements from b into a, it just reassigns the reference a to b, so that both a and b refer or point to the same array.