Java Arrays: College of Science Department of Computer Programing Fundamentals (Java) First Stage
Java Arrays: College of Science Department of Computer Programing Fundamentals (Java) First Stage
Department of Computer
Programing Fundamentals (Java)
First Stage
Java Arrays
Prepared by:
Ari M.Saeed
2018 - 2019
Arrays
• You can make an array of any type int, double, String, etc..
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
int arraySize[]={33,98,74,65};
System.out.println(arraySize.length);
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or) // this form is discouraged
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]);
Mr. Smith
Ms. Jones
Tips: //The following code prints the array's size to standard output
System.out.println(names.length);
Copying Arrays
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"};
http://java.sun.com/docs/books/tutorial
http://www.vogella.com
http://journals.ecs.soton.ac.uk/java/tutorial
15