Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a generic list class called GenericList. The class should be able to hold

ID: 3655091 • Letter: W

Question

write a generic list class called GenericList. The class should be able to hold up to 100 of any type item. The class should have the following members: 1- a simple constructor 2- add (item) add the item to the list 3- pullSmallest()- find, return and remove the smallest item in the list Here is how you should be able to use it: GenericList myList; myList.add(10); myList.add(5); myList.add(20); myList.add(15); cout << myList.pullSmallest() << " "; // This should be the output 10 20 15

Explanation / Answer

Lists A list is an ordered collection of typed primitives, sObjects, user-defined objects, Apex objects or collections that are distinguished by their indices. For example, the following table is a visual representation of a list of Strings: Index 0 Index 1 Index 2 Index 3 Index 4 Index 5 'Red' 'Orange' 'Yellow' 'Green' 'Blue' 'Purple' The index position of the first element in a list is always 0. Because lists can contain any collection, they can be nested within one another and become multidimensional. For example, you can have a list of lists of sets of Integers. A list can contain up to four levels of nested collections inside it. To declare a list, use the List keyword followed by the primitive data, sObject, nested list, map, or set type within characters. For example: // Create an empty list of String List my_list = new List(); // Create a nested list List>> my_list_2 = new List>>(); // Create a list of account records from a SOQL query List accs = [SELECT Id, Name FROM Account LIMIT 1000]; To access elements in a list, use the system methods provided by Apex. For example: List MyList = new List(); // Define a new list MyList.add(47); // Adds a second element of value 47 to the end // of the list MyList.get(0); // Retrieves the element at index 0 MyList.set(0, 1); // Adds the integer 1 to the list at index 0 MyList.clear(); // Removes all elements from the list For more information, including a complete list of all supported methods, see List Methods. Using Array Notation for One-Dimensional Lists of Primitives or sObjects When using one-dimensional lists of primitives or sObjects, you can also use more traditional array notation to declare and reference list elements. For example, you can declare a one-dimensional list of primitives or sObjects by following the data or sObject type name with the [] characters: String[] colors = new List(); To reference an element of a one-dimensional list of primitives or sObjects, you can also follow the name of the list with the element's index position in square brackets. For example: colors[3] = 'Green'; All lists are initialized to null. Lists can be assigned values and allocated memory using literal notation. For example: Example Description List ints = new Integer[0]; Defines an Integer list with no elements List accts = new Account[]{}; Defines an Account list with no elements List ints = new Integer[6]; Defines an Integer list with memory allocated for six Integers List accts = new Account[] {new Account(), null, new Account()}; Defines an Account list with memory allocated for three Accounts, including a new Account object in the first position, null in the second position, and another new Account object in the third position List contacts = new List (otherList); Defines the Contact list with a new list Lists of sObjects Apex automatically generates IDs for each object in a list of sObjects when the list is successfully inserted or upserted into the database with a data manipulation language (DML) statement. Consequently, a list of sObjects cannot be inserted or upserted if it contains the same sObject more than once, even if it has a null ID. This situation would imply that two IDs would need to be written to the same structure in memory, which is illegal. For example, the insert statement in the following block of code generates a ListException because it tries to insert a list with two references to the same sObject (a): try { // Create a list with two references to the same sObject element Account a = new Account(); Account[] accs = new Account[]{a, a}; // Attempt to insert it... insert accs; // Will not get here System.assert(false); } catch (ListException e) { // But will get here } For more information on DML statements, see Apex Data Manipulation Language (DML) Operations. You can use the generic sObject data type with lists. You can also create a generic instance of a list. List Sorting Using the List.sort method, you can sort lists of primitive data types, custom types (your Apex classes) that implement the Comparable Interface, SelectOption elements, and sObjects (standard objects and custom objects). Sorting is in ascending order for primitive data types. For custom types, the sort criteria and sort order depends on the implementation that you provide for the compareTo method of the Comparable interface. For more information on implementing the Comparable Interface for your own classes, see Comparable Interface. For sObjects, sorting is in ascending order and uses a sequence of comparison steps outlined in the next section. However, you can also implement a custom sort order for sObjects by wrapping your sObject in an Apex class and implementing the Comparable Interface, as shown in Custom Sort Order of sObjects. For SelectOption, sorting is in ascending order based on the value and label fields. See Default Sort Order for SelectOption for the sequence of comparison steps used for SelectOption. Default Sort Order of sObjects The List.sort method sorts sObjects in ascending order and compares sObjects using an ordered sequence of steps that specify the labels or fields used. The comparison starts with the first step in the sequence and ends when two sObjects are sorted using specified labels or fields. The following is the comparison sequence used: The label of the sObject type. For example, an Account sObject will appear before a Contact. The Name field, if applicable. For example, if the list contains two accounts named A and B respectively, account A comes before account B. Standard fields, starting with the fields that come first in alphabetical order, except for the Id and Name fields. For example, if two accounts have the same name, the first standard field used for sorting is AccountNumber. Custom fields, starting with the fields that come first in alphabetical order. For example, suppose two accounts have the same name and identical standard fields, and there are two custom fields, FieldA and FieldB, the value of FieldA is used first for sorting. Not all steps in this sequence are necessarily carried out. For example, if a list contains two sObjects of the same type and with unique Name values, they