College of Science
Department of Computer
Programing Fundamentals (Java)
First Stage
Java Arrays
Prepared by:
Ari M.Saeed
2018 - 2019
Arrays
• An array is a container object that holds a fixed number of
values of a single type.
• An array is a way to hold more than one value at a time.
• Variables hold only one value.
• You can make an array of any type int, double, String, etc..
• The length of an array is established when the array is
created.
Arrays
• Each item in an array is called an element.
• Each element is accessed by its numerical index.
• The index starts at zero and ends at length-1.
• For example, the 4th element would therefore be accessed
at index 3.
Arrays
Advantage of Array:
• Code Optimization: It makes the code optimized, we can
retrieve or sort the data easily.
• Random access: We can get any data located at any index
position.
Disadvantage of Array:
Size Limit: We can store only fixed size of elements in the
array. It doesn't grow its size at runtime.
Array Types
There are two types of array:
• Single Dimensional Array
• Multidimensional Array ( array of arrays)
Declare Syntax of Single Dimensional Array
dataType[] array'sName; (or)
dataType []array'sName; (or) // this form is discouraged
dataType array'sName[]; // this form is discouraged
Single Dimensional Array
int[] firstArray = new int[3]; //declaration and instantiation
firstArray[0] = 10; //initialization
firstArray[1] = 20;
firstArray[2] = 70;
System.out.println("Element at index 0: " + firstArray[0]);
System.out.println("Element at index 1: " + firstArray[1]);
System.out.println("Element at index 2: " + firstArray[2]);
***************************************
The output from this array is:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 70
Tips: You would probably use one of the supported looping constructs to iterate through each
element of the array.
Single Dimensional Array
Declaration, Instantiation and Initialization of Array:
double[] vertical = { 1.1, -10.5, 3.7, 9.98, 7.4, 9.9, -2.5 };
The length of the array is determined by the number of values
provided between braces and separated by commas.
int arraySize[]={33,98,74,65};
System.out.println(arraySize.length);
The output is:
4
Multidimensional Array
Array of arrays (also known as a multidimensional array) by
using two or more sets of brackets.
Each element, therefore, must be accessed by a corresponding
number of index values.
Syntax to declare multidimensional array:
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or) // this form is discouraged
dataType arrayRefVar[][]; (or) // this form is discouraged
dataType []arrayRefVar[]; // this form is discouraged
Multidimensional Array
Instantiate Multidimensional Array:
int[][] arr = new int[3][3]; //3 row and 3 column
Data is stored in row and column based index.
Initialize multidimensional array:
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Multidimensional Array
Example:
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};
System.out.println(names[0][0] + names[1][0]);
System.out.println(names[0][2] + names[1][1]);
The output is:
Mr. Smith
Ms. Jones
Tips: //The following code prints the array's size to standard output
System.out.println(names.length);
Copying Arrays
public static void arraycopy(Object src, int srcPos, Object
dest, int destPos, int length)
The two Object arguments specify the array to
copy from and the array to copy to.
The three int arguments specify:
• The starting position in the source array.
• The starting position in the destination array.
• The number of array elements to copy.
Copying Arrays
char[] copyFrom = {'r', 'u', 'n', 'i', 'v', 'e', 'r', 's', 'i', 't', 'y', 'g' };
char[] copyTo = new char[10];
System.arraycopy(copyFrom, 1, copyTo, 0, 10);
System.out.println(copyTo);
The output from this program is:
university
Exercises 1
• What is the index of Binar in the following array?
String[] names = {“Kurdistan", “Hunar", "Binar", “Shamal", “Hardi",
“Diare"};
Write an expression that refers to the string Binar within the array.
What is the value of the expression names.length?
What is the index of the last item in the array?
What is the value of the expression names [4]?
• Write a program to add the odd numbers in the following Excel table and
testing the value by remainder operator with 2 .
Exercises 2
Do we have any error in this program? Yes or No, if Yes, correct to
show “Saturday” String.
class ArrayDemo{
public static void main(String[] args){
String[] array1 = {"Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday", "Saturday"};
String[] array2 = new String[5];
System.arraycopy(array1, 0, array2, 0, 7);
System.out.println(array2[6]);
}
}
References
Java in a Nutshell, 6th Edition, By David Flanagan,2015
Introduction to programing with java, John S.Dean,2008
http://java.sun.com/docs/books/tutorial
http://www.vogella.com
http://journals.ecs.soton.ac.uk/java/tutorial
15