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

PDF
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
PDF
For this lab you will complete the class MyArrayList by implementing.pdf
PDF
Hi,I have added the methods and main class as per your requirement.pdf
DOCX
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
PDF
Implementation The starter code includes List.java. You should not c.pdf
PDF
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
PDF
Please help me to make a programming project I have to sue them today- (1).pdf
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
Hi,I have added the methods and main class as per your requirement.pdf
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
Implementation The starter code includes List.java. You should not c.pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Please help me to make a programming project I have to sue them today- (1).pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf

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

DOCX
Please complete all the code as per instructions in Java programming.docx
PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
PDF
Note- Can someone help me with the Public boolean add(E value) method.pdf
PDF
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
DOCX
Note- Can someone help me with the private E get(int index- int curren (1).docx
PPT
Jhtp5 20 Datastructures
PDF
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
PDF
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
PDF
In this assignment you will implement insert() method for a singly l.pdf
PDF
package linkedLists- import java-util-Iterator- --- A class representi.pdf
PDF
I need help with this code working Create another project and add yo.pdf
PPT
Csphtp1 23
PPT
Csphtp1 23
PDF
File LinkedList.java Defines a doubly-l.pdf
PPTX
Adt of lists
PDF
Select three methods in the ObjectList class to work through algori.pdf
PDF
The LinkedList1 class implements a Linked list. class.pdf
PPTX
Chapter 4 Linked List introduction lessons.pptx
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
Java AssignmentUsing the ListNode.java file below Write method.pdf
Please complete all the code as per instructions in Java programming.docx
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
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
Jhtp5 20 Datastructures
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
In this assignment you will implement insert() method for a singly l.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
Csphtp1 23
Csphtp1 23
File LinkedList.java Defines a doubly-l.pdf
Adt of lists
Select three methods in the ObjectList class to work through algori.pdf
The LinkedList1 class implements a Linked list. class.pdf
Chapter 4 Linked List introduction lessons.pptx
public class MyLinkedListltE extends ComparableltEgtg.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdf
Ad

More from SHIVA101531 (20)

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

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
master seminar digital applications in india
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Institutional Correction lecture only . . .
O5-L3 Freight Transport Ops (International) V1.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Anesthesia in Laparoscopic Surgery in India
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Computing-Curriculum for Schools in Ghana
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial disease of the cardiovascular and lymphatic systems
Microbial diseases, their pathogenesis and prophylaxis
Supply Chain Operations Speaking Notes -ICLT Program
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
master seminar digital applications in india
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Institutional Correction lecture only . . .

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.