SlideShare a Scribd company logo
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
Ad

More Related Content

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

In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
arjunstores123
 
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
feelinggift
 
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
UNIT I LINEAR DATA STRUCTURES – LIST .pptxUNIT I LINEAR DATA STRUCTURES – LIST .pptx
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
VISWANATHAN R V
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
Arturo Herrero
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
Programming 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
arccreation001
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
21E135MAHIESHWARJ
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
Chaitanya Kn
 
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
LeonardN9WWelchw
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
teyaj1
 
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
aathiauto
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
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
fashionbigchennai
 
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
ABHISHEKREADYMADESKO
 
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
michardsonkhaicarr37
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
Programming 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
arrowmobile
 
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
AKVIGFOEU
 
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
facevenky
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
arjunstores123
 
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
feelinggift
 
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
UNIT I LINEAR DATA STRUCTURES – LIST .pptxUNIT I LINEAR DATA STRUCTURES – LIST .pptx
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
VISWANATHAN R V
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
Arturo 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.pdf
arccreation001
 
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
LeonardN9WWelchw
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
teyaj1
 
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
aathiauto
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
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
fashionbigchennai
 
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
ABHISHEKREADYMADESKO
 
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
michardsonkhaicarr37
 
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
arrowmobile
 
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
AKVIGFOEU
 
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
facevenky
 

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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
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.pdf
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
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
mail931892
 
Ad

Recently uploaded (20)

Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdfAPM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
Association for Project Management
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Ad

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