Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating A Linked List In Java Using Generics
    primarykey
    data
    text
    <p>I've looked through the site and can't find anything that is at least comparable to what I'm doing (please do not link any stackoverflow responses or questions I've been looking for an hour), so I've decided to ask this question. I'm trying to create a linked list (not using libraries of any kind) that uses generics, here is all the relevant code.</p> <pre><code>public class LList&lt;T&gt; { MyNode&lt;T&gt; head = null; MyNode&lt;T&gt; tail = null; MyNode&lt;T&gt; temp = null; int count = 0;; Scanner scan = new Scanner(System.in); //puts a new value at the edn of the list public void add(T i) { if(head == null) { head = new MyNode&lt;T&gt;(); head.data = i; head.next = tail; tail = head; } else { tail.next = new MyNode&lt;T&gt;(); tail = tail.next; tail.data = i; } count ++; } //inserts a new value at a given index public void insert(int index, T item) { if(index == size()) { add(item); } else if(index == 0) { MyNode&lt;T&gt; temp = new MyNode&lt;T&gt;(); temp.data = item; temp.next = head; head.previous = temp; head = temp; count++; } temp = head; for(int i = 0; i &lt; index-1; i++) { temp = temp.next; MyNode&lt;T&gt; myNode = new MyNode&lt;T&gt;(); myNode.data = item; myNode.next = temp.next; temp.next = myNode; count++; } } //returns the number of values in the list public int size() { return count; } //returns the value at a given index public T get(int index) { if(head == null || index &gt; count -1) { return null; } if(index &lt; 0 || index &gt;= count) { System.out.println("This does not exist"); } MyNode&lt;T&gt; p = head; int size = 0; while(size &lt; index &amp;&amp; p.next != null) { p = p.next; size++; } if(count != index) { return null; } else { return p.data; } } //removes the returns the first value in the list public T remove() { head = head.next; head.previous = null; count--; return (T) head; } //removes and returns the value at a given index public T removeAt(T elem) { temp = head; MyNode&lt;T&gt; two = null; if(head.data.equals(elem)) { head = head.next; head.previous = null; count--; return elem; } else if(tail.data.equals(elem)) { tail = tail.previous; tail.next = null; count--; return elem; } while(temp != null &amp;&amp; !temp.data.equals(elem)) { two = temp; temp = temp.next; } if(temp == null) { return null; } two.next = temp.next; T spare = temp.data; temp = null; count--; return spare; } //removes and returns the last value in the list public T removeLast() { temp = tail; tail = tail.previous; temp = null; count--; return (T) tail; } //creates a string representation of all the values in the list public String toString() { String result = null; for(int i = 0; i &lt; count; i++) { result = i + " : " + get(i).toString(); } return result; } //removes all the values in the list public void clear() { for(int i = count -1; i &gt;= 0; i++) { removeAt(i); } } //searches for a value in the list and returns the first index of that //value when found public int search(T find) { if(head == null) { return -10; } MyNode&lt;T&gt; p = head; do { if(find.compareTo(p.data) == 0) { return 0; } else { return -1; } p = p.next; }while(p != null); } public void itemChosen(int choice, LLMenu[] menu) { LLMenu m = menu[choice-1]; switch(m) { case ADD: System.out.println("What value would you like to add?"); T addThis = scan.nextInt(); add(addThis); break; case INSERT: System.out.println("What index would you like to replace?"); T replace = scan.nextInt(); System.out.println("What number would you like to insert?"); int val = scan.nextInt(); insert(val, replace); break; case SIZE: size(); break; case GET: System.out.println("What index would you like to look at?"); int thisOne = scan.nextInt(); get(thisOne); break; case REMOVE: remove(); break; case REMOVEAT: System.out.println("What index would you like to remove?"); T thisHere = scan.nextInt(); removeAt(thisHere); break; case REMOVELAST: removeLast(); break; case TOSTRING: toString(); break; case CLEAR: clear(); break; case SEARCH: System.out.println("What value would you like to search for?"); T searchForMe = scan.nextInt(); search(searchForMe); break; } } } </code></pre> <p>and MyNode:</p> <pre><code>public class MyNode&lt;T&gt; { T data; MyNode&lt;T&gt; next; MyNode&lt;T&gt; previous; } </code></pre> <p>Where I'm really having problems is at the switch statement in LList where I am scanning in items that are supposed to be set to Generics and obviously there is no method using Scanners to read in Generics. So question1, how do I read in and set these to Generics, and question2, in my clear method in LList how do I send variable i when using removeAt when it is expecting a Generic? Please keep all answers relevant and thanks for your time!</p> <p><em>Edit</em> One more question in my search method inside the do-while there is an if statement that says if(find.compareTo(p.data) == 0) how can I change this so that it works? I wasn't really sure of what to put there so I kind of just wrote my thought down.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload