Question: i need help to fix the code in bold as i am trying to make my toarraylist method return a list of elements from my
i need help to fix the code in bold as i am trying to make my toarraylist method return a list of elements from my linked list and make it work with its test so please look at my code fix it and tell me what i did wrong thank you.
the class with arraylist method i need help fixin in bold
package test2; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException;
import javafx.scene.Node;
public class testing { protected Node head; protected Node tail; protected int size; private T[] data;//Array in which the elements in this list are stored. private ArrayList list; public Iteratoriterator;
public testing() { size =0; } /** * this class keeps track of each element information * @author * */ class Node { private T data; private Node next; private Node prev; Node (T data, Node next,Node prev) { this.setData(data); this.setNext(next); this.setPrev(prev); } public T getData() {return data;} public void setData(T data) {this.data = data;} public Node getNext() {return next;} public void setNext(Node next) {this.next = next; } public Node getPrev() {return prev; } public void setPrev(Node prev) {this.prev = prev;} } public testing addToEnd(T data) { Node newNode = new Node(data, null, getTail()); if(getTail()!=null) {getTail().setNext(newNode);} setTail(newNode); if(getHead()==null) { setHead(newNode); } size++; System.out.println(data); return this;} public testing addToFront(T data) { Node newNode = new Node(data, getHead(), getTail()); if(getHead()!=null) {getHead().setPrev(newNode);} if(getTail()==null) { setTail(newNode); } setHead(newNode); size++; System.out.println(data); return this;} public T getFirst() { if(getHead()!=null) return getHead().data; else return null;} public T getLast() { if(getTail()!=null) return getTail().data;else return null;} /**Returns an arraylist of the items in the list from head of list to tail of list * @return an arraylist of the items in the list */ public ArrayList toArrayList() { //Node newNode=new Node(null, head, tail); // = null; ArrayList list = new ArrayList(); for (int i=0;i<=list.size();i++) { //list.get(i); list.add(data[i]); list.getClass(); System.out.println(data[i]);} return list; } public Node getHead() { return head; } public void setHead(Node head) { this.head = head;} public Node getTail() { return tail; } public void setTail(Node tail) { this.tail = tail;}}
its test method
package test2;
import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.Comparator; import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException; import org.junit.After; import org.junit.Before; import org.junit.Test; public class testingTest { testing linkedString; testing linkedDouble; StringComparator comparator; DoubleComparator comparatorD; @Before public void setUp() throws Exception { linkedString = new testing(); linkedString.addToEnd("Hello"); linkedString.addToEnd("World"); comparator = new StringComparator(); //STUDENT: Use the linkedDouble for the STUDENT tests linkedDouble = new testing(); linkedDouble.addToEnd(15.0); linkedDouble.addToEnd(100.0); comparatorD = new DoubleComparator(); } @After public void tearDown() throws Exception { linkedString = null; comparator = null; } @Test public void testAddToEnd() { assertEquals("World", linkedString.getLast()); linkedString.addToEnd("End"); assertEquals("End", linkedString.getLast()); } @Test public void testAddToEndSTUDENT(){ //test addToEnd for the linkedDouble assertEquals("World", linkedString.getLast()); linkedString.addToEnd("End"); assertEquals("End", linkedString.getLast()); } @Test public void testAddToFront() { assertEquals("Hello", linkedString.getFirst()); linkedString.addToFront("Begin"); assertEquals("Begin", linkedString.getFirst()); } @Test public void testAddToFrontSTUDENT(){ //test addToFront for the linkedDouble assertEquals("Hello", linkedString.getFirst()); linkedString.addToFront("Begin"); assertEquals("Begin", linkedString.getFirst()); } @Test public void testToArrayList() { ArrayList list; linkedString.addToFront("Begin"); linkedString.addToEnd("End"); list = linkedString.toArrayList(); assertEquals("Begin", list.get(0)); assertEquals("Hello", list.get(1)); assertEquals("World", list.get(2)); assertEquals("End", list.get(3)); } private class StringComparator implements Comparator { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub return arg0.compareTo(arg1); } } private class DoubleComparator implements Comparator { @Override public int compare(Double arg0, Double arg1) { // TODO Auto-generated method stub return arg0.compareTo(arg1); } } }