Java add方法应按排序顺序将元素插入列表中

Java add方法应按排序顺序将元素插入列表中,java,linked-list,Java,Linked List,我已经完成了将元素添加到LinkedList中的代码。现在我想将元素按排序顺序插入到列表中。我该怎么做 public void add(String element) { if (isEmpty()) { first = new Node(element);

我已经完成了将元素添加到
LinkedList
中的代码。现在我想将元素按排序顺序插入到列表中。我该怎么做

         public void add(String element)
                  {

                      if (isEmpty()) 
                      {
                          first = new Node(element);
                          last = first;
                      }
                      else
                      {
                          // Add to end of existing list
                          last.next = new Node(element);
                          last = last.next;
                      }          
                  }
我的主要类是针对Linkedlist和arraylist的,它调用了
SimpleLinkedList
class和
SimpleRayListClass

            package Comp10152_linkedlist;


            import java.util.Random;

            public class Comp10152_Lab4 
            {
               public static void main(String[] args)
              {
                final int NUMBER_OF_ITERATIONS = 10;
                String names[] = {"Amy", "Bob", "Al", "Beth", "Carol", "Zed", "Aaron"};
                SimpleLinkedList ll = new SimpleLinkedList();
                final int TOTALOPERATIONS = names.length * NUMBER_OF_ITERATIONS;

                Random random = new Random();

                for (int i=0; i<NUMBER_OF_ITERATIONS;i++)
                {
                  for (int j=0; j<names.length; j++)
                    ll.add(names[j]);
                }
                 System.out.println("The members of list are:");
                  System.out.println(ll);
                // remove half of the items in the list by selecting randomly from names
                for (int i=0; i<TOTALOPERATIONS/2;i++)
                {
                  ll.remove(names[random.nextInt(names.length)]);
                }
                System.out.println("The members of list are:");
                  System.out.println(ll);
                SimpleArrayList al = new SimpleArrayList();
                try
                {
                for (int i=0; i<NUMBER_OF_ITERATIONS;i++)
                {
                  for (int j=0;j<names.length;j++)
                     al.add(i,names[j]);
                }
                  System.out.println("The members of array are:");
                  System.out.println(al);

                // remove half of the items in the list by selecting randomly from names
                for (int i=0; i<TOTALOPERATIONS/2;i++)
                {
                  al.remove(names[random.nextInt(names.length)]);
                }   
                 System.out.println("The members of array are:");
                  System.out.println(al);
                }
                catch (Exception e)
                {
                    System.out.println(e);
                }
              }     
            }
package Comp10152\u链接列表;
导入java.util.Random;
公共类COM10152_Lab4
{
公共静态void main(字符串[]args)
{
迭代的最终整数=10;
字符串名[]={“Amy”、“Bob”、“Al”、“Beth”、“Carol”、“Zed”、“Aaron”};
SimpleLink列表ll=新的SimpleLink列表();
最终整数TOTALOPERATIONS=names.length*迭代次数;
随机=新随机();

对于(inti=0;i,首先将元素插入列表之外,然后通过调用add方法将元素插入到列表中。
如何对列表外的元素进行排序取决于所使用的数据结构、数据类型以及要应用的算法。

插入列表时,按排序顺序添加。 首先搜索排序列表中大于该元素的元素,然后搜索要插入的元素,然后在此之前添加新元素

类似于

//Considering ascending order
public void add(String element) {
    if(isEmpty) {
        first = new Node(element);
        last = first;
    } else {
        currentNode = first;
        while(currentNode.next != null && currentNode.next.element > element) {
            currentNode = currentNode.next;
        }

        Node newNode = new Node(element);
        newNode.next = currentNode.next;
        currentNode.next = newNode;
    }
}

Comp10152_linkedlist.SimpleLinkedList.add(SimpleLinkedList.java:108)处的线程“main”java.lang.NullPointerException和“currentNode.next.element”中的异常在这个示例中,您使用了哪个元素?这是一个粗略的代码。添加诸如currentNode!=null之类的条件。此外,currentNode.next.element意味着访问下一个连接节点的元素。