Chapter 03 - Arrays & Strings Part 1
Chapter 03 - Arrays & Strings Part 1
1
Part-01
Arrays
2
Outline
Arrays basic terminology
Advantages/uses of arrays
Java array features
Declaration, Creation, Initialization and accessing array
elements
Multidimensional arrays
Some operations on arrays and their JAVA programs
Traversing
Searching
Sorting
Finding minimum & maximum element
3
What are Arrays ?
An array is a group of consecutive memory locations with
same name and data type.
Simple variable is a single memory location with unique name
and a type. But an Array is collection of different adjacent
memory locations. All these memory locations have one
collective name and type.
The memory locations in the array are known as elements of
array. The total number of elements in the array is called
length.
The elements of array is accessed with reference to its position
in array, that is call index or subscript.
Basic terminology
An array is collection of homogenous data elements
STEP 1 : (Declaration)
int marks[];
marks null
STEP 2: (Memory Allocation)
marks = new int[5];
marks 0 0 0 0 0
marks[0] marks[1] marks[2] marks[3] marks[4]
Example:- System.out.println(units[4]);
0 1 2 3 4 5 6
7 8 9
PROCESSING ARRAY ELEMENTS
Often a for( ) loop is used to process each of the
14
Bounds Checking
For example, if the array score can hold 100 values,
it can be indexed using only the numbers 0 to 99
If i has the value 100, then the following reference
will cause an exception to be thrown:
System.out.println (score[i]);
48 37 33 25 12
Various sorting algorithms/techniques
Bubble Sort
Selection Sort
Insertion Sort
Quick Sort
Merge Sort
Some operations on Arrays
Bubble Sorting
Bubble Sort is also known as exchange sort. It
repeatedly visits the array and compares two items
at a time. It works as follows:
Compare adjacent element. If the first is greater
than the second, swap them.
Repeat this for each pair of adjacent element,
starting with the first two and ending with the last
two. (at this point last element should be
greatest).
Repeat the step for all elements except the last
one.
Keep repeating for one fewer element each time
until there are no pairs to compare
//program#07: Bubble Sort program
import java.util.Scanner;
public class BubbleSort
{
public static void main(String[] args)
{
int numlist[],N,i,j,temp;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of elements in the list>>");
N=input.nextInt();
numlist= new int[N];
System.out.println("Enter " + N + " integer numbers");
for (i= 0; i <N; i++)
numlist[i] = input.nextInt();
System.out.println("Before sorting the numbers in the list are>>");
for(i=0;i<N;i++)
System.out.print(numlist[i]+" ");
for(i=0;i<N-1;i++)
for(j=0;j<N-1-i;j++)
if(numlist[j]>numlist[j+1])
{
temp=numlist[j];
numlist[j]=numlist[j+1];
numlist[j+1]=temp;
}
System.out.println("\nthe numbers in the list after sorting are");
for(i=0;i<N;i++)
System.out.print(numlist[i]+" ");
}
}
1) Write java program to find the minimum or maximum
element in an array of elements depend upon choice entered
by the user