SlideShare a Scribd company logo
Lecture 18
Dynamic Data Structures and Generics (II)
*
*
Class ArrayListObject of an ArrayList can be used to store an
unlimited number of objects.
*
Methods of ArrayList (I)
*
java.util.ArrayList
+ArrayList()
+add(o: Object) : void
+add(index: int, o: Object) : void
+clear(): void
+contains(o: Object): boolean
+get(index: int) : Object
+indexOf(o: Object) : int
+isEmpty(): boolean
+lastIndexOf(o: Object) : int
+remove(o: Object): boolean
+size(): int
+remove(index: int) : Object
+set(index: int, o: Object) : Object
Appends a new element o at the end of this list.
Adds a new element o at the specified index in this list.
Removes all the elements from this list.
Returns true if this list contains the element o.
Returns the element from this list at the specified index.
Returns the index of the first matching element in this list.
Returns true if this list contains no elements.
Returns the index of the last matching element in this list.
Removes the element o from this list.
Returns the number of elements in this list.
Removes the element at the specified index.
Sets the element at the specified index.
Creates an empty list.
Methods of ArrayList (II) Array is used to implement the
methodsMethods 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.
*
Linked Data StructureTo improve efficiency for adding and
removing an element anywhere in a list.It containsCollection of
objectsEach object contains data and a reference to another
object in the collection
*
Linked ListA dynamic data structureA linked list consists of
nodes. Each node contains an elementa link: linked to its next
neighbor. Example: take notes in the class!
*
Linked List
*
ListNodes
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;
}
*
ListNodes in Linked List (cont’d)
/** */
public void setData (String newData) {
data = newData;
}
/** */
public String getData () {
return data;
}
*
ListNodes in Linked List (cont’d)
/** */
public void setLink (ListNode newLink) {
link = newLink;
}
/** */
public ListNode getLink () {
return link;
}
}
*
Linked List ClassA linked list class uses the ListNode classThe
variable head refers to the first node in the list.
*
Linked List of String
public class StringLinkedList { //write comments
private ListNode head;
/** */
public StringLinkedList () {
head = null; //
}
*
Linked List of String (cont’d)
/** */
public void showList () {
ListNode position = head; //
while (position != null) {
System.out.println (position.getData ());
position = position.getLink (); //
}
}
*
Linked List of String (cont’d)
/** */
public int length () {
int count = 0;
ListNode position = head; //
while (position != null) { //
count++; //
position = position.getLink (); //
}
return count;
}
*
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);
}
}
*
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;
}
}
*
Linked List of String (cont’d)
/** */
public boolean onList (String target) {
return find (target) != null;
}
*
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 listDelete the first course you addedcheck some courses
whether they are in the list or not.
*
java.util.ArrayList
+
ArrayList
()
+add(o: Object) : void
+add(index: int, o: Object) : void
+clear(): void
+contains(o: Object): boolean
+get(index: int) : Object
+indexOf(o: Object) : int
+isEmpty(): boolean
+lastIndexOf(o: Object) : int
+remove(o:
Object): boolean
+size(): int
+remove(index: int) : Object
+set(index: int, o: Object) : Object
Appends a new element o at the end of this list.
Adds a new element o at the specified index in this list.
Removes all the elements from this list.
Returns t
rue if this list contains the element o.
Returns the element from this list at the specified index.
Returns the index of the first matching element in this list.
Returns true if this list contains no elements.
Returns the index of the last matching elemen
t in this list.
Removes the element o from this list.
Returns the number of elements in this list.
Removes the element at the specified index.
Sets the element at the specified index.
Creates an empty list.

More Related Content

Similar to Lecture 18Dynamic Data Structures and Generics (II).docx

Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
cgraciela1
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
info430661
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Stewart29UReesa
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
deepakangel
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
VictorzH8Bondx
 
Jhtp5 20 Datastructures
Jhtp5 20 DatastructuresJhtp5 20 Datastructures
Jhtp5 20 Datastructuresmartha leon
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
footstatus
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Augstore
 
In this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdfIn this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdf
fantasiatheoutofthef
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
arcellzone
 
I need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdfI need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdf
fantoosh1
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
HUST
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
HUST
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
Nivegeetha
 
Select three methods in the ObjectList class to work through algori.pdf
Select three methods in the ObjectList class to work through algori.pdfSelect three methods in the ObjectList class to work through algori.pdf
Select three methods in the ObjectList class to work through algori.pdf
aroraopticals15
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
accostinternational
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdf
ambersushil
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
Waf1231
 

Similar to Lecture 18Dynamic Data Structures and Generics (II).docx (20)

Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
 
Jhtp5 20 Datastructures
Jhtp5 20 DatastructuresJhtp5 20 Datastructures
Jhtp5 20 Datastructures
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
 
In this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdfIn this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdf
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
 
I need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdfI need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdf
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Select three methods in the ObjectList class to work through algori.pdf
Select three methods in the ObjectList class to work through algori.pdfSelect three methods in the ObjectList class to work through algori.pdf
Select three methods in the ObjectList class to work through algori.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdf
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 

More from SHIVA101531

Answer the following questions in a minimum of 1-2 paragraphs ea.docx
Answer the following questions in a minimum of 1-2 paragraphs ea.docxAnswer the following questions in a minimum of 1-2 paragraphs ea.docx
Answer the following questions in a minimum of 1-2 paragraphs ea.docx
SHIVA101531
 
Answer the following questions using scholarly sources as references.docx
Answer the following questions using scholarly sources as references.docxAnswer the following questions using scholarly sources as references.docx
Answer the following questions using scholarly sources as references.docx
SHIVA101531
 
Answer the following questions about this case studyClient .docx
Answer the following questions about this case studyClient .docxAnswer the following questions about this case studyClient .docx
Answer the following questions about this case studyClient .docx
SHIVA101531
 
Answer the following questions using art vocabulary and ideas from L.docx
Answer the following questions using art vocabulary and ideas from L.docxAnswer the following questions using art vocabulary and ideas from L.docx
Answer the following questions using art vocabulary and ideas from L.docx
SHIVA101531
 
Answer the following questions in a total of 3 pages (900 words). My.docx
Answer the following questions in a total of 3 pages (900 words). My.docxAnswer the following questions in a total of 3 pages (900 words). My.docx
Answer the following questions in a total of 3 pages (900 words). My.docx
SHIVA101531
 
Answer the following questions No single word responses (at lea.docx
Answer the following questions No single word responses (at lea.docxAnswer the following questions No single word responses (at lea.docx
Answer the following questions No single word responses (at lea.docx
SHIVA101531
 
Answer the following questions based on the ethnography Dancing Skel.docx
Answer the following questions based on the ethnography Dancing Skel.docxAnswer the following questions based on the ethnography Dancing Skel.docx
Answer the following questions based on the ethnography Dancing Skel.docx
SHIVA101531
 
Answer the following questions to the best of your ability1) De.docx
Answer the following questions to the best of your ability1) De.docxAnswer the following questions to the best of your ability1) De.docx
Answer the following questions to the best of your ability1) De.docx
SHIVA101531
 
Answer the following questionDo you think it is necessary to .docx
Answer the following questionDo you think it is necessary to .docxAnswer the following questionDo you think it is necessary to .docx
Answer the following questionDo you think it is necessary to .docx
SHIVA101531
 
Answer the following question. Use facts and examples to support.docx
Answer the following question. Use facts and examples to support.docxAnswer the following question. Use facts and examples to support.docx
Answer the following question. Use facts and examples to support.docx
SHIVA101531
 
Answer the bottom questions  in apa format and decent answer no shor.docx
Answer the bottom questions  in apa format and decent answer no shor.docxAnswer the bottom questions  in apa format and decent answer no shor.docx
Answer the bottom questions  in apa format and decent answer no shor.docx
SHIVA101531
 
Answer the following below using the EXCEL attachment. chapter 5.docx
Answer the following below using the EXCEL attachment. chapter 5.docxAnswer the following below using the EXCEL attachment. chapter 5.docx
Answer the following below using the EXCEL attachment. chapter 5.docx
SHIVA101531
 
Answer the following prompts about A Germanic People Create a Code .docx
Answer the following prompts about A Germanic People Create a Code .docxAnswer the following prompts about A Germanic People Create a Code .docx
Answer the following prompts about A Germanic People Create a Code .docx
SHIVA101531
 
Answer the following discussion board question below minumun 25.docx
Answer the following discussion board question below minumun 25.docxAnswer the following discussion board question below minumun 25.docx
Answer the following discussion board question below minumun 25.docx
SHIVA101531
 
Answer the following questions about IT Project Management. What.docx
Answer the following questions about IT Project Management. What.docxAnswer the following questions about IT Project Management. What.docx
Answer the following questions about IT Project Management. What.docx
SHIVA101531
 
Answer the following in at least 100 words minimum each1.Of.docx
Answer the following in at least 100 words minimum each1.Of.docxAnswer the following in at least 100 words minimum each1.Of.docx
Answer the following in at least 100 words minimum each1.Of.docx
SHIVA101531
 
Answer the following questions(at least 200 words) and responses 2 p.docx
Answer the following questions(at least 200 words) and responses 2 p.docxAnswer the following questions(at least 200 words) and responses 2 p.docx
Answer the following questions(at least 200 words) and responses 2 p.docx
SHIVA101531
 
Answer the following questions in a Word document and upload it by M.docx
Answer the following questions in a Word document and upload it by M.docxAnswer the following questions in a Word document and upload it by M.docx
Answer the following questions in a Word document and upload it by M.docx
SHIVA101531
 
Answer the following questions in complete sentences. Each answer sh.docx
Answer the following questions in complete sentences. Each answer sh.docxAnswer the following questions in complete sentences. Each answer sh.docx
Answer the following questions in complete sentences. Each answer sh.docx
SHIVA101531
 
ANSWER THE DISCUSSION QUESTION 250 WORDS MINDiscussion Q.docx
ANSWER THE DISCUSSION QUESTION 250 WORDS MINDiscussion Q.docxANSWER THE DISCUSSION QUESTION 250 WORDS MINDiscussion Q.docx
ANSWER THE DISCUSSION QUESTION 250 WORDS MINDiscussion Q.docx
SHIVA101531
 

More from SHIVA101531 (20)

Answer the following questions in a minimum of 1-2 paragraphs ea.docx
Answer the following questions in a minimum of 1-2 paragraphs ea.docxAnswer the following questions in a minimum of 1-2 paragraphs ea.docx
Answer the following questions in a minimum of 1-2 paragraphs ea.docx
 
Answer the following questions using scholarly sources as references.docx
Answer the following questions using scholarly sources as references.docxAnswer the following questions using scholarly sources as references.docx
Answer the following questions using scholarly sources as references.docx
 
Answer the following questions about this case studyClient .docx
Answer the following questions about this case studyClient .docxAnswer the following questions about this case studyClient .docx
Answer the following questions about this case studyClient .docx
 
Answer the following questions using art vocabulary and ideas from L.docx
Answer the following questions using art vocabulary and ideas from L.docxAnswer the following questions using art vocabulary and ideas from L.docx
Answer the following questions using art vocabulary and ideas from L.docx
 
Answer the following questions in a total of 3 pages (900 words). My.docx
Answer the following questions in a total of 3 pages (900 words). My.docxAnswer the following questions in a total of 3 pages (900 words). My.docx
Answer the following questions in a total of 3 pages (900 words). My.docx
 
Answer the following questions No single word responses (at lea.docx
Answer the following questions No single word responses (at lea.docxAnswer the following questions No single word responses (at lea.docx
Answer the following questions No single word responses (at lea.docx
 
Answer the following questions based on the ethnography Dancing Skel.docx
Answer the following questions based on the ethnography Dancing Skel.docxAnswer the following questions based on the ethnography Dancing Skel.docx
Answer the following questions based on the ethnography Dancing Skel.docx
 
Answer the following questions to the best of your ability1) De.docx
Answer the following questions to the best of your ability1) De.docxAnswer the following questions to the best of your ability1) De.docx
Answer the following questions to the best of your ability1) De.docx
 
Answer the following questionDo you think it is necessary to .docx
Answer the following questionDo you think it is necessary to .docxAnswer the following questionDo you think it is necessary to .docx
Answer the following questionDo you think it is necessary to .docx
 
Answer the following question. Use facts and examples to support.docx
Answer the following question. Use facts and examples to support.docxAnswer the following question. Use facts and examples to support.docx
Answer the following question. Use facts and examples to support.docx
 
Answer the bottom questions  in apa format and decent answer no shor.docx
Answer the bottom questions  in apa format and decent answer no shor.docxAnswer the bottom questions  in apa format and decent answer no shor.docx
Answer the bottom questions  in apa format and decent answer no shor.docx
 
Answer the following below using the EXCEL attachment. chapter 5.docx
Answer the following below using the EXCEL attachment. chapter 5.docxAnswer the following below using the EXCEL attachment. chapter 5.docx
Answer the following below using the EXCEL attachment. chapter 5.docx
 
Answer the following prompts about A Germanic People Create a Code .docx
Answer the following prompts about A Germanic People Create a Code .docxAnswer the following prompts about A Germanic People Create a Code .docx
Answer the following prompts about A Germanic People Create a Code .docx
 
Answer the following discussion board question below minumun 25.docx
Answer the following discussion board question below minumun 25.docxAnswer the following discussion board question below minumun 25.docx
Answer the following discussion board question below minumun 25.docx
 
Answer the following questions about IT Project Management. What.docx
Answer the following questions about IT Project Management. What.docxAnswer the following questions about IT Project Management. What.docx
Answer the following questions about IT Project Management. What.docx
 
Answer the following in at least 100 words minimum each1.Of.docx
Answer the following in at least 100 words minimum each1.Of.docxAnswer the following in at least 100 words minimum each1.Of.docx
Answer the following in at least 100 words minimum each1.Of.docx
 
Answer the following questions(at least 200 words) and responses 2 p.docx
Answer the following questions(at least 200 words) and responses 2 p.docxAnswer the following questions(at least 200 words) and responses 2 p.docx
Answer the following questions(at least 200 words) and responses 2 p.docx
 
Answer the following questions in a Word document and upload it by M.docx
Answer the following questions in a Word document and upload it by M.docxAnswer the following questions in a Word document and upload it by M.docx
Answer the following questions in a Word document and upload it by M.docx
 
Answer the following questions in complete sentences. Each answer sh.docx
Answer the following questions in complete sentences. Each answer sh.docxAnswer the following questions in complete sentences. Each answer sh.docx
Answer the following questions in complete sentences. Each answer sh.docx
 
ANSWER THE DISCUSSION QUESTION 250 WORDS MINDiscussion Q.docx
ANSWER THE DISCUSSION QUESTION 250 WORDS MINDiscussion Q.docxANSWER THE DISCUSSION QUESTION 250 WORDS MINDiscussion Q.docx
ANSWER THE DISCUSSION QUESTION 250 WORDS MINDiscussion Q.docx
 

Recently uploaded

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 

Recently uploaded (20)

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 

Lecture 18Dynamic Data Structures and Generics (II).docx

  • 1. Lecture 18 Dynamic Data Structures and Generics (II) * * Class ArrayListObject of an ArrayList can be used to store an unlimited number of objects. * Methods of ArrayList (I) * java.util.ArrayList +ArrayList() +add(o: Object) : void +add(index: int, o: Object) : void
  • 2. +clear(): void +contains(o: Object): boolean +get(index: int) : Object +indexOf(o: Object) : int +isEmpty(): boolean +lastIndexOf(o: Object) : int +remove(o: Object): boolean +size(): int +remove(index: int) : Object +set(index: int, o: Object) : Object Appends a new element o at the end of this list.
  • 3. Adds a new element o at the specified index in this list. Removes all the elements from this list. Returns true if this list contains the element o. Returns the element from this list at the specified index. Returns the index of the first matching element in this list. Returns true if this list contains no elements. Returns the index of the last matching element in this list. Removes the element o from this list. Returns the number of elements in this list. Removes the element at the specified index. Sets the element at the specified index.
  • 4. Creates an empty list. Methods of ArrayList (II) Array is used to implement the methodsMethods 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. * Linked Data StructureTo improve efficiency for adding and removing an element anywhere in a list.It containsCollection of objectsEach object contains data and a reference to another object in the collection * Linked ListA dynamic data structureA linked list consists of nodes. Each node contains an elementa link: linked to its next neighbor. Example: take notes in the class!
  • 5. * Linked List * ListNodes 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; } * ListNodes in Linked List (cont’d)
  • 6. /** */ public void setData (String newData) { data = newData; } /** */ public String getData () { return data; } * ListNodes in Linked List (cont’d) /** */ public void setLink (ListNode newLink) { link = newLink; } /** */ public ListNode getLink () { return link; } } * Linked List ClassA linked list class uses the ListNode classThe variable head refers to the first node in the list. * Linked List of String public class StringLinkedList { //write comments
  • 7. private ListNode head; /** */ public StringLinkedList () { head = null; // } * Linked List of String (cont’d) /** */ public void showList () { ListNode position = head; // while (position != null) { System.out.println (position.getData ()); position = position.getLink (); // } } * Linked List of String (cont’d) /** */ public int length () { int count = 0; ListNode position = head; // while (position != null) { // count++; // position = position.getLink (); // } return count; } *
  • 8. 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); } } * 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; }
  • 9. } * Linked List of String (cont’d) /** */ public boolean onList (String target) { return find (target) != null; } * 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 listDelete the first course you addedcheck some courses whether they are in the list or not. * java.util.ArrayList + ArrayList () +add(o: Object) : void +add(index: int, o: Object) : void +clear(): void +contains(o: Object): boolean +get(index: int) : Object
  • 10. +indexOf(o: Object) : int +isEmpty(): boolean +lastIndexOf(o: Object) : int +remove(o: Object): boolean +size(): int +remove(index: int) : Object +set(index: int, o: Object) : Object Appends a new element o at the end of this list. Adds a new element o at the specified index in this list. Removes all the elements from this list. Returns t rue if this list contains the element o. Returns the element from this list at the specified index. Returns the index of the first matching element in this list. Returns true if this list contains no elements. Returns the index of the last matching elemen t in this list. Removes the element o from this list.
  • 11. Returns the number of elements in this list. Removes the element at the specified index. Sets the element at the specified index. Creates an empty list.