Download csci185_18 - Homework Market

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 18
Dynamic Data Structures
and Generics (II)
1
Class ArrayList
• Object of an ArrayList can be used to store an
unlimited number of objects.
2
Methods of ArrayList (I)
java.util.ArrayList
+ArrayList()
Creates an empty list.
+add(o: Object) : void
Appends a new element o at the end of this list.
+add(index: int, o: Object) : void
Adds a new element o at the specified index in this list.
+clear(): void
Removes all the elements from this list.
+contains(o: Object): boolean
Returns true if this list contains the element o.
+get(index: int) : Object
Returns the element from this list at the specified index.
+indexOf(o: Object) : int
Returns the index of the first matching element in this list.
+isEmpty(): boolean
Returns true if this list contains no elements.
+lastIndexOf(o: Object) : int
Returns the index of the last matching element in this list.
+remove(o: Object): boolean
Removes the element o from this list.
+size(): int
Returns the number of elements in this list.
+remove(index: int) : Object
Removes the element at the specified index.
+set(index: int, o: Object) : Object
Sets the element at the specified index.
3
Methods of ArrayList (II)
• Array is used to implement the methods
– Methods get(int index) and set(int index, Object o)
for accessing and modifying an element through
an index and the add(Object o) for adding an
element at the end of the list are efficient. Why?
– Methods add(int index, Object o) and remove(int
index) are inefficient because it requires shifting
potentially a large number of elements.
4
Linked Data Structure
• To improve efficiency for adding and removing
an element anywhere in a list.
• It contains
– Collection of objects
– Each object contains data and a reference to
another object in the collection
5
Linked List
• A dynamic data structure
• A linked list consists of nodes. Each node
contains
– an element
– a link: linked to its next neighbor.
• Example: take notes in the class!
6
Linked List
ListNodes
7
ListNodes in Linked List
public class ListNode { // fill the comments
private String data; //
private ListNode link; //
/**
*/
public ListNode () {
link = null; //
data = null; //
}
/**
public ListNode (String newData, ListNode linkValue) {
data = newData;
link = linkValue;
}
*/
8
ListNodes in Linked List (cont’d)
/**
*/
public void setData (String newData) {
data = newData;
}
/**
public String getData () {
return data;
}
*/
9
ListNodes in Linked List (cont’d)
/**
*/
public void setLink (ListNode newLink) {
link = newLink;
}
/**
*/
public ListNode getLink () {
return link;
}
}
10
Linked List Class
• A linked list class uses the ListNode class
• The variable head refers to the first node in
the list.
11
Linked List of String
public class StringLinkedList { //write comments
private ListNode head;
/**
*/
public StringLinkedList () {
head = null; //
}
12
Linked List of String (cont’d)
/**
public void showList () {
ListNode position = head; //
while (position != null) {
System.out.println (position.getData ());
position = position.getLink (); //
}
}
*/
13
Linked List of String (cont’d)
/**
public int length () {
int count = 0;
ListNode position = head; //
while (position != null) { //
count++; //
position = position.getLink (); //
}
return count;
}
*/
14
Linked List of String (cont’d)
/**
*/
public void addANodeToStart (String addData) {
head = new ListNode (addData, head);
}
/**
*/
public void deleteHeadNode () {
if (head != null)
head = head.getLink (); //
else {
System.out.println ("Deleting from an empty list.");
System.exit (0);
}
}
15
Linked List of String (cont’d)
/* Returns a reference to the first node containing the target
data. If target is not on the list, returns null. */
private ListNode find (String target) {
boolean found = false; //
ListNode position = head;
while ((position != null) && !found) { //
String dataAtPosition = position.getData ();
if (dataAtPosition.equals (target))
found = true;
else
position = position.getLink (); //
}
return position;
}
16
}
Linked List of String (cont’d)
/**
public boolean onList (String target) {
return find (target) != null;
}
*/
17
Lab Exercises
Write a demo program that tests the discussed
methods
• add the courses you took into the linked list.
• Show all courses in the linked list
• Delete the first course you added
• check some courses whether they are in the
list or not.
18
Related documents