SlideShare a Scribd company logo
1 of 9
Download to read offline
Help I keep getting the same error when running a code. Below is the code and the instructions
about the code.
import java.util.Arrays;
public class Album implements Comparable {
private final int id;
private final String[] artists;
private final int numSongs;
public Album(int id, String title, String[] artists, int numSongs) {
this.id = id;
this.artists = new String[]{Arrays.toString(artists)};
this.numSongs = numSongs;
}
public int getId() {
return id;
}
@Override
public int compareTo(Album other) {
return Integer.compare(this.numSongs, other.numSongs);
}
@Override
public String toString() {
StringBuilder artistNames = new StringBuilder();
for (String artist : artists) {
artistNames.append(artist).append(", ");
}
artistNames.delete(artistNames.length() - 2, artistNames.length());
return "ID: " + id + " -- " + numSongs + " songs -- [" + artistNames + "]";
}
}
import java.util.Random;
public class DoublyLinkedList {
private static class Node {
T data;
Node prev;
Node next;
Node(T data) {
this.data = data;
}
}
private Node head;
private Node tail;
private int size;
public DoublyLinkedList() {
head = null;
tail = null;
size = 0;
}
public Node append(T data) {
Node newNode = new Node<>(data);
if (head == null) {
head = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
}
tail = newNode;
size++;
return newNode;
}
public Node insert(int location, T data) {
if (location < 0 || location > size) {
throw new IllegalArgumentException("Invalid location");
}
Node newNode = new Node<>(data);
if (location == 0) {
newNode.next = head;
if (head != null) {
head.prev = newNode;
}
head = newNode;
if (tail == null) {
tail = newNode;
}
} else if (location == size) {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
} else {
Node current = head;
for (int i = 0; i < location; i++) {
current = current.next;
}
newNode.prev = current.prev;
newNode.next = current;
current.prev.next = newNode;
current.prev = newNode;
}
size++;
return newNode;
}
public Node delete(int location) {
if (location < 0 || location >= size) {
throw new IllegalArgumentException("Invalid location");
}
Node deletedNode;
if (location == 0) {
deletedNode = head;
head = head.next;
if (head != null) {
head.prev = null;
} else {
tail = null;
}
} else if (location == size - 1) {
deletedNode = tail;
tail = tail.prev;
tail.next = null;
} else {
Node current = head;
for (int i = 0; i < location; i++) {
current = current.next;
}
deletedNode = current;
current.prev.next = current.next;
current.next.prev = current.prev;
}
size--;
return deletedNode;
}
public int getIndex(T data) {
Node current = head;
for (int i = 0; i < size; i++) {
if (current.data.equals(data)) {
return i;
}
current = current.next;
}
return -1;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
Node current = head;
while (current != null) {
result.append(current.data).append(" -> ");
current = current.next;
}
result.append("NULL");
return result.toString();
}
public Node shuffle() {
Random rand = new Random();
for (int i = size - 1; i > 0; i--) {
int j = rand.nextInt(i + 1);
swapNodes(i, j);
}
return head;
}
private void swapNodes(int i, int j) {
if (i == j) {
return;
}
Node node1 = getNodeAtIndex(i);
Node node2 = getNodeAtIndex(j);
Node tempPrev1 = node1.prev;
Node tempNext1 = node1.next;
if (node1.next == node2) {
// Nodes are adjacent
node1.next = node2.next;
node2.prev = node1.prev;
node2.next = node1;
node1.prev = node2;
if (tempPrev1 != null) {
tempPrev1.next = node2;
}
if (node1.next != null) {
node1.next.prev = node1;
}
} else if (node2.next == node1) {
// Nodes are adjacent, but in reverse order
node2.next = node1.next;
node1.prev = node2.prev;
node1.next = node2;
node2.prev = node1;
if (tempPrev1 != null) {
tempPrev1.next = node2;
}
if (node2.next != null) {
node2.next.prev = node2;
}
} else {
// Nodes are not adjacent
node1.next = node2.next;
node2.prev = tempPrev1;
node2.next = tempNext1;
if (tempPrev1 != null) {
tempPrev1.next = node2;
}
if (tempNext1 != null) {
tempNext1.prev = node1;
}
if (node1.next != null) {
node1.next.prev = node1;
}
if (node2.next != null) {
node2.next.prev = node2;
}
}
if (i == 0) {
head = node2;
} else if (j == 0) {
head = node1;
}
if (i == size - 1) {
tail = node2;
} else if (j == size - 1) {
tail = node1;
}
}
public DoublyLinkedList partition(T data) {
DoublyLinkedList newList = new DoublyLinkedList<>();
Node current = head;
while (current != null) {
if (((Comparable) current.data).compareTo(data) >= 0) {
newList.append(current.data);
delete(getIndex(current.data));
} else {
current = current.next;
}
}
return newList;
}
private Node getNodeAtIndex(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Invalid index");
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
}
{
public static void main(String[] args) {
// Create an empty DLL
DoublyLinkedList dll = new DoublyLinkedList<>();
// Create albums
Album album1 = new Album(1, "Album 1", new String[]{"Artist A", "Artist B"}, 10);
Album album2 = new Album(2, "Album 2", new String[]{"Artist C"}, 15);
// Append albums to DLL
dll.append(album1);
dll.append(album2);
// Test append()
Album album5 = new Album(5, "Album 5", new String[]{"Artist G"}, 7);
dll.append(album5);
System.out.println("DLL after append: " + dll);
// Test toString()
System.out.println("DLL: " + dll);
// Test insert()
Album album4 = new Album(4, "Album 4", new String[]{"Artist F"}, 12);
dll.insert(1, album4);
System.out.println("DLL after insert: " + dll);
// Test delete()
dll.delete(2);
System.out.println("DLL after delete: " + dll);
// Test getIndex()
System.out.println("Index of album2: " + dll.getIndex(album2));
// Test shuffle()
dll.shuffle();
System.out.println("Shuffled DLL: " + dll);
// Test partition()
DoublyLinkedList newList = dll.partition(album1);
System.out.println("New List after partition: " + newList);
System.out.println("Original DLL after partition: " + dll);
}
}c: Users Fleegyboy .jdksopenjdk-20.0.1binjava.exe ... DLL after append: ID: 1 -- 10 songs
-- [[Artist A, Artist B]] -> ID: 2 -- 15 songs -- [[Artist C]] -> ID: 5 -- 7 songs -- [[Artist G]] ->
NULL DLL: ID: 1 -- 10 songs -- [[Artist A, Artist B]] -> ID: 2 -- 15 songs -- [[Artist C]] -> ID: 5
-- 7 songs -- [[Artist G]] -> NULL after insert: ID: 1 -- 10 songs -- [[Artist A, Artist B]] -> ID: 4
-- 12 songs -- [[Artist F]] -> ID: 2 -- 15 songs -- [[Artist C]] -> ID: 5 -- 7 after delete: ID: 1 -- 10
songs -- [[Artist A, Artist B]] -> ID: 4 -- 12 songs -- [[Artist F]] -> ID: 5 -- 7 songs -- [[Artist G]]
-> NULL Index of album2: -1 Shuffled DLL: ID: 1 -- 10 songs -- [[Artist A, Artist B]] -> ID: 4 -
- 12 songs -- [[Artist F]] -> ID: 5 -- 7 songs -- [[Artist G]] -> NULL Exception in thread "main"
java.lang.IlleqalArqumentException Create breakpoint: Invalid location at
DoublyLinkedList.delete (DoublyLinkedList.java:73) at DoublyLinkedList.partition
(DoublyLinkedList. java:208) at DoublyLinkedListTest.main (DoublyLinkedListTest. java:37)
Process finished with exit code 1

More Related Content

Similar to Help I keep getting the same error when running a code. Below is the.pdf

How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfarccreation001
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxLeonardN9WWelchw
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfaathiauto
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdffashionbigchennai
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfABHISHEKREADYMADESKO
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfmichardsonkhaicarr37
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
 
fix the error - class Node{ int data- Node next-.pdf
fix the error -   class Node{           int data-           Node next-.pdffix the error -   class Node{           int data-           Node next-.pdf
fix the error - class Node{ int data- Node next-.pdfAKVIGFOEU
 
double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdffacevenky
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfezycolours78
 
Please change this method to recursive method.  public String post.pdf
Please change this method to recursive method.  public String post.pdfPlease change this method to recursive method.  public String post.pdf
Please change this method to recursive method.  public String post.pdfpallavi953613
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfferoz544
 

Similar to Help I keep getting the same error when running a code. Below is the.pdf (20)

How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdf
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdf
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
fix the error - class Node{ int data- Node next-.pdf
fix the error -   class Node{           int data-           Node next-.pdffix the error -   class Node{           int data-           Node next-.pdf
fix the error - class Node{ int data- Node next-.pdf
 
double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdf
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
Please change this method to recursive method.  public String post.pdf
Please change this method to recursive method.  public String post.pdfPlease change this method to recursive method.  public String post.pdf
Please change this method to recursive method.  public String post.pdf
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
 

More from mail931892

I need help on this 5 and 6. here is the code so far import jav.pdf
I need help on this 5 and 6. here is the code so far import jav.pdfI need help on this 5 and 6. here is the code so far import jav.pdf
I need help on this 5 and 6. here is the code so far import jav.pdfmail931892
 
I need a substantive comment on this postThe formal structure of .pdf
I need a substantive comment on this postThe formal structure of .pdfI need a substantive comment on this postThe formal structure of .pdf
I need a substantive comment on this postThe formal structure of .pdfmail931892
 
I need help debugging some code. I am trying to read a text file and.pdf
I need help debugging some code. I am trying to read a text file and.pdfI need help debugging some code. I am trying to read a text file and.pdf
I need help debugging some code. I am trying to read a text file and.pdfmail931892
 
I have these files in picture I wrote most the codes but I stuck wit.pdf
I have these files in picture I wrote most the codes but I stuck wit.pdfI have these files in picture I wrote most the codes but I stuck wit.pdf
I have these files in picture I wrote most the codes but I stuck wit.pdfmail931892
 
I am trying to create a dynamic web project in Eclipse IDE that impl.pdf
I am trying to create a dynamic web project in Eclipse IDE that impl.pdfI am trying to create a dynamic web project in Eclipse IDE that impl.pdf
I am trying to create a dynamic web project in Eclipse IDE that impl.pdfmail931892
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfmail931892
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfmail931892
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfmail931892
 
how can i build this as problem 2 of my code #include iostream.pdf
how can i build this as problem 2 of my code #include iostream.pdfhow can i build this as problem 2 of my code #include iostream.pdf
how can i build this as problem 2 of my code #include iostream.pdfmail931892
 
Hi All!I have a question regrading creating a subgraph or rather v.pdf
Hi All!I have a question regrading creating a subgraph or rather v.pdfHi All!I have a question regrading creating a subgraph or rather v.pdf
Hi All!I have a question regrading creating a subgraph or rather v.pdfmail931892
 
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfHeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfmail931892
 
For this part you will need to analyze the provided logfile part2.lo.pdf
For this part you will need to analyze the provided logfile part2.lo.pdfFor this part you will need to analyze the provided logfile part2.lo.pdf
For this part you will need to analyze the provided logfile part2.lo.pdfmail931892
 
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdf
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdfGinny (45) is unmarried. Who of the following may be Ginnys quali.pdf
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdfmail931892
 

More from mail931892 (13)

I need help on this 5 and 6. here is the code so far import jav.pdf
I need help on this 5 and 6. here is the code so far import jav.pdfI need help on this 5 and 6. here is the code so far import jav.pdf
I need help on this 5 and 6. here is the code so far import jav.pdf
 
I need a substantive comment on this postThe formal structure of .pdf
I need a substantive comment on this postThe formal structure of .pdfI need a substantive comment on this postThe formal structure of .pdf
I need a substantive comment on this postThe formal structure of .pdf
 
I need help debugging some code. I am trying to read a text file and.pdf
I need help debugging some code. I am trying to read a text file and.pdfI need help debugging some code. I am trying to read a text file and.pdf
I need help debugging some code. I am trying to read a text file and.pdf
 
I have these files in picture I wrote most the codes but I stuck wit.pdf
I have these files in picture I wrote most the codes but I stuck wit.pdfI have these files in picture I wrote most the codes but I stuck wit.pdf
I have these files in picture I wrote most the codes but I stuck wit.pdf
 
I am trying to create a dynamic web project in Eclipse IDE that impl.pdf
I am trying to create a dynamic web project in Eclipse IDE that impl.pdfI am trying to create a dynamic web project in Eclipse IDE that impl.pdf
I am trying to create a dynamic web project in Eclipse IDE that impl.pdf
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 
how can i build this as problem 2 of my code #include iostream.pdf
how can i build this as problem 2 of my code #include iostream.pdfhow can i build this as problem 2 of my code #include iostream.pdf
how can i build this as problem 2 of my code #include iostream.pdf
 
Hi All!I have a question regrading creating a subgraph or rather v.pdf
Hi All!I have a question regrading creating a subgraph or rather v.pdfHi All!I have a question regrading creating a subgraph or rather v.pdf
Hi All!I have a question regrading creating a subgraph or rather v.pdf
 
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfHeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
 
For this part you will need to analyze the provided logfile part2.lo.pdf
For this part you will need to analyze the provided logfile part2.lo.pdfFor this part you will need to analyze the provided logfile part2.lo.pdf
For this part you will need to analyze the provided logfile part2.lo.pdf
 
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdf
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdfGinny (45) is unmarried. Who of the following may be Ginnys quali.pdf
Ginny (45) is unmarried. Who of the following may be Ginnys quali.pdf
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

Help I keep getting the same error when running a code. Below is the.pdf

  • 1. Help I keep getting the same error when running a code. Below is the code and the instructions about the code. import java.util.Arrays; public class Album implements Comparable { private final int id; private final String[] artists; private final int numSongs; public Album(int id, String title, String[] artists, int numSongs) { this.id = id; this.artists = new String[]{Arrays.toString(artists)}; this.numSongs = numSongs; } public int getId() { return id; } @Override public int compareTo(Album other) { return Integer.compare(this.numSongs, other.numSongs); } @Override public String toString() { StringBuilder artistNames = new StringBuilder(); for (String artist : artists) { artistNames.append(artist).append(", "); } artistNames.delete(artistNames.length() - 2, artistNames.length()); return "ID: " + id + " -- " + numSongs + " songs -- [" + artistNames + "]"; } } import java.util.Random;
  • 2. public class DoublyLinkedList { private static class Node { T data; Node prev; Node next; Node(T data) { this.data = data; } } private Node head; private Node tail; private int size; public DoublyLinkedList() { head = null; tail = null; size = 0; } public Node append(T data) { Node newNode = new Node<>(data); if (head == null) { head = newNode; } else { tail.next = newNode; newNode.prev = tail; } tail = newNode; size++; return newNode; } public Node insert(int location, T data) {
  • 3. if (location < 0 || location > size) { throw new IllegalArgumentException("Invalid location"); } Node newNode = new Node<>(data); if (location == 0) { newNode.next = head; if (head != null) { head.prev = newNode; } head = newNode; if (tail == null) { tail = newNode; } } else if (location == size) { tail.next = newNode; newNode.prev = tail; tail = newNode; } else { Node current = head; for (int i = 0; i < location; i++) { current = current.next; } newNode.prev = current.prev; newNode.next = current; current.prev.next = newNode; current.prev = newNode; } size++; return newNode; } public Node delete(int location) { if (location < 0 || location >= size) { throw new IllegalArgumentException("Invalid location");
  • 4. } Node deletedNode; if (location == 0) { deletedNode = head; head = head.next; if (head != null) { head.prev = null; } else { tail = null; } } else if (location == size - 1) { deletedNode = tail; tail = tail.prev; tail.next = null; } else { Node current = head; for (int i = 0; i < location; i++) { current = current.next; } deletedNode = current; current.prev.next = current.next; current.next.prev = current.prev; } size--; return deletedNode; } public int getIndex(T data) { Node current = head; for (int i = 0; i < size; i++) { if (current.data.equals(data)) { return i; } current = current.next;
  • 5. } return -1; } @Override public String toString() { StringBuilder result = new StringBuilder(); Node current = head; while (current != null) { result.append(current.data).append(" -> "); current = current.next; } result.append("NULL"); return result.toString(); } public Node shuffle() { Random rand = new Random(); for (int i = size - 1; i > 0; i--) { int j = rand.nextInt(i + 1); swapNodes(i, j); } return head; } private void swapNodes(int i, int j) { if (i == j) { return; } Node node1 = getNodeAtIndex(i); Node node2 = getNodeAtIndex(j); Node tempPrev1 = node1.prev; Node tempNext1 = node1.next;
  • 6. if (node1.next == node2) { // Nodes are adjacent node1.next = node2.next; node2.prev = node1.prev; node2.next = node1; node1.prev = node2; if (tempPrev1 != null) { tempPrev1.next = node2; } if (node1.next != null) { node1.next.prev = node1; } } else if (node2.next == node1) { // Nodes are adjacent, but in reverse order node2.next = node1.next; node1.prev = node2.prev; node1.next = node2; node2.prev = node1; if (tempPrev1 != null) { tempPrev1.next = node2; } if (node2.next != null) { node2.next.prev = node2; } } else { // Nodes are not adjacent node1.next = node2.next; node2.prev = tempPrev1; node2.next = tempNext1; if (tempPrev1 != null) { tempPrev1.next = node2; } if (tempNext1 != null) { tempNext1.prev = node1; } if (node1.next != null) {
  • 7. node1.next.prev = node1; } if (node2.next != null) { node2.next.prev = node2; } } if (i == 0) { head = node2; } else if (j == 0) { head = node1; } if (i == size - 1) { tail = node2; } else if (j == size - 1) { tail = node1; } } public DoublyLinkedList partition(T data) { DoublyLinkedList newList = new DoublyLinkedList<>(); Node current = head; while (current != null) { if (((Comparable) current.data).compareTo(data) >= 0) { newList.append(current.data); delete(getIndex(current.data)); } else { current = current.next; } } return newList; } private Node getNodeAtIndex(int index) {
  • 8. if (index < 0 || index >= size) { throw new IllegalArgumentException("Invalid index"); } Node current = head; for (int i = 0; i < index; i++) { current = current.next; } return current; } } { public static void main(String[] args) { // Create an empty DLL DoublyLinkedList dll = new DoublyLinkedList<>(); // Create albums Album album1 = new Album(1, "Album 1", new String[]{"Artist A", "Artist B"}, 10); Album album2 = new Album(2, "Album 2", new String[]{"Artist C"}, 15); // Append albums to DLL dll.append(album1); dll.append(album2); // Test append() Album album5 = new Album(5, "Album 5", new String[]{"Artist G"}, 7); dll.append(album5); System.out.println("DLL after append: " + dll); // Test toString() System.out.println("DLL: " + dll); // Test insert() Album album4 = new Album(4, "Album 4", new String[]{"Artist F"}, 12); dll.insert(1, album4); System.out.println("DLL after insert: " + dll); // Test delete() dll.delete(2);
  • 9. System.out.println("DLL after delete: " + dll); // Test getIndex() System.out.println("Index of album2: " + dll.getIndex(album2)); // Test shuffle() dll.shuffle(); System.out.println("Shuffled DLL: " + dll); // Test partition() DoublyLinkedList newList = dll.partition(album1); System.out.println("New List after partition: " + newList); System.out.println("Original DLL after partition: " + dll); } }c: Users Fleegyboy .jdksopenjdk-20.0.1binjava.exe ... DLL after append: ID: 1 -- 10 songs -- [[Artist A, Artist B]] -> ID: 2 -- 15 songs -- [[Artist C]] -> ID: 5 -- 7 songs -- [[Artist G]] -> NULL DLL: ID: 1 -- 10 songs -- [[Artist A, Artist B]] -> ID: 2 -- 15 songs -- [[Artist C]] -> ID: 5 -- 7 songs -- [[Artist G]] -> NULL after insert: ID: 1 -- 10 songs -- [[Artist A, Artist B]] -> ID: 4 -- 12 songs -- [[Artist F]] -> ID: 2 -- 15 songs -- [[Artist C]] -> ID: 5 -- 7 after delete: ID: 1 -- 10 songs -- [[Artist A, Artist B]] -> ID: 4 -- 12 songs -- [[Artist F]] -> ID: 5 -- 7 songs -- [[Artist G]] -> NULL Index of album2: -1 Shuffled DLL: ID: 1 -- 10 songs -- [[Artist A, Artist B]] -> ID: 4 - - 12 songs -- [[Artist F]] -> ID: 5 -- 7 songs -- [[Artist G]] -> NULL Exception in thread "main" java.lang.IlleqalArqumentException Create breakpoint: Invalid location at DoublyLinkedList.delete (DoublyLinkedList.java:73) at DoublyLinkedList.partition (DoublyLinkedList. java:208) at DoublyLinkedListTest.main (DoublyLinkedListTest. java:37) Process finished with exit code 1