Sulaimani polytechnic university
Computer Science Institute
Network Department / second stage
Midterm
Array in java
Prepared by: Supervised:
Rezdar Hassan Mr Rebwar Khaled
Introduction
What is an "array"?
Arrays themselves are objects in Java
Creating an array
Arrays Can Be Made of Any Type or Class
[Link]
Array Manipulation
What is an "array"?
A graphic representation
A variable (field, reference)
int answer;
CSStudent aStudent;
An array
int[] answers = new int[4];
CSStudent[] someStudents = new CSStudent[4];
Arrays themselves are objects in Java
Even arrays of primitive data types.
int intArray[];
intArray = new int[4];
float[] fnumbers = new float[8];
CSStudent studentArray[] = new CSStudent[10];
Note the last declaration and instantiation of an array of
CSStudent
objects
Note that array declarations use [], not ()
Question: How many CSStudent objects are created by the
declaration?
Since arrays are objects they inherit all the characteristics of
[Link]
All array objects also have some other characteristics; i.e., each
array has an
associated field named length.
Notice it is a field named length, unlike the instance method
named length()
associated with String objects.
Creating an array
... is like creating an object from a class:
declaration - assigns a name to the reference
instantiation - creates space for the object
initialization - gives the objects values for the first time
Arrays of primitive data types are initialized to 0
int[] grades;
grades = new int[60];
Arrays of Objects are initialized to null
Student[] students;
students = new Student[60];
The students array has been declared and instantiated, but not
yet initialized: no
Student object references have been assigned to the array
elements.
To initialize the array elements, you need to instantiate each
individually:
for (int nextStudent = 0; nextStudent < 10; nextStudent++ ) {
students[nextStudent] = new CSStudent();
Arrays Can Be Made of Any Type or Class
"Declaring a variable of array type does not create an array object or
allocate any space for array components. It creates only the variable
itself, which can contain a reference to an array."
from: Java Language Specification, Gosling, Joy, and Steel, 1996
Arrays are created (instantiated) with new, just like other objects.
Once an array is created, its length never changes.
Question: Any idea why?
Examples
int[] intArray = new int[4]; // elements initially set to 0
CreditCard cards[] = new CreditCard[MAXCARDS];
// elements initially set to null
// notice the [] can be placed with the field name
// or the type; though the latter is "better"
Tip: If you need a colllection’s size to change consider using
[Link] or another collection class instead of arrays
[Link]
/** An Example of some array manipulation
**/
public class ArrayExample {
public static void main (String args[]) {
int numberOfElements = 0;
if ([Link] > 0) {
// Use value from command line
numberOfElements = [Link](args[0]);
}
ArrayExample anExample = new ArrayExample();
[Link](numberOfElements);
// Notice that method calls can be included in other method
// calls: in this case, returning primitive values that will
// be converted to String objects.
[Link]("sum = " + [Link]() +
" average = " + [Link]() );
}
private int[] intArray; // all instance (non static) methods
// have acess to this ’instance’ variable
/** Initialize the array (which will be made big enough to hold
size entries) contents with some numbers */
public void initializeArray(int size) {
intArray = new int[size];
int startValue = size * 3; // pick any number
for (int i = 0; i < [Link]; i++) {
intArray[i] = startValue; // put current number in next slot
startValue = startValue - 2; // and calculate next number
}
}
/** Calculate the sum of the array contents */
public long Sum() {
int index;
int arrayLen;
long sum = 0;
// All arrays have a length field that specifies how many
// elements the array contains
arrayLen = [Link];
// Calculate the sum the values in all array elements
for (index = 0; index < arrayLen; index++) {
sum += intArray[index];
}
return sum;
}
/** Calculate the average of the array contents */
public double Average() {
// Notice that Average calls Sum() to total the values in the
// array, rather than duplicating that calculation here. What
// is going on with the "(double)" ?
double average = (double) Sum() / [Link];
return average;
}
Array Manipulation
In class example of some array manipulation
Write a Java class named [Link]. This class should have the following
methods.
1. public void listArgs( String [] args)
To list out the arguments in an array of Strings
2. public long product( int [] intArray )
To compute the product of the integers in the array intArray
3. public static void main( String[] args ) Should have the following code:
if ([Link] == 0) {
[Link]("usage: Arrays [integers]");
}
else {
Arrays inst = new Arrays();
[Link](args);
int input[] = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
input[i] = [Link](args[i]);
}
[Link]("product = ");
long answer = [Link](input);
[Link](answer);
}
Refrences:
[Link]