Entering and Using Array Elements

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Arrays


Introduction

Arrays can last the life of a program, which is why they need to have the ability to be accessed and changed to keep their data current. This section will cover both accessing and modifying items in an array.

Modifying Array Elements

For arrays to be of any use to us, we must be able to change and add items to arrays. Since the size of arrays are of a static nature (the size doesn't change), I shall first talk about how to change the elements inside arrays.

The elements inside arrays can be changed using the following form:

 array[i] = a; 

where array is the array, i refers i th element inside the array(i th index), and a is the thing you wish to set the ith element in array to. (Note: this will overwrite the current ith element, and will not change or effect the order of the array)

Inserting Array Elements

To insert an array element, you must have space within the array which is unused or can be overwritten (e.g., This can be kept track of by a variable used as a counter to keeps track of where you last inserted an element into an array.)

Once you have the position where you wish to insert the element selected, you may copy the step(s) described in "Modifying array elements", substituting the position you wish to insert the element into the array for i.

Example:

 array[i] = a; 

Considerations For Inserting And Modifying Array Elements

When inserting elements into arrays, there are a few things to consider:

  1. Arrays have finite sizes (you cannot grow an array, however, you can make a new array and copy the items over to the new array.) See: Out of Bounds and One off Errors
  2. You cannot change the order of the items inside an array without manually shifting them by copying them.
  3. All array indices must be between 0 and array.length - 1 (inclusive) (array.length is the last element inside the array + 1) See: Out of Bounds and One off Errors
  4. Whenever you insert or modify an array, you loose the element that you overwrote (i.e., the element that is stored in the index where you did the insertion of modification.)

Accessing Elements Within An Array

To access elements within an array, you can use the following template:

 a = array[i]; 

where a is the item you wish to access/obtain, array is the array you wish to get the item from, and i is the index/position you want to obtain the item from.

Note: you can replace any usage of a for array[i] since they are equal, as well i can be a variable of integer

Example:

Replace

 a = array[1];
if (a == 3)
{
     System.out.println("the second element in the array is " + a);
} 

with

 if (array[1] == 3)
{
     System.out.println("the second element in the array is " + array[1]);
} 

Considerations For Using Array Elements

When inserting elements into arrays, there are a few things to consider:

  1. All array indices must be between 0 and array.length - 1 (inclusive) (array.length is the last element inside the array + 1) See: Out of Bounds and One off Errors
  2. Whenever you insert or modify an array, you lose the element that you overwrote (i.e., the element that is stored in the index where you did the insertion of modification.)

Extra array methods/functions

Playing with array limits

Retrieving the length of an array

To retrieve the length of an array, you can use the array's length method. This can be accessed using the following template:

 a = array.length; 

Where a is the destination for the array length, and array is the array you wish to obtain the length for.

Calculating The End Of An Array

The last element in the array can be calculated by array.length - 1.

String and character array functions

Converting A Character Array To A String

You can convert a character array to a String using the string's constructor. This can be done using this template:

 char[] a;
String b;

a = new char[6];
a[0] = 'U';
a[1] = ' ';
a[2] = 'o';
a[3] = 'f';
a[4] = ' ';
a[5] = 'M';
'''b = new String(a);'''
System.out.println(b); 

Note: that this will print "U of M" to the screen.

Converting A String To A Character Array

You can convert a String to a character array using the string's constructor. This can be done using the toCharArray method:

 char[] a;
int i;
String b = "U of M";

a = b.toCharArray();
for (i = 0; i < a.length; a++)
{
     System.out.print(a[i]);
     System.out.print("\n"); /* ends the line that you are printing */
} 

Note: This will print "U of M" to the screen.

Summary

After seeing the basic operations on arrays (accessing, accessing length, retrieving the last value, converting char arrays to Strings, and converting Strings to char arrays), you are ready to move on to Out of Bounds and One off Errors to understand what happens if you try to access elements not in the array.

Previous Page: Creating Arrays Next Page: Out of Bounds and One off Errors