190 likes | 204 Views
Understand arrays, lists, and generics in Java, including declarations, storage, and usage. Learn about ArrayList and List collections, generics implementation, and the benefits of using type-safe data structures.
E N D
The entire array has a single name Each value has a numeric index 79 87 94 82 67 98 87 81 74 91 scores Arrays An array is an ordered list of values 0 1 2 3 4 5 6 7 8 9 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 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
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
79 scores 87 94 82 67 98 87 81 74 91 Arrays Another way to depict the scores array:
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
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: arrayRefVar.Length For example, for the array from the last slide myList.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.
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
ArrayList • Example: ArrayListls = 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 xin ls) { Console.Write("\t" + x); } // prints: 3 7 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: 12 7 Hi Console.WriteLine();
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
What are Generics? • Genericsgive 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 < … >
Generics • Exampleof 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();
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";
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.WriteLine(); Console.ReadKey();
Common ArrayList and List Methods • Add() • Insert() • Remove() • Clear() • Contains() • IndexOf() • LastIndexOf() • BinarySearch() • Sort()