In this unit you will learn: -
how arrays can be used to store a collection of
data items -
how to declare and initialize an array -
how to access and modify array elements -
how to use arrays with methods Suppose you were asked to write a program that
prompts the user to enter 100 integers.
After the user is done entering the values, your program is supposed
to print out the values in reverse order.
How many variables would you need?
It is in fact impossible to construct a solution that does not store
each value in a variable, which means you’ll need 100 variables. If this sounds like a tedious program to
write, try to imagine declaring a million variables. Luckily, Java allows us to deal with a
collection of items as one – an array.
The entire collection of array elements can be represented by a single
variable. To access a particular
element, we simply need to designate its position in the array, or
index. Coupled with loops, arrays
become powerful tools to solve some exciting problems. |