Escolar Documentos
Profissional Documentos
Cultura Documentos
(3 hrs)
Chapter Objectives:
– Discover what an array is and its use and importance.
– Create, declare and initialize primitive and object reference arrays.
– Introduce the length attribute for arrays in determining the number of elements in an array.
– Introduce the enhanced for-loop capability of version 5.0
– Create a rectangular and a non rectangular multi-dimensional array.
– Write a code that copies an array value from one array to another.
Well, to start with…arrays is a data structure in Java that is used to group common data
together. It is a homogeneous collection, which means that the data type of one element is the
same data type of the other elements. It has a fix size and once the size has been set, it cannot
be changed to accommodate other elements. There is also a heterogeneous array, but we will
not discuss heterogeneous arrays in this chapter, lets discuss that on the next chapter.
Let’s say your instructor gave you five short quizzes for your midterms, instead of
declaring them as:
This means that you are declaring and creating an array of quizzes of type integer that
can have 5 integer values. Let ‘s now determine the parts of the given array:
data type
int quiz[];
array name
Sometimes you might need more that one array. Consider this statement:
Once the [] notation follows the type, all variables in the declaration are arrays,
otherwise, the [] must follow after the variables. You can declare arrays of any type, either
primitive data type or of reference type. Arrays in Java are treated as objects therefore we can
construct our arrays by using the new keyword.
You have now created an array of quizzes and an array of seatwork of type integer
having 5 and 7 elements respectively.
The Java programming language allows creation of arrays with initial values:
String names[] =
{
new String ("Lawrence"),
new String ("Dean"),
new String ("Ella"),
new String ("Michelle")
};
Upon the creation of arrays initial values are automatically assigned to them. Elements of
integral arrays are all set to zero. Boolean arrays are all set to false. And arrays of objects, such
as strings, are all set to null.
As you take a look in line 1 an array of integer was created but there are no initial values
assigned to it. Nevertheless since it is an array of integral value zero will be assigned to each
element. As the code loop through the for loop body it will give us an output of:
Unlike the fundamental (atomic) types (char, int, float, double, etc.), arrays are composites. It
consists of several values. Consequently, many of the operations used on fundamental types do
not work as expected with arrays. Consider the following array operations:
Array Limits
All arrays begin with index 0. Let’s take a look at this example:
String coffee[] =
{“Barako”,
“Espresso”,
“Java”,
“Mocha chino”};
coffee[]
“Barako” 0
“Espresso” 1 index
“Java” 2
“Mocha chino” 3
The index of array coffee[] starts with 0 and ends with 3. So if you want to access
Java, you would do it like this:
I love Java
How about if you would like to access the last element of your array without you knowing
exact number of elements in your array? Since arrays in Java are objects, it has a length
attribute that returns the actual length of your array.
To access the last element of any array you can do it this way:
Using an integer variable i as an index, you can access all the elements of array coffee
from the first element until the last element (coffee.length-1).
The enhanced for loop is new for version 5 also called as the for-each loop. The for
loop created in the previous example can be rewritten this way:
21 String coffee[] =
22 {“Barako”,
23 “Espresso”,
24 “Java”,
25 “Mocha chino”};
26
27 System.out.println (“The coffee lovers choices are: “)
28
29 for (String c : coffee)
30 {
31 System.out.println (c);
32 }
Copying Arrays
If you need to put the elements of one array to another array, there's a convenient
method for this. We can use the System.arraycopy() method. Here's an example:
Once compiled and run, you will have the following output:
The first parameter is the original method, followed by the starting point then the third one
is the destination array, the four argument is the starting point where you will start putting the
copied elements and the last argument is the number of elements to be copied.
Some of you may ask, what is the String args[] for? It is actually Java's way to
accept inputs via command-line.
After compiling it, you can run it by providing command line arguments like this:
The Strings that comes after the file name are command line arguments that are passed
to the main method's array of String parameter. And each String is actually an element of the
args[] array, you just need to separate each String with a space. But if incase you would like to
one
two two
three
The argument list are not dynamically accepted. So we can rewrite our code this way:
But with the use of the enhanced for loop, it can be rewritten this way as well.
But we must remember that when we pass arguments via command line, we are passing
String values, even if we pass integers or other data types. As a rule, all arguments list passed
via command line are Strings.
So, let's say, we would like to pass integer values via command line and then compute
for the sum and the average of the integer parameters, how are we suppose to do this?
When we compile and run this code with the following command line values, you will
have the following output:
java CommandLineArgs4 1 22 3 4 5 6 7
Sum: 48
Ave: 6.857142857142857
Two-dimensional Arrays
In Java, a two-dimensional array is also known as an array of array. You can have a
rectangular "normal" two-dimensional array or a non-rectangular array. Let's first take a look at
the "normal" two-dimensional array. Consider this example:
What happens here is that you have declared an array of arrays (two-dimensional array)
such that objectArray[0] is an array of object Integer, and objectArray[2] which is an
array of object Long and objectArray[3] is of type Double.
You can also create a two-dimensional array having one common data type (whether
primitive or reference) in all its column and row elements. Take a look at this example:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Non-Rectangular Arrays
Since you are allowed to declare an array of arrays, you can also create non-rectangular
array of arrays. This means you can declare arrays this way.
As you notice array irregularArray[0][] can only hold 3 integer values while array
irregularArray[1][] can hold up to 5 integer values. Even though it is a non-rectangular
array their respective default values will still be assigned upon creation.
0 1 2 3 4
0 0 0 0
1 0 0 0 0 0
2 0
3 0 0 0 0
This type of array declaration is tedious that is why the rectangular array is commonly
used.