0% found this document useful (0 votes)
20 views23 pages

Arrays - Part 1

Uploaded by

Vishal kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views23 pages

Arrays - Part 1

Uploaded by

Vishal kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 23

ARRAYS

PART - 1
2

1. MANAGING VARIABLES IN A PROGRAM.


2. UTILITY OF AN ARRAY.
3. UNDERSTANDING DATA STRUCTURE.
AGENDA 4. WHAT IS AN ARRAY?
5. UNDERSTANDING ELEMENT & SUBSCRIPT.
6. DATA TYPE OF AN ARRAY.
7. SINGLE DIMENSION AND MULTI-
DIMENSION ARRAY.
8. ARRAY DECLARATION.
9. ARRAY DEFINITION.
10.INITIALIZING AN ARRAY.
3

MANAGING VARIABLES

Consider a situation where we need to accept 5 integer


numbers into a program.

eg. System.out.println(“enter 5 values”);


int a = scr.nextInt();
int b = scr.nextInt();
int c = scr.nextInt();
int d = scr.nextInt();
int e = scr.nextInt();
4

MANAGING VARIABLES

With reference to the example in the previous slide:

* Each variable has a unique name.


* For doing same job, statements are repeated.
* There is change only in variable name.

Now let us assume we are going to accept 5000 values, are


we going to use 5000 variables?
MANAGING VARIABLES

TO HANDLE SUCH
SITUATIONS ALL
PROGRAMMING
LANGUAGES PROVIDE A
DATA STRUCTURE CALLED
ARRAY.
6

 Data structures are essential in


DATA managing large amounts of data.
STRUCTURE  Data chunked together into a
single unit for a specific purpose.
 Arrays, Objects, Lists, Stacks,
Queues are all examples of data
structure.
7

WHAT IS AN
ARRAY ?

 A container object that holds a fixed number of


values.
 All values in an array are of the same type.
 After creation of an array, its length is fixed.
 Each item in an array is called an element.
 Each element is accessed by its numerical
index.

8

UTILITY OF AN ARRAY

 Array is used to store and process a collection of data(or


collection of variables) of the same type.
 Used to group a number of data items into a large unit.
9

Primitive:
byte, short, int, long, float, double, char, boolean
DATA TYPE OF
AN ARRAY

Reference:
Objects (user –
defined)
10

TYPES OF ARRAY

ONE-DIMENSIONAL OR SINGLE DIMENSIONAL ARRAY


 Each element is referenced by a single index number.
ARRAYS
Eg. arr[0], arr[9]…..

MULTI-DIMENSIONAL ARRAY( 2 DIMENSIONAL ARRAY)


 In a 2 dimensional array every element in the array is
referenced by 2 index numbers.
 Eg. arr[1,0], arr[7,100]……
11
ARRAY DECLARATION

 Arrays should be declared before use, as other elements of


a program.
 Declaration tells the compiler :
 what type of data will be stored.
 name by which the array will be referred to.
Internally creates a reference variable, which stores the
starting address of the array.
12
ARRAY DECLARATION

Syntax: array-type[] array-


name ;

Data type:
of each element Identifier:
in an array Array name

Separator:
Separator: statement
specifies name terminator
preceding it is
an array.

Note: array can also be declared as – array-type array-name[];


13
ARRAY DECLARATION
This statement internally creates a
e.g.: int[] arr; reference variable ‘arr’.

Data type:
of each element Identifier:
in an array Array name

Separator:
Separator: statement
specifies name terminator
preceding it is
an array.

Note: array can also be declared as – int arr[];


14
ARRAY DECLARATION

If an array is declared as
int[] arr, j, k This means that arr, j & k are arrays of int
type.

Instead, if an array is declared as


int arr[], j, k This means that only arr is an array of
int type but j & k are variables which are
of int type.

Note: when an array is declared, only a reference is created.


15
ALLOCATING MEMORY TO AN ARRAY
int[] arr; // 1. declaring an array. arr is a reference variable.
arr = new int[20]; // 2. allocating memory to an array.
• ‘new’ keyword creates 20 contiguous memory locations.
• These locations are initialized with default initial values, in this case 0.
• The address of the first element of the array is copied to the reference
variable arr.
• Reference variable arr now points to the array.
Combining both the statements(1 & 2)
int[] arr = new int[20];

Note: An array is created at run time.



16

Picture shown here refers to an No. of elements : 8


array arr which is initiazed with Index nos. : 0 to 7
default initial values.

int[] arr = new int[8]; address 0 arr[0]


0
address arr[1]
0
arr
0 arr[2]

0
Length of an array is given by arr.length
0
- this will give 8 for the given example.
0
length is a final variable associated with arrays.
0
arr[7]

17

Picture shown here refers to No. of elements : 10


an array myList which Index nos. : 0 to 9
contains 10 double values.

double[] mylist = new double[10]


18

ELEMENT & SUBSCRIPT

Element Subscript (index no.)


• A value in an array. • It is a +ve integer attributed to each element.
• Element count starts at 1. • Subscript starts at 0.
• No. of elements • Last subscript no. in an array is the no. of
corresponds to the no. of elements – 1.
values in an array.
19
int[] x = new int[5];

for(int i = 0; i<x.length; i++)

READING VALUES {
INTO AN ARRAY System.out.println(“Enter a value”);
x[i] = scr.nextInt();
}
For every iteration, a value will be read into the
array.
When i = 0, value will be read into x[0]
When i = 1, value will be read into x[1]
When i = 2, value will be read into x[2]
When i = 3, value will be read into x[3]
When i = 4, last value will be read into x[4]
(assuming that values are stored in the array) 20

for(int i = 0; i<x.length; i++)

DISPLAYING {
VALUES OF AN
ARRAY System.out.println(“value of x[”+(i+1)+“]:
”+x[i]);

}
For every iteration, an array value is displayed.
When i = 0, 1st value will be displayed ie. x[0]
When i = 1, 2nd value will be displayed ie. x[1]
When i = 2, 3rd value will be displayed ie. x[2]
When i = 3, 4th value will be displayed ie. x[3]
When i = 4, last value will be displayed ie. x[4]
21

char[ ] ch = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};

INITIALIZING AN ARRAY 1. In the above example a character array ch is initialized


WITHOUT NEW OPERATOR with vowels.
2. ‘new’ operator is not used explicitly to create an array.
3. ch.length will give the length of the array( or number of
elements in the array)
Other examples to initialize array:
 double[] nos = {3.7, 9.5, 76.5};
 String[] str = {“TIGER”, “LION”, “PANTHER”, “JAGUAR”};
 float[] even = {4.0f, 6.0f, 2.0f, 7.8f, 10.24f};
 boolean[] boo = {true, false, false, true};
PROGRAMS 22

1. Write a program to create an array arr. Accept 10 int values into the array, display
the double of each element that is accepted.
2. Write a program to create an array x and accept 5 double values into the array.
Find the cube of each value and store it into another array y. Display both the
arrays.
3. Write a program to accept 5 values into an array arr1 and 5 values into another
array arr2. Find the sum of the corresponding values of both the arrays and store
the sum into an another array sum.
4. Write a program to accept 10 int values into an array. Copy the value of arr[0] into
arr[1], arr[1] into arr[2] and so on. The value of arr[9] should be copied to arr[0].
Display the initial and the final array.
5. Write a program to initialize a String array with 10 names. Reverse each element
and store it into another array. Display both the arrays.
6. Write a program to accept 20 int values into an array arr1. Copy only the even
numbers from the arr1 to another array arr2. Display both the arrays.
23

Thanks
!

You might also like