You are on page 1of 16

Chapter 6

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

-Instead of declaring individual variables, such as number_0, number_1, . . .


, and number_99, you declare one array variable such as numbers and use
numbers[0], numbers[1], . . . , and numbers[99] to represent individual
variables.

- it more useful to think of an array as a collection of variables of the same


type.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Introduction
- The array named number will look like following:

number[99] number[98] number[97] ..... number[i] ..... number[2]number[1] number[0]


90 72 88 ….. 60 ….. 55 45 70

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:

double [ ] marks; // double marks [ ];


int [ ] number; // int number [ ];

- 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:

arrayName = new datatype [array_size];

- The array_size specifies the number of elements that can be stored in this
array.
Example:
marks = new double [45];
number= new int [100];

- The size of an array cannot be changed after the array is created

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:

datatype [ ] arrayName = new datatype [array_size];


or
datatype arrayName [ ] = new datatype [array_size];

Example:

double [ ] marks = new double [45];


int 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
- 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:

default value data type

zero 0 numeric data types

space char data type

false boolean data type

null String data type

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:

int [ ] num= {4 , 18 , -13 , 41 , 23};


int size=num.length; // size=5

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:

num[2] = num[0]+num[1]; // num[2] = 22

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 :

int [ ] num= {4 , 18 , -13 , 41 , 23};


for(int i=0 ; i < num.length ; i++){
System.out.println(num[i]);
}

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.

Q: write a program to store the following temperatures in an array


25 , 14 , 34 , 8 , 14 , 7 , 32 , 18 , 15 , 22 , 40 , 38 , 14 , 21 , 28 , 40 , 19. then find
the following:
1. Print all the temperatures.
2. Find the sum and average of the temperatures.
3. Find the smallest temperature.
4. Find the largest temperature.
5. Find the index of temperature 32.
6. Find the smallest index of largest element.
7. How many times temperature 14 was repeated.
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 prompts the user to enter his name, and puts the
name characters in an array.

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.

Q: write a program to copy the following array into another one:

10.8 93 75.3 22 19 60 88 14.5 5 12.4

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

You might also like