Professional Documents
Culture Documents
Assm
Assm
Single-Dimensional
Arrays
Introduction
- An array is a data structure that can be used to store a collection of data
(elements) of the same type.
- Suppose, for instance, that you need to read 100 numbers, compute their
average, and find out how many numbers are above the average
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Declaration
- The syntax for declaring an array variable is:
datatype [ ] arrayName;
// or datatype arrayName [ ];
Example:
- The array can be any data type, and all elements in the array will have
the same data type.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Creation
- The syntax for creating the array is:
- The array_size specifies the number of elements that can be stored in this
array.
Example:
marks = new double [45];
number= new int [100];
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Declaration and Creation
-Declaring and creating an array can be combined in one statement.
-The syntax for declaring and creating an array in one statement is:
Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Declaration and Creation
- We have declared and created the array, but we haven’t filled it yet!.
- The syntax for assigning (storing) a value at specific index in an array is:
arrayName[index] = value;
- The index represents the position where the value will be stored in.
- The value must be in the data type of the array.
Example:
marks[4] = 72.0;
marks[30] = 87.0;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Declaration and Creation
Example:
int [] num=new int [5];
num[0] = 4;
num
num[1] = 18;
num[2] = -13;
4 3 2 1 0
num[3] = 41; 23 41 -13 18 4
num[4] = 23;
---------------------------------------------------------------------------------
char
- [] vowels = new char[5];
vowels[0]= ‘a’; vowels
vowels[1]= ‘e’;
4 3 2 1 0
vowels[2]= ‘i’;
vowels[3]= ‘o’; ‘u’ ‘o’ ‘i’ ‘e’ ‘a’
vowels[4]= ‘u;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Default Values
- When an array is created, its elements are assigned the default values as
following:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Initializers
- Java has a shorthand notation, known as the array initializer, which
combines the declaration, creation, and initialization of an array in one
statement.
-The syntax is:
datatype [ ] arrayName = {value_1 , value_2 , … , value_n};
Example:
int [ ] num= {4 , 18 , -13 , 41 , 23};
char [ ] vowels= { ‘a’ , ‘e’ , ‘i’ , ‘o’ , ‘u’};
- Array initializing has to be written in one statement. The following causes
syntax error:
int [ ] num;
num={4 , 18 , -13 , 41 , 23};
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Size
-The syntax for finding the size of an array is:
arrayName.length;
Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Array Index
-The array elements are accessed through the index. Array indices are
zero based; that is, they range from zero to arrayName.length-1.
-Each element in the array is represented using the following syntax:
arrayName[index];
Example:
int [ ] num= {4 , 18 , -13 , 41 , 23};
System.out.println(num[3]); // 41
-Afteran array is created, an indexed variable can be used in the same
way as a regular variable.
-Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Processing Arrays
-When processing array elements, you will often use a for loop.
Example :
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Processing Arrays
Q: write a program that defines an integer array of size 10, and prompts the user to
enter 10 numbers, stores it in an array and prints the numbers, and average of the
numbers.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Copying Arrays
-To copy the contents of one array, you have to create another array of the
same size and data type, then by using for loop you can copy the elements
of the first array to its corresponding indices in the second array.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein