Data Structure - Arryas - Notes, Study notes of Data Structures and Algorithms

Summary about Arrays, Creating, Initializing, and Accessing an Array, Searching in Ordered Arrays, Operations on Arrays, Ordered Arrays, Insertion in Ordered Arrays.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays
An array is a container object that holds a fixed
number of values of a single type. The length of an
array is established when the array is created. After
creation, its length is fixed.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Data Structure - Arryas - Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Arrays

  • (^) An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Arrays

You can also place the square brackets after the array's name: float anArrayOfFloats[]; // this form is discouraged However, convention discourages this form; the brackets identify the array type and should appear with the type designation.

Creating, Initializing, and

Accessing an Array

Alternatively, you can use the shortcut syntax to create and initialize an array: int[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000}; Here the length of the array is determined by the number of values provided between { and }. You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.

Creating, Initializing, and

Accessing an Array

In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length. class MultiDimArrayDemo { public static void main(String[] args) { String[][] names = {{"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"}}; System.out.println(names[0][0] + names[1][0]); //Mr. Smith System.out.println(names[0][2] + names[1][1]); //Ms. Jones } }

Creating, Initializing, and

Accessing an Array

Array elements are accessed using an index number in square brackets. This is similar to how other languages work: temp = intArray[3]; // get fourth element of array intArray[7] = 66; // insert 66 into the eighth cell Unless you specify otherwise, an array of integers is automatically initialized to 0 when it’s created.

Example

Classes LowArray and LowArrayApp (TextBook)

  • (^) In lowArray.java , we wrap the class LowArray around an ordinary Java array. The array is hidden from the outside world inside the class; it’s private, so only LowArray class methods can access it. There are three LowArray methods : setElem() and getElem() , which insert and retrieve an element, respectively; and a constructor , which creates an empty array of a specified size.

Example

Example

Example

Example

Operations on Arrays

Insertion Operation

1.Insertion at the end : Append

2.Insertion in between

Ordered Arrays

Imagine an array in which the data items

are arranged in order of ascending key

values—that is, with the smallest value at

index 0, and each cell holding a value

larger than the cell below. Such an array

is called an ordered array.

Searching in Ordered Arrays

There are two types of searching

commonly used in ordered arrays.

• Linear Searching

• Binary searching

Searching in Ordered Arrays

Linear searches operate in much the

same way as the searches in the

unordered array

Only difference is that once you find an

index having greater value than

desired number, you stop.