0% found this document useful (0 votes)
2 views

Arrays_part1

The document provides an overview of arrays in programming, covering topics such as array creation, indexing, and basic operations on both 1D and 2D arrays. It explains key concepts like base type, array size, out-of-bounds exceptions, and includes Java code examples for declaring and printing elements of different types of arrays. The document emphasizes the importance of correct indexing to avoid common errors.

Uploaded by

ewadefrdaaew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays_part1

The document provides an overview of arrays in programming, covering topics such as array creation, indexing, and basic operations on both 1D and 2D arrays. It explains key concepts like base type, array size, out-of-bounds exceptions, and includes Java code examples for declaring and printing elements of different types of arrays. The document emphasizes the importance of correct indexing to avoid common errors.

Uploaded by

ewadefrdaaew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Arrays

Part 1
COMP102 Prog. Fundamentals I:
Arrays / Slide 2

Outline of Arrays concept

1. Array creation
2. Performing different functions on Arrays
3. Searching 1D arrays
4. Sorting 1D arrays
5. 2D arrays(Basic operations)
COMP102 Prog. Fundamentals I:
Arrays / Slide 3

Arrays

An array is a collection of variables of the same


type that are referenced by a common name
COMP102 Prog. Fundamentals I:
Arrays / Slide 4

Base type

The common data type of the array elements


COMP102 Prog. Fundamentals I:
Arrays / Slide 5

Index

The number designating the position of array


element
An array index always starts with 0 and ends at
(size-1)
Index is also called subscript
COMP102 Prog. Fundamentals I:
Arrays / Slide 6

Memory representation of Arrays

0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --

Ar

4
COMP102 Prog. Fundamentals I:
Arrays / Slide 7

Size of the array

The of the array is the number of elements


present in the array.
It is determined as array variable.length
Ex: arr.length
Note: length is not a function in arrays.
COMP102 Prog. Fundamentals I:
Arrays / Slide 8

Initial value

When an array variable is created with the new


operator, its elements are automatically
initialised to 0. Which is the default initial value
COMP102 Prog. Fundamentals I:
Arrays / Slide 9

Out of bounds subscripts

The subscripts other than 0 to n-1(both limits


exclusive) for an array having n elements, are
called out of bounds subscripts
COMP102 Prog. Fundamentals I:
Arrays / Slide 10

Out of bounds Exception

Reference to an out of bound subscripts


produces an out -of- bounds- exception and the
program will crash if the exception is not
handled
COMP102 Prog. Fundamentals I:
Arrays / Slide 11

Array Declaration
● Syntax:
<type> <arrayName>=new <type> [<array_size>]
Ex. int Ar[]=new int[10];(or)
int []Ar=new int[10];
● The array elements are all values of the type <type>.
● The size of the array is indicated by <array_size>, the
number of elements in the array.
● <array_size> must be an int constant or a constant
expression. Note that an array can have multiple
dimensions.
COMP102 Prog. Fundamentals I:
Arrays / Slide 12

Subscripting
● Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
● To access an individual element we must apply a subscript to array
named Ar.
● A subscript is a bracketed expression.
– The expression in the brackets is known as the index.
● First element of array has index 0.Ar[0]
● Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
● Last element has an index one less than the size of the array.
Ar[9]
● Incorrect indexing is a common error.
COMP102 Prog. Fundamentals I:
Arrays / Slide 13

Subscripting
// array of 10 uninitialized ints
int Ar[10];
--
Ar[3] = 1;
int x = Ar[3]; 1 --

-- -- --

0 1 2 3 4 5 6 7 8 9
Ar -- -- -- 1 -- -- -- -- -- --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]
COMP102 Prog. Fundamentals I:
Arrays / Slide 14

Java program to declare a array of int data type


and print its elements using for loop
COMP102 Prog. Fundamentals I:
Arrays / Slide 15

import java.util.*;
class arr1
{
void main()
{ int i;
int m[]={1,2,3,4,5};
for(i=0;i<5;i++)
{
System.out.println(m[i]);
}
}
}
COMP102 Prog. Fundamentals I:
Arrays / Slide 16

Java program
. To declare a array of char data type
. Print its elements using for loop
. Print the length of array
COMP102 Prog. Fundamentals I:
Arrays / Slide 17

import java.util.*;
class arr2
{ void main()
{ int i,len;
char m[]={'c','o','m','p','u','t','e','r'};
for(i=0;i<8;i++)
{
System.out.println(m[i]);
//to print all elements in an array using for loop
}
len=m.length; //print length of array
System.out.println("Length of the array is"+len);
}
}
COMP102 Prog. Fundamentals I:
Arrays / Slide 18

Java program
. To declare a array of String data type
. Print its elements using for loop and length
function
COMP102 Prog. Fundamentals I:
Arrays / Slide 19

import java.util.*;
class strarr
{
void arr()
{ int i,len;
String m[]={"student1","student2","student3"};
len=m.length;
System.out.println("Length of the array is"+len);
System.out.println("list of students are:");
for(i=0;i<len;i++)
{
System.out.println(m[i]); //To print all elements in an array using for loop
} //end of for
} // end of arr
} //end of arr

You might also like