Download presentation
Presentation is loading. Please wait.
1
Recitation 2 CS0445 Data Structures
Mehmud Abliz
2
Outline Discuss AList class and its methods add() remove() replace()
getEntry() countObject() getPosition() contains()
3
Outline (cont.) clear() getLength() isEmpty() isFull() equals()
display()
4
AList class AList class differs from ArrayList class of JAVA, where AList class: position numbers begin at 1 instead of 0, some method names are different
5
add() Returns ‘true’ if successful; other wise returns ‘false’.
There are 2 ‘add’ methods add(Object newEntry): appends the specified element to the end of this list. add(int newPosition, Object newEntry): inserts the element at the specified position in this list.
6
remove() remove(int givenPosition): removes the element at the given position. Returns the removed element
7
replace() replace(int givenPosition, Object newEntry): replace the element at the given position with the new element. Returns ‘true’ if replacing is true; otherwise returns ‘false’.
8
getEntry() getEntry(int givenPosition): get the element at the given position. Returns the element if successful; otherwise returns ‘null’.
9
countObject() countObject(Object anEntry): count how many times a given element (object) is appeared in the list. Returns the number of times appeared; if the element is not in the list, returns 0.
10
getPosition() getPosition(Object anEntry): get the position of the given element Note that the position starts from 1, not 0. Returns the position of the element; if the element is not in the list returns -1.
11
contains() contains(Object anEntry): returns true if this list contains the specified element.
12
clear() clear(): removes all entries from the list.
Actually, the only thing it does is setting the length to zero.
13
getLength() getLength(): gets the length (current number of entries in list).
14
isEmpty() isEmpty(): check to see if there is element in the list.
Same implementation as getLenght() method.
15
isFull() isFull(): check to see if the current number of elements in the list is equal to the maximum capacity of the list. Returns ‘true’ if so; else returns ‘false’.
16
equals() equals(AList other): check to see if the list is equal to other list (whether length is equal, and the all the elements in the list are the same, and the order of elements are same)
17
display() display(): print out the elements in the list
18
Example myList.add(5); //add Integer myList.add(23); myList.add(2);
myList.add(1,"73");//add String myList.add(3,97); myList.replace(2,"W"); myList.remove(4); myList.display();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.