Data Structures and Database Applications Arrays And Lists

Data Structures and Database Applications Arrays And Lists 1

Arrays An array is an ordered list of values Each value has a numeric index The entire array has a single name 0 scores 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9

Arrays � A particular value in an array is referenced using the array name followed by the index in brackets � For example, the expression scores[2] refers to the value 94 (the 3 rd value in the array) � That expression represents a place to store a single integer and can be used wherever an integer variable can be used

Arrays � For example, an array element can be assigned a value, printed, or used in a calculation: scores[2] = 89; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2;

Arrays � The values held in an array are called array elements � An array stores multiple values of the same type – the element type � The element type can be a primitive type or an object reference � Therefore, we can create an array of integers, an array of characters, an array of String objects, an array of Coin objects, etc. � In Java, the array itself is an object that must be instantiated

Arrays Another way to depict the scores array: scores 79 87 94 82 67 98 87 81 74 91
![Declaring Arrays q The scores array could be declared as follows: q int[] scores Declaring Arrays q The scores array could be declared as follows: q int[] scores](http://slidetodoc.com/presentation_image_h2/a4c17eb1d645e142e02da8474de6fd23/image-7.jpg)
Declaring Arrays q The scores array could be declared as follows: q int[] scores = new int[10]; q The type of the variable scores is int[] (an array of integers) q Note that the array type does not specify its size, but each object of that type has a specific size q The reference variable scores is set to a new array object that can hold 10 integers
![Declaring Arrays � Some other examples of array declarations: bool[] flags; // just declaring Declaring Arrays � Some other examples of array declarations: bool[] flags; // just declaring](http://slidetodoc.com/presentation_image_h2/a4c17eb1d645e142e02da8474de6fd23/image-8.jpg)
Declaring Arrays � Some other examples of array declarations: bool[] flags; // just declaring it flags = new bool[20]; // just creating it char[] codes = new char[1750]; // doing both float[] prices = new float[500]; // doing both

Array Storage Array data storage for ten doubles:

The Length of an Array You can find its size using the Length attribute: array. Ref. Var. Length For example, for the array from the last slide my. List. Length returns 10

System. Collections � Contains interfaces and classes that define various collections of objects, such as different types of lists, queues, bit arrays, hash tables and dictionaries. The list type collections assign an integer (the index) to each element stored. � Indices of elements are 0 for the element at the beginning of the list, 1 for the next element, and so on. � The list type collections permit duplicate elements, which are distinguished by their position in the list. �

Array. List � System. Collections. Array. List � Can hold any data type: (hybrid) � Internally: array object � Automatic resizing � Not type safe: casting errors detected only at runtime � Boxing/unboxing: extra-level of indirection affects performance � Not homogeneous

Array. List � Example: Array. List ls = new Array. List(); ls. Add(3); ls. Add("Hi"); ls. Insert(1, 7); foreach (Object x in ls) { Console. Write("t" + x); } Console. Write. Line(); // instantiates ls object // adds 3 to the end of ls // adds "Hi" to the end of ls // adds 7 after 3 and before "Hi" in ls ls[0] = 12; for (int i = 0; i < ls. Count; ++i) { Console. Write("t" + ls[i]); } Console. Write. Line(); // replaces first element with 12 // prints: 3 7 // prints: 12 7 Hi Hi

List � System. Collections. Generic. List � Automatic resizing homogeneous Array � Mostly has the same methods as Array. List � Unlike with Array. List, the List class is typesafe through the use of required Generics

What are Generics? � Generics give a class or a method the capability to parameterize types. This means Generics gives you the ability to assign a specific type of data that a particular class or method will deal with in a particular instance. The Generic data type is assigned when the class is defined, and it is given a concrete value when the class is instantiated to an object that will use the class to handle that particular data type. � The parameter identifying the Generic type is placed between angle brackets < … > 15

Generics � Example of defining a class called Widget to use a Generic type: public class Widget<E> { private E some. Field; public Widget(E init. Field) { some. Field = init. Field; } public void Show. Widget() { Console. Write. Line(“The Widget is: {0}”, some. Field); } } � Examples of creating a Widget object with a Generic type: Widget<int> int. Widget = new Widget<int>(1001); int. Widget. Show. Widget(); Widget<Date. Time> date. Widget = new Widget<Date. Time>(Date. Time. Now); date. Widget. Show. Widget(); 16

Data Types assigned using Generics Type-safe collections � Reusability � � Example: List<String> student. Names = new List<String>(); student. Names. Add("John"); … String name = student. Names[3]; student. Names[2] = "Mike";

List � Example: List<string> ls = new List<string>(); ls. Add("Joe"); ls. Add("Mary"); ls. Insert(1, "Jane"); foreach (string x in ls) { Console. Write("t" + x); } Console. Write. Line(); // instantiates ls object // adds "Joe " to the end of ls // adds "Mary" to the end of ls // adds "Jane" after "Joe" before “Mary" ls[0] = "Fred"; for (int i = 0; i < ls. Count; ++i) { Console. Write("t" + ls[i]); } Console. Write. Line(); Console. Read. Key(); // replaces first element with "Fred" // prints: Joe // prints: Fred Jane Mary

Common Array. List and List Methods � Add() � Insert() � Remove() � Clear() � Contains() � Index. Of() � Last. Index. Of() � Binary. Search() � Sort()
- Slides: 19