Arrays Basicfundamentaldatastructure.ppt

JyothiAmpally 24 views 16 slides Aug 05, 2024
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Java Arrays fundamental data structure


Slide Content

●This module is intend to develop basic skills in
creating arrays.
●Understand the importance of Arrays.
●Working with Single Dimensional Arrays.
●Working with multidimensional Arrays.
●Implementation of Arrays in real time.
●Using enhanced for loop.
●Passing and returning arrays from methods.
●Introduction to java.util.Arrays class.
Module Overview
2

Uderstand the concept of arrays and create single
double dimensional arrays.
Understand advantages and disadvantages of Arrays.
Declare and create arrays of primitive, class, or array
types
Explain why elements of an array are initialized
Explain how to initialize the elements of an array
Determine the number of elements in an array
Accessing elements using enhanced for loop.
Working with java.util.Arrays class.
Objectives
3

Array: An array is a collection of different values of
same data type which are stored in a single place
and identified by a single variable .Since it stores
multiple values ,array is termed as data structure.
It stores a fixed-size sequential collection of elements
of the same type.
Array elements are accessed using array index.
Array
4

Array Declaration
Array Declaration:
First declare a variable to reference the array.
Second step is to specify the type of array the
variable can reference.
Example:
double[] myList; // preferred way.
or
double myList[]; // works but not preferred way.
char s[]; Point p[]; Char[] s; Point[] p;
Create space for a reference.
An array is an object; it is created with new.

Creating Arrays
Using new operator in order to create array.
arrayRefVar = new dataType[arraySize];
The above statement does two things:
It creates an array using new
dataType[arraySize];
It assigns the reference of the newly created array
to the variable arrayRefVar.
Above steps can be combined in one statement.
dataType[] arrayRefVar = new dataType[arraySize];

Creating Arrays(Cont…)
Alternative Way:
dataType[] arrayRefVar = {value0, value1, ...,
valuek};
The array elements are accessed through the
index. Array indices start from 0 to
arrayRefVar.length-1.

Creating an Array of Primitive Types.

Creating an Array of Objects.
Another example, an object array:
public Point[] createArray() {
Point[] p;
p = new Point[10];
for ( int i=0; i<10; i++ ) {
p[i] = new Point(i, i+1);
}
return p;
}

Creating an Array Object Continuation

for or foreach loop can be used inorder to process
the elements of an array, because all of the elements
in an array are of the same type and the size of the
array is known.
Syntax of for:
for (int i = 1; i < array.length; i++) {
if (array[i] > max) max = array[i];
}
Syntax of for each:
for (double element: array) {
System.out.println(element);
}
Processing Arrays

Passing Arrays to Methods
Like other values to (parameters)methods, arrays
also can be passed to methods.
Ex:
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
printArray(new int[]{3, 1, 2, 6, 4, 2});//invoking an
array

Returning Arrays from methods
A method may also return an array like any other
value to the caller .
Public int[] reverse(int[] list) {
int[] result = new int[list.length];
for (int i = 0; i = result.length - 1;
i < list.length; i++, j--) {
result[j] = list[i];
}
result result;
}

The Array Class
The java.util.Arrays class contains various static
methods for sorting , searching , comparing and
filling array elements.
These methods are overloaded for all primitive
types.
public static int binarySearch(Object[] a, Object
key)
public static boolean equals(long[] a, long[] a2)
public static void fill(int[] a, int val)
public static void sort(Object[] a)

Excercise
Create an application that will sort an array
elements in ascending order and descending.
Create an application that will create a array
which contains elements in reverse order to that
of the original array.
Create an application that will store elements of
one array into another.

Thank You