Difference between revisions of "Entering and Using Array Elements"

From CompSciWiki
Jump to: navigation, search
(Expanded the introduction)
m (Fixed excessive spacing)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Template:1010Topic
 
{{Template:1010Topic
 
|Chapter_TOC=[[Arrays]]
 
|Chapter_TOC=[[Arrays]]
|Previous=[[User-Defined Methods]]
+
|Previous=[[Creating Arrays]]
|Next=[[Entering and Using Array Elements]]
+
|Next=[[Out of Bounds and One off Errors]]
 
|Body=
 
|Body=
 
==Introduction==
 
==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.
 
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==
 
==Modifying Array Elements==
Line 16: Line 15:
 
array[i] = a;
 
array[i] = a;
 
}}
 
}}
where '''array''' is the array, '''i''' reffers '''i''' th element inside the array('''i''' th index), and '''a''' is the thing you wish to set the '''i'''th element in '''array''' to. (Note: this will overwrite the current ith element, and will '''not''' change or effect the order of the array)
+
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 '''i'''th element in '''array''' to. (Note: this will overwrite the current ith element, and will '''not''' change or effect the order of the array)
 
+
See: [[#Considerations for inserting and modifying array elements|Considerations for inserting and modifying array elements]]
+
 
+
  
 
==Inserting Array Elements==
 
==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.)
 
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|Modifying array elements]]", substituing the position you wish to insert the element into the array for '''i'''.
+
Once you have the position where you wish to insert the element selected, you may copy the step(s) described in "[[#Modifying array elements|Modifying array elements]]", substituting the position you wish to insert the element into the array for '''i'''.
  
 
Example:
 
Example:
Line 31: Line 27:
 
array[i] = a;
 
array[i] = a;
 
}}
 
}}
 
See: [[#Considerations for inserting and modifying array elements|Considerations for inserting and modifying array elements]]
 
 
  
 
==Considerations For Inserting And Modifying Array Elements==
 
==Considerations For Inserting And Modifying Array Elements==
Line 39: Line 32:
 
#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.) [[Out of Bounds and One off Errors|See: Out of Bounds and One off Errors]]
 
#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.) [[Out of Bounds and One off Errors|See: Out of Bounds and One off Errors]]
 
#You cannot change the order of the items inside an array without manually shifting them by copying them.
 
#You cannot change the order of the items inside an array without manually shifting them by copying them.
#All array indicies must be between 0 and array.length - 1 (inclusive) (array.length is the last element inside the array + 1) [[Out of Bounds and One off Errors|See: Out of Bounds and One off Errors]]
+
#All array indices must be between 0 and array.length - 1 (inclusive) (array.length is the last element inside the array + 1) [[Out of Bounds and One off Errors|See: Out of Bounds and One off Errors]]
 
#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.)
 
#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==
 
==Accessing Elements Within An Array==
Line 61: Line 53:
 
a = array[1];
 
a = array[1];
 
if (a == 3)
 
if (a == 3)
System.out.println("the second element in the array is " + a);
+
{
 +
    System.out.println("the second element in the array is " + a);
 +
}
 
}}
 
}}
 
with
 
with
Line 67: Line 61:
 
|Code=
 
|Code=
 
if (array[1] == 3)
 
if (array[1] == 3)
System.out.println("the second element in the array is " + array[1]);
+
{
 +
    System.out.println("the second element in the array is " + array[1]);
 +
}
 
}}
 
}}
 
See: [[#Considerations for using array elements|Considerations for using array elements]]
 
 
  
 
==Considerations For Using Array Elements==
 
==Considerations For Using Array Elements==
 
When inserting elements into arrays, there are a few things to consider:
 
When inserting elements into arrays, there are a few things to consider:
#All array indicies must be between 0 and array.length - 1 (inclusive) (array.length is the last element inside the array + 1) [[Out of Bounds and One off Errors|See: Out of Bounds and One off Errors]]
+
#All array indices must be between 0 and array.length - 1 (inclusive) (array.length is the last element inside the array + 1) [[Out of Bounds and One off Errors|See: Out of Bounds and One off Errors]]
 
#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.)
 
#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==
 
==Extra array methods/functions==
Line 87: Line 79:
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
a = array.'''length''';
+
a = array.length;
 
}}
 
}}
 
Where '''a''' is the destination for the array length, and '''array''' is the array you wish to obtain the length for.
 
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====
 
====Calculating The End Of An Array====
 
The last element in the array can be calculated by '''array.length - 1'''.
 
The last element in the array can be calculated by '''array.length - 1'''.
 
===String and character array functions===
 
===String and character array functions===
 
  
 
====Converting A Character Array To A String====
 
====Converting A Character Array To A String====
Line 115: Line 105:
 
}}
 
}}
 
Note: that this will print "U of M" to the screen.
 
Note: that this will print "U of M" to the screen.
 
  
 
====Converting A String To A Character Array====
 
====Converting A String To A Character Array====
Line 128: Line 117:
 
a = b.toCharArray();
 
a = b.toCharArray();
 
for (i = 0; i < a.length; a++)
 
for (i = 0; i < a.length; a++)
System.out.print(a[i]);
+
{
System.out.print("\n"); /* ends the line that you are printing */
+
    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.
 
Note: This will print "U of M" to the screen.
  
 
==Summary==
 
==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.
 
}}
 
}}

Latest revision as of 21:15, 8 December 2011

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