Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Similar documents
Recursion (Part 3) 1

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Arrays. Chapter Arrays What is an Array?

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

Recursive Objects. Singly Linked List (Part 2)

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

Section 05: Solutions

Algorithm Analysis. Performance Factors

Section 05: Midterm Review

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

(Refer Slide Time: 1:27)

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

ASYMPTOTIC COMPLEXITY

4.1 Real-world Measurement Versus Mathematical Models

ADTs, Arrays, Linked Lists

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Recursion. notes Chapter 8

CS:3330 (22c:31) Algorithms

Algorithm Efficiency and Big-O

List ADT. B/W Confirming Pages


What is an algorithm?

Recitation 9. Prelim Review

CS 61B Asymptotic Analysis Fall 2018

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

Measuring algorithm efficiency

3. Java - Language Constructs I

Analysis of Algorithms. CS 1037a Topic 13

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Section 05: Solutions

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Summer Final Exam Review Session August 5, 2009

Day 10. COMP1006/1406 Summer M. Jason Hinek Carleton University

Programming II (CS300)

Array Based Lists. Collections

Introduction to the Analysis of Algorithms. Algorithm

Arrays and ArrayLists. David Greenstein Monta Vista High School

CS 261 Data Structures. Big-Oh Analysis: A Review

Advanced Java Concepts Unit 2: Linked Lists.

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Chapter 2: Complexity Analysis

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

ArrayList. Introduction. java.util.arraylist

ASYMPTOTIC COMPLEXITY

Complexity of Algorithms

Java Review: Objects

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

University of Petra Faculty of Information Technology

Nested Loops. A loop can be nested inside another loop.

+ Abstract Data Types

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

A Linked Structure. Chapter 17

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

CSE wi: Practice Midterm

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

List ADT. Announcements. The List interface. Implementing the List ADT

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

Intro to Algorithms. Professor Kevin Gold

UNIT 1 ANALYSIS OF ALGORITHMS

CE204 Data Structures and Algorithms Part 2

Algorithm Analysis. CENG 707 Data Structures and Algorithms

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

CS 231 Data Structures and Algorithms, Fall 2016

Introduction to Analysis of Algorithms

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Building Java Programs

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Algorithm Analysis. Big Oh

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Round 1: Basics of algorithm analysis and data structures

CS 307 Midterm 2 Spring 2008

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

ECE 122. Engineering Problem Solving with Java

Transcription:

1 Arrays

Arrays in Java 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 2 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays to declare an array you use the element type followed by an empty pair of square brackets double[] collection; // collection is an array of double values collection = new double[10]; // collection is an array of 10 double values 3 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays to create an array you use the new operator followed by the element type followed by the length of the array in square brackets double[] collection; // collection is an array of double values collection = new double[10]; // collection is an array of 10 double values 4 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays the number of elements in the array is stored in the public field named length double[] collection; // collection is an array of double values collection = new double[10]; // collection is an array of 10 double values int n = collection.length; // the public field length holds the number of elements 5 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays the values in an array are called elements the elements can be accessed using a zero-based index (similar to lists and strings) 6 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays the elements can be accessed using a zero-based index (similar to lists and strings) collection[0] = 100.0; collection[1] = 100.0; collection[2] = 100.0; collection[3] = 100.0; collection[4] = 100.0; collection[5] = 100.0; collection[6] = 100.0; collection[7] = 100.0; collection[8] = 100.0; collection[9] = 100.0; // set all elements to equal 100.0 collection[10] = 100.0; // ArrayIndexOutOfBoundsException 7 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

8 Computational complexity

Computational complexity computational complexity is concerned with describing the amount of resources needed to run an algorithm for our purposes, the resource is time complexity is usually expressed as a function of n the size of the problem the size of the problem is always a non-negative integer value (i.e., a natural number) 9

Searching a list /** * Returns true if the specified array contains the specified value, and false * otherwise. * * @param arr * an array to search * @param value * a value to search for * @return true if the specified array contains the specified value, and false * otherwise */ public static boolean contains(int[] arr, int value) { boolean result = false; for (int i = 0; i < arr.length; i++) { if (arr[i] == value) { result = true; break; return result; size of problem, n, is the number of elements in the array arr 10

Estimating complexity the basic strategy for estimating complexity: 1. for each line of code, estimate its number of elementary instructions 2. for each line of code, determine how often it is executed 3. determine the total number of elementary instructions 11

Elementary instructions what is an elementary instruction? for our purposes, any expression that can be computed in a constant amount of time examples: 12 declaring a variable assignment (=) arithmetic (+, -, *, /, %) comparison (<, >, ==,!=) Boolean expressions (, &&,!) if, else array access return statement

Elementary instructions loops are technically more complicated, but for our purposes you can consider for(/* something */) to be a single elementary operation but this is not true if the loop initialization, condition, or increment expression involves non-elementary operations 13

Estimating complexity count the number of elementary operations in each line of contains discuss amongst yourselves... 14

Searching a list public static boolean contains(int[] arr, int value) { boolean result = false; 2 for (int i = 0; i < arr.length; i++) { 1 if (arr[i] == value) { 3 result = true; 1 break; 1 return result; 1 15

Estimating complexity for each line of code, determine how often it is executed 16

Searching a list public static boolean contains(int[] arr, int value) { boolean result = false; 1 for (int i = 0; i < arr.length; i++) { 1 if (arr[i] == value) { up to n result = true; up to 1 break; up to 1 return result; 1 17

Total number of operations when counting the total number of operations, we often consider the worst case scenario let s assume that the lines that might run always run multiply the number of elementary operations by the number of times each line runs 18

Total number of operations multiply the number of elementary operations by the number of times each line runs 19

Searching a list public static boolean contains(int[] arr, int value) { boolean result = false; 2 * 1 for (int i = 0; i < arr.length; i++) { 1 * 1 if (arr[i] == value) { 3 * n result = true; 1 * 1 break; 1 * 1 return result; 1 * 1 20

Running time the running time for contains is f n = 3n + 6 21

Big-O notation when counting the number of elementary operations we assumed that all elementary operations would run in 1 unit of time in reality this isn t true and exactly what constitutes an elementary operation and how much time each operation requires depends on many factors in our expression f n = 3n + 6 the constants 3 and 6 are likely to be inaccurate big-o notation describes the complexity of an algorithm that is insensitive to variations in how elementary operations are counted 22

Big-O notation using big-o notation we say that the complexity of contains is in O(n) more formally, a function f(n) is an element of O(g(n)) if and only if there is a positive real number M and a real number m such that f(n) < M g(n) for all n > m 23

Big-O notation Claim: f n = 3n + 6 O(n) Proof: f n = 3n + 6, g n = n For n > 1, f n > 0 and g n 0; therefore, we do not need to consider the absolute values. We need to find M and m such that the following is true: For all n > 1 we have: 3n + 6 < Mn for all n > m 3n + 6 9n 3n + 6 < 9n for all n > 1 and f(n) O(n) 24

Big-O notation Proof 2: f n = 3n + 6, g n = n For n > 1, f n > 0 and g n 0; therefore, we do not need to consider the absolute values. We need to find M and m such that the following is true: For n > 1 we have: 3n + 6 < Mn for all n > m 3n + 6 n < 3n + 6n 3n + 6 < 9n for all n > 1 and f(n) O(n) n < 9n n < 9 25

Big-O notation the second proof uses the following recipe: 1. Choose m = 1 2. Assuming n > 1 derive M such that f(n) g(n) < M g(n) g(n) = M assuming n > 1 implies that 1 < n, n < n 2, n 2 < n 3, etc. which means you can replace terms in the numerator to simplify the expression 26

Big-O notation Claim: f n = 3n 2 n + 100 O n 2 Proof: 1. Choose m = 1 2. Assume n > 1 change to + increase increase 3n 2 n + 100 n 2 < 3n2 + n + 100 n 2 < 3n2 + n 2 + 100n 2 n 2 = 104n2 n 2 = 104 27

O(1) O(1) describes an algorithm that runs in constant time i.e., the run time does not depend on the size of the input examples: determine if an integer is even or odd get for ArrayList contains for HashSet 28

O(log 2 n) O(log 2 n) describes an algorithm whose runtime grows in proportion to the logarithm of the input size i.e., doubling the size of the input increases the runtime by 1 unit of time called logarithmic complexity examples: Arrays.binarySearch (contains for a sorted array) contains for TreeSet 29

O(n) O(n) describes an algorithm whose runtime grows in proportion to the size of the input i.e., doubling the input size double the runtime (approximately) called linear complexity examples: finding the minimum or maximum value in an array or list contains for an unsorted array 30

O(nlog 2 n) O(n log 2 n) describes an algorithm whose runtime complexity is slightly greater than linear i.e., doubling the size of the input more than doubles the runtime (approximately) called linearithmic complexity examples: efficient sorting of an array or list 31

O(n 2 ) O(n 2 ) describes an algorithm whose runtime grows in proportion to the square of the size of the input i.e., doubling the input size quadruples the runtime (approximately) called quadratic complexity examples: inefficient sorting of an array or list checking if everything in one list is in another list 32

O(2 n ) O(2 n ) describes an algorithm whose runtime grows exponentially with the size of the input i.e., increasing the input size by 1 doubles the runtime (approximately) called exponential complexity example: trying to break a combination lock by trying every possible combination 33

Comparing Rates of Growth O(2 n ) O(n 2 ) O(n logn) O(n) 34 n

Comments big-o complexity tells you something about the running time of an algorithm as the size of the input, n, approaches infinity we say that it describes the limiting, or asymptotic, running time of an algorithm for small values of n it is often the case that a less efficient algorithm (in terms of big-o) will run faster than a more efficient one 35

36 Implementing a list

Data Structures data structures (and algorithms) are one of the foundational elements of computer science a data structure is a way to organize and store data so that it can be used efficiently list sequence of elements set a group of unique elements map access elements using a key many more... 37

Implementing a list suppose that we wanted to implement our own list-ofstrings class we want to use an array to store the string references what public features should our class have? discuss amongst yourselves here... 38

Implementing a list how does the choice of using an array affect the implementation of the list features? discuss amongst yourselves here... 39

Implementing a list using an array the capacity of a list is the maximum number of elements that the list can hold note that the capacity is different than the size the size of the list is the number of elements in the list whereas the capacity is the maximum number of elements that the list can hold the client can specify the capacity using a constructor if the clients tries to add more elements than the list can hold we have to increase the capacity 40

public class StringList { private String[] elements; private int capacity; private int size; // Initializes an empty list of strings having the given capacity. public StringList(int capacity) { if (capacity < 1) { throw new IllegalArgumentException("capacity must be positive"); this.capacity = capacity; this.size = 0; this.elements = new String[capacity]; 41

Get and set to get and set an element at an index we simply get or set the element in the array at the given index because arrays are stored contiguously in memory, this operation has O(1) complexity (in theory) 42

/** * Returns the string at the specified position in this list. * * @param index * index of the string to return * @return the string at the specified position in this list * @throws IndexOutOfBoundsException * if index is out of range (index is less than zero or * index is greater than or equal to the size of this list) */ public String get(int index) { if (index < 0 index >= this.size) { throw new IndexOutOfBoundsException("index: " + index); return this.elements[index]; 43

/** * Replaces the string at the specified position in this list with the * specified string. * * @param index * index of the element to replace * @param element * string to be stored at the specified position * @return the string previously at the specified position */ public String set(int index, String element) { String oldelement = this.get(index); this.elements[index] = element; return oldelement; 44

Adding to the end of the list when we add an element to the end of the list we have to check if there is room in the array to hold the new element if not then we have to: 1. make a new array with double the capacity of the old array 2. copy all of the elements from the old array into the new array 3. add the new element to the new array we say that adding to the end of an array-based list has O(1) amortized complexity 45

/** * Appends the specified string to the end of this list. * * @param element string to be appended to this list * @return true (consistent with java.util.collection) */ public boolean add(t element) { if (this.size == this.capacity) { this.resize(); this.elements[this.size] = element; this.size++; return true; 46

/** * Creates a new array twice the size of this.elements, and copies * the references from this.elements to the new array. Assigns the * new array to this.elements. */ private void resize() { int newcapacity = 2 * this.capacity; String[] newelements = new String[newCapacity]; for (int i = 0; i < this.size; i++) { newelements[i] = this.elements[i]; this.capacity = newcapacity; this.elements = newelements; 47

Inserting in the middle of an array when we insert an element into the middle of an array we have to: 1. check if there is room in the array to hold the new element resize if necessary 2. shift the elements from the insertion index to the end of the array up by one index 3. set the array at the insertion index to the new element Step 2 has O(n) complexity 48

/** * Inserts the specified string at the specified position in this list. * Shifts the string currently at that position (if any) and any subsequent * strings to the right (adds one to their indices). * * @param index * index at which the specified element is to be inserted * @param element * element to be inserted * @throws IndexOutOfBoundsException * if index is out of range (index is less than zero or index is * greater than or equal to the size of this list) */ public void add(int index, T element) { if (index < 0 index > this.size) { throw new IndexOutOfBoundsException("index: " + index); if (this.size == this.capacity) { this.resize(); for (int i = this.size - 1; i >= index; i--) { this.elements[i + 1] = this.elements[i]; this.set(index, element); 49

Other list operations removing an element from the end of an array-based list takes O(1) time removing an element from the middle of an arraybased list takes O(n) time need to shift all elements from the removal index to the end of the array down by one index 50

in most cases you should use an array-based list if you find yourself in a situation where most of your operations require inserting or removing elements near the front of a list then you should use a different kind of list 51

Recursive Objects Singly Linked Lists 52

Recursive Objects an object that holds a reference to its own type is a recursive object linked lists and trees are classic examples in computer science of objects that can be implemented recursively 53

Singly Linked List a data structure made up of a sequence of nodes each node has some data a field that contains a reference (a link) to the next node in the sequence suppose we have a linked list that holds characters; a picture of our linked list would be: node 'a' 'x' 'r' 'a' 's' data link null 54

Singly Linked List head node tail node 'a' 'x' 'r' 'a' 's' data link null the first node of the list is called the head node the last node of the list is called the tail node 55

UML Class Diagram LinkedList - size : int - head : Node... Node - data : char - next : Node... Node 'a' data next 56

Node nodes are implementation details that the client does not need to know about LinkedList needs to be able to create nodes i.e., needs access to a constructor if we create a separate Node class other clients can create nodes no way to hide the constructor from every client except LinkedList Java allows the implementer to define a class inside of another class 57

public class LinkedCharList { /** * A class representing the internal nodes of the linked list. * A node is an aggregation of a data element and a link to the * next node in the sequence. * */ public static class Node { private char data; private Node next; // see next slide for Node implementation 58

/** * Initialize this node to store the given data value and sets * the reference to the next node in the sequence to null. * * @param data * the data element to store in this node */ public Node(char data) { this.data = data; this.next = null; 59

/** * Returns the data element stored in this node. * * @return the data element stored in this node */ public char data() { return this.data; /** * Returns the reference to the next node in the sequence after * this node. * * @return the reference to the next node in the sequence * after this node */ public Node next() { return this.next; 60

Node details Node is an nested class a nested class is a class that is defined inside of another class a static nested class behaves like a regular class does not have access to private members of the enclosing class Node does not have access to the private fields of LinkedList a nested class is a member of the enclosing class LinkedList has direct access to private features of Node 61

Linked list fields at a minimum we need fields for: size of the list (the number of elements in the list) the first node of the list (the head node) do we need a field for the capacity? discuss amongst yourselves... 62

public class LinkedCharList { public static class Node { // see previous slides for Node implementation private Node head; private int size; 63

No argument constructor the no argument constructor should create an empty list the size of the list is equal to zero there is no head node (because there is nothing in the list) 64

No argument constructor /** * Initialize the linked list to be empty (size == 0). */ public LinkedCharList() { this.head = null; this.size = 0; 65

Creating a linked list to create the following linked list: 'a' 'x' 'r' 'a' 's' null LinkedCharList t = new LinkedCharList(); t.add('a'); t.add('x'); t.add('r'); t.add('a'); t.add('s'); 66

Add to end of list to add an element to the end of the list we need to: make a node to store the new element get a reference to the current tail node set the current tail node s next field to point to the new node increment size 67

/** * Add an element to the end of this linked list. * * @param elem * the element to add to the end of this linked list * @return true (to be consistent with java.util.collection) */ public boolean add(char elem) { Node n = this.head; for (int i = 0; i < this.size; i++) { n = n.next; n.next = new Node(elem); this.size++; What s wrong with this implementation? return true; 68

/** * Add an element to the end of this linked list. * * @param elem * the element to add to the end of this linked list * @return true (to be consistent with java.util.collection) */ public boolean add(char elem) { Node n = this.head; for (int i = 0; i < this.size - 1; i++) { n = n.next; n.next = new Node(elem); this.size++; return true; What s wrong with this implementation? 69

public boolean add(char elem) { if (this.head == null) { this.head = new Node(elem); else { Node n = this.head; for (int i = 0; i < this.size - 1; i++) { n = n.next; n.next = new Node(elem); this.size++; return true; 70

Getting an element in the list a client may wish to retrieve the ith element from a list the ability to access arbitrary elements of a sequence in the same amount of time is called random access arrays support random access; linked lists do not to access the ith element in a linked list we need to sequentially follow the first (i - 1) links 'a' 'x' 'r' 'a' 's' t.get(3) link 0 link 1 link 2 takes O(n) time versus O(1) for arrays 71

Getting an element in the list to get an element from the list we need to: validate the index get a reference to the node at the specified index return the data of the node 72

/** * Get the element stored at the given index in this linked list. * * @param index * the index of the element to get * @return the element stored at the given index in this linked list * @throws IndexOutOfBoundsException * if (index < 0) (index > size) */ public char get(int index) { if (index < 0 index >= this.size) { throw new IndexOutOfBoundsException(); Node n = this.head; for (int i = 0; i < index - 1; i++) { n = n.next; return n.data; What s wrong with this implementation? 73

/** * Get the element stored at the given index in this linked list. * * @param index * the index of the element to get * @return the element stored at the given index in this linked list * @throws IndexOutOfBoundsException * if (index < 0) (index > size) */ public char get(int index) { if (index < 0 index >= this.size) { throw new IndexOutOfBoundsException(); Node n = this.head; for (int i = 0; i < index; i++) { n = n.next; return n.data; 74

Setting an element in the list setting the ith element is almost exactly the same as getting the ithelement: validate the index get a reference to the node at the specified index remember the current data of the node set the data of the node return the old data of the node 75

/** * Sets the element stored at the given index in this linked list. Returns the * old element that was stored at the given index. * * @param index * the index of the element to set * @param elem * the element to store in this linked list * @return the old element that was stored at the given index * @throws IndexOutOfBoundsException * if (index < 0) (index > size) */ public char set(int index, char elem) { if (index < 0 index >= this.size) { throw new IndexOutOfBoundsException(); Node n = this.head; for (int i = 0; i < index; i++) { n = n.next; char olddata = n.data; n.data = elem; return olddata; 76

Adding in the middle of a list a client may wish to add an element at the ith index of a list t 'a' 'x' 'r' 'a' t.add(2, 'Q'); 'a' 'x' 'Q' 'r' 'a' what steps are required? 77 discuss amongst yourselves here

Removing an element a client may wish to remove an element at the ith index of a list t 'a' 'x' 'Q' 'r' 'a' t.remove(2) 'a' 'x' 'r' 'a' what steps are required? 78 discuss amongst yourselves here