subject

Please in Java, with any comments! Thank you. In the LinkedList class, write the new method public void add(int index, E newValue). This method should add newValue to the list at the specified index. If the index is invalid for the list, throw an .
(You can delete and/or add on to the already made add method within this code, and if you perform any tests in the main method please show it, if not no worries, more curious for the actual method code than testing code. )
public class LinkedList implements List {
// This instance variable keeps track of the head node of the list.
// From that head node, we can get anywhere else in the list.
private Node head;
// This instance variable keeps track of how many elements are currently in the list
private int size = 0;
// Returns the list element at the specified index
public E get(int index) {
if (index >= 0 && index < size)
return nodeAt(index).getData();
else
throw new ();
}
// Replaces the list element at an existing index with a new value
public void set(int index, E newValue) {
if (index >= 0 && index < size)
nodeAt(index).setData(newValue);
else
throw new ();
}
// Adds a new element to the end of the list
public void add(E newValue) {
// Create a new node containing the newValue
Node newNode = new Node<>(newValue, null);
if (size == 0) { // If the list is empty...
// Point the head reference to the new node
head = newNode;
}
else { // If the list is not empty...
// Get to the last node in the list, and set its next to the new node
nodeAt(size - 1).setNext(newNode);
}
size++;
}
// Removes and returns the list element at the specified index
// Method stub - to be implemented later
public E remove(int index) {
return null;
}
public String toString() {
String result = "LinkedList object (size = " + size + "), elements: head -> ";
// The commented out loop below works, but it's inefficient because *every*
// call to nodeAt involves a loop
// for (int i = 0; i < size; i++)
// result += nodeAt(i).getData() + " -> ";
// Better - just a single loop through the list is needed
for (Node temp = head; temp != null; temp = temp. getNext())
result += temp. getData() + " -> ";
result += "null";
return result;
}
// Returns the Node object at the specified index in the list
// Declared private, since nodeAt is meant to be called only by other
// methods within this class
private Node nodeAt(int index) {
Node temp = head;
for (int i = 0; i < index; i++) // Runs for "index" iterations
temp = temp. getNext(); // Each time this runs, temp advances down the list by one node
return temp;
}
public static void main(String[] args) {
List test = new LinkedList<>();
System. out. println(test);
test. add(14);
System. out. println(test);
test. add(8);
System. out. println(test);
test. add(3);
System. out. println(test);
}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 19:30
When using a public computer or network, you should always
Answers: 2
question
Computers and Technology, 23.06.2019 02:00
Arecipients list has been loaded into a document. which commands should be clicked in order to filter the list so that letters will not be printed for recipients who live in a certain state? mailings tab, start mail merge, select recipients, type new list, then insert only contacts from the desired states mailings tab, rules, select recipients, use existing list, then choose a recipients list that includes only contacts in certain states mailings tab, select recipients, use existing list, rules, fill in, then type in certain states mailings tab, rules, skip record select β€œstate” under field name, then type in the state name under β€œequal to”
Answers: 2
question
Computers and Technology, 23.06.2019 08:00
What is a scenario where records stored in a computer frequently need to be checked
Answers: 2
question
Computers and Technology, 24.06.2019 18:20
The following if statement contains a logic error, not a syntax error. rewrite it so that it is correct. assume the variable age already exists and holds a valid number. if (age == 18 & & age == 19) {
Answers: 1
You know the right answer?
Please in Java, with any comments! Thank you. In the LinkedList class, write the new method public...
Questions
question
Physics, 22.06.2019 12:30
question
Mathematics, 22.06.2019 12:30
Questions on the website: 13722362