CS0445 Data Structures Recitation 1

Download Report

Transcript CS0445 Data Structures Recitation 1

Recitation 2
CS0445 Data Structures
Mehmud Abliz
Outline
• Discuss AList class and its methods
– add()
– remove()
– replace()
– getEntry()
– countObject()
– getPosition()
– contains()
Outline (cont.)
– clear()
– getLength()
– isEmpty()
– isFull()
– equals()
– display()
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
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.
remove()
• remove(int givenPosition): removes the
element at the given position.
• Returns the removed element
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’.
getEntry()
• getEntry(int givenPosition): get the
element at the given position.
• Returns the element if successful;
otherwise returns ‘null’.
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.
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.
contains()
• contains(Object anEntry): returns true if
this list contains the specified element.
clear()
• clear(): removes all entries from the list.
• Actually, the only thing it does is setting
the length to zero.
getLength()
• getLength(): gets the length (current
number of entries in list).
isEmpty()
• isEmpty(): check to see if there is element
in the list.
• Same implementation as getLenght()
method.
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’.
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)
display()
• display(): print out the elements in the list
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();