Note that there are some explanatory texts on larger screens.

plurals
  1. POJava linked list set method
    primarykey
    data
    text
    <p>I have to convert my ArrayList code to Linked list without using import.java.util.LinkedList.</p> <p>My previous code has 4 ArrayList (name, price, quantity, priority)</p> <p>I have to sort arraylist depends on ArrayList prioirty.</p> <p>So I did like this.</p> <pre><code>public void sortPriority() { for (int i=0; i&lt;name.length(); i++) { for (int j=0; j&lt;priority.size()-i-1; j++) { if (priority.get(j).compareTo(priority.get(j+1)) &gt; 0) { int temp1 = priority.get(j); priority.set(j,priority.get(j+1)); priority.set(j+1, temp1); String temp2 = name.get(j); name.set(j,name.get(j+1)); name.set(j+1, temp2); double temp3 = price.get(j); price.set(j,price.get(j+1)); price.set(j+1, temp3); int temp4 = quantity.get(j); quantity.set(j,quantity.get(j+1)); quantity.set(j+1, temp4); } } } } </code></pre> <p>I changed my name ArrayList to Linked List.</p> <p>Since I cannot import java.util.LinkedList, I put my own code in other class called StringLinkedList.</p> <pre><code>abstract class StringLinkedList implements LinkList { private ListNode head; public StringLinkedList() { head = null; } public void showList() { ListNode position = head; while (position != null) { System.out.println(position.getData()); position = position.getLink(); } } public int length() { int count = 0; ListNode position = head; while (position != null) { count++; position = position.getLink(); } return count; } public void addNodeToStart(String addData) { head = new ListNode(addData, head); } public void deleteHeadNode() { if (head != null) { head = head.getLink(); } else { System.out.println("Deleting from an empty list."); System.exit(0); } } public boolean onList(String target) { return find(target) != null; } private ListNode find(String target) { boolean found = false; ListNode position = head; while ((position != null) &amp;&amp; !found) { String dataAtPosition = position.getData(); if (dataAtPosition.equals(target)) { found = true; } else { position = position.getLink(); } } return position; } public String get(int i) { assert (i&gt;=0); ListNode current = this.head; while (i &gt; 0) { i--; current = current.link; if (current == null) { return null; } } return current.data; } public String set (int index, String element) { ListNode current = this.head; ListNode e = current; String oldVal = e.element; e.element = element; return oldVal; } } </code></pre> <p>Every other methods works fine besides my set().</p> <p>What should I change?</p>
    singulars
    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.
    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