Download presentation
Presentation is loading. Please wait.
Published byΜίδας Αγγελίδης Modified over 6 years ago
1
Data Structures and Database Applications Arrays And Lists
2
Each value has a numeric index
Arrays An array is an ordered list of values scores The entire array has a single name Each value has a numeric index An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9
3
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 3rd 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
4
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;
5
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
6
Arrays Another way to depict the scores array: 79 87 94 82 67 98 81 74
91
7
int[] scores = new int[10];
Declaring Arrays The scores array could be declared as follows: int[] scores = new int[10]; The type of the variable scores is int[] (an array of integers) Note that the array type does not specify its size, but each object of that type has a specific size The reference variable scores is set to a new array object that can hold 10 integers
8
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
9
Array Storage Array data storage for ten doubles:
10
The Length of an Array You can find its size using the Length attribute: arrayRefVar.Length For example, for the array from the last slide myList.Length returns 10
11
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.
12
ArrayList System.Collections.ArrayList
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
13
ArrayList Example: ArrayList ls = new ArrayList(); // instantiates ls object ls.Add(3); // adds 3 to the end of ls ls.Add("Hi"); // adds "Hi" to the end of ls ls.Insert(1,7); // adds 7 after 3 and before "Hi" in ls foreach (Object x in ls) { Console.Write("\t" + x); } // prints: Hi Console.WriteLine(); ls[0] = 12; // replaces first element with 12 for (int i = 0; i < ls.Count; ++i) Console.Write("\t" + ls[i]); } // prints: Hi
14
List System.Collections.Generic.List
Automatic resizing homogeneous Array Mostly has the same methods as ArrayList Unlike with ArrayList, the List class is type-safe through the use of required Generics
15
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 < … >
16
Generics Example of defining a class called Widget to use a Generic type: public class Widget<E> { private E someField; public Widget(E initField) { someField = initField; } public void ShowWidget() { Console.WriteLine(“The Widget is: {0}”, someField); } Examples of creating a Widget object with a Generic type: Widget<int> intWidget = new Widget<int>(1001); intWidget.ShowWidget(); Widget<DateTime> dateWidget = new Widget<DateTime>(DateTime.Now); dateWidget.ShowWidget();
17
Data Types assigned using Generics
Type-safe collections Reusability Example: List<String> studentNames = new List<String>(); studentNames.Add("John"); … String name = studentNames[3]; studentNames[2] = "Mike";
18
List Example: List<string> ls = new List<string>(); // instantiates ls object ls.Add("Joe"); // adds "Joe " to the end of ls ls.Add("Mary"); // adds "Mary" to the end of ls ls.Insert(1, "Jane"); // adds "Jane" after "Joe" before “Mary" foreach (string x in ls) { Console.Write("\t" + x); } // prints: Joe Jane Mary Console.WriteLine(); ls[0] = "Fred"; // replaces first element with "Fred" for (int i = 0; i < ls.Count; ++i) Console.Write("\t" + ls[i]); } // prints: Fred Jane Mary Console.ReadKey();
19
Common ArrayList and List Methods
Add() Insert() Remove() Clear() Contains() IndexOf() LastIndexOf() BinarySearch() Sort()
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.