Arrays

advertisement
Arrays
What is an array
• An array is used to store a collection of
data
• It is a collection of variables of the same
type
Declaring Array Variables
dataType [] name;
// preferred way
double[] myList;
dataType name [];
// works but not preferred
double myList [];
Arrays
• An array is an ordered list of values
Each value has a numeric index
The entire array
has a single name
0
scores
1
2
3
4
5
6
7
8
9
79 87 94 82 67 98 87 81 74 91
An array of size N is indexed from zero to N-1
This array holds 10 values that are indexed from 0 to 9
4
Arrays
• A particular value in an array is referenced using
the array name followed by the index in brackets
• For example, the expression
scores[2]
refers to the value 94 (the 3rd value in the array)
• That expression represents a place to store a
single integer and can be used wherever an
integer variable can be used
5
Arrays
• The values held in an array are called array elements
• An array stores multiple values of the same type (the
element type)
• The element type can be a primitive type or an object
reference
• Therefore, we can create an array of integers, or an
array of characters, or an array of String objects, etc.
• In Java, the array itself is an object
• Therefore the name of the array is a object reference
variable, and the array itself must be instantiated
6
Creating Arrays
You can create an array by using the new operator
with the following syntax
dataType [] name = new dataType [arraySize];
The above statement does two things:
It creates an array using new dataType[arraySize];
It assigns the reference of the newly created array to
the variable name.
Declaring an array variable, creating an array, and
assigning the reference of the array to the
variable can be combined in one statement, as
shown below:
dataType [] name = new dataType [arraySize];
dataType [] name = {value0, value1, ..., valuek};
int [] age = {10, 15, 20, 5};
int [] age = new int [4];
Declaring Arrays
• Some examples of array declarations:
double[] prices = new double[500];
boolean[] flags;
flags = new boolean[20];
char[] codes = new char[1750];
9
Bounds Checking
• Once an array is created, it has a fixed size
• An index used in an array reference must specify a valid
element
• That is, the index value must be in bounds (0 to N-1)
• The Java interpreter throws an
ArrayIndexOutOfBoundsException if an
array index is out of bounds
• This is called automatic bounds checking
10
Bounds Checking
• For example, if the array codes can hold 100 values,
it can be indexed using only the numbers 0 to 99
• If count has the value 100, then the following
reference will cause an exception to be thrown:
System.out.println (codes[count]);
• It’s common to introduce off-by-one errors when
problem
using arrays
for (int index=0; index <= 100; index++)
codes[index] = index*50 + epsilon;
11
Example
myList[0]
double[] myList = new double[4];
myList[1]
0.0
myList[2]
0.0
myList[3]
0.0
double[] myList = {1.9, 2.9, 3.4, 3.5};
myList.length will be 4
myList[0]
1.9
myList[1]
2.9
myList[2]
3.4
myList[3]
0.0
3.5
Processing Arrays:
• When processing array elements, we often
use either for loop or foreach loop because all
of the elements in an array are of the same
type and the size of the array is known.
Create an array
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
Print an array
// Print all the array elements
for (int i = 0; i < myList.length; i++)
{
System.out.println(myList[i] + " ");
}
Sum of all elements in an array
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++)
{
total += myList[i];
}
System.out.println("Total is " + total);
Largest element in an array
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++)
{
if (myList[i] > max)
max = myList[i];
}
System.out.println("Max is " + max);
}
}
Write a program to reverse the elements in
an array
public static void reverse(int[] arrayOne) {
for(int i = 0; i < arrayOne.length; i++)
{
int temp = arrayOne[i];
arrayOne[i] = arrayOne[arrayOne.length - i - 1];
arrayOne[arrayOne.length - 1] = temp;
System.out.println("myArray[" + i + "] = " + arrayOne[i]);
}
Download