Java LinkedList在索引处插入对象

Java LinkedList在索引处插入对象,java,methods,linked-list,inner-classes,singly-linked-list,Java,Methods,Linked List,Inner Classes,Singly Linked List,我在理解如何在链接中放置对象时遇到了一些困难。 在这种情况下,如果在特定索引处已经有一个对象,它将不会替换它(即对于另一个方法)。我想我很难理解如何获取特定索引,从该索引检索数据,然后将数据放在那里并连接节点,或者告诉用户那里已经有一个对象 这是我的密码: public class CourseList { private Coursenode head; int currentSize; public void insertAtIndex(Course c, int

我在理解如何在链接中放置对象时遇到了一些困难。 在这种情况下,如果在特定索引处已经有一个对象,它将不会替换它(即对于另一个方法)。我想我很难理解如何获取特定索引,从该索引检索数据,然后将数据放在那里并连接节点,或者告诉用户那里已经有一个对象

这是我的密码:

public class CourseList {

    private Coursenode head;
    int currentSize;

    public void insertAtIndex(Course c, int index) {

        Coursenode insert =new Coursenode(c,head); 
        Coursenode temp = new Coursenode();

        if (index > currentSize - 1 || index < 0) {
            throw (new IndexOutOfBoundsException());
        }


        for(int x = 0; x < index; x++) {
            if (insert.getNext()!= null) {
                temp = insert;
                insert.setNext(insert);
                insert.setData(temp.getData());
            }
            if (insert.getNext() == null && x == index) {
                insert.setNext(insert.getNext());
            }
            if (insert.getNext() != null && x == index) {
                System.out.println("There is already a Course at that Index");
            }

        }
    }
}

任何想法都值得赞赏,我怀疑我在节点之间的head引用中迷失了方向,但我不太明白如何解决这个问题。

首先,在linkedlist中,如果

索引 然后,linkedlist中已经存在一个对象。如果node.next中有空节点,则说明您如何知道已到达链接列表的末尾

public class CourseList {

    private Coursenode head;
    int currentSize;

    public void insertAtIndex(Course c, int index) {

        Coursenode insert =new Coursenode(c,head); 
        Coursenode temp = new Coursenode();

        if (index > currentSize - 1 || index < 0) {
            throw (new IndexOutOfBoundsException());
        }

        //tempnode = head;
        for(int x=1; x< index;x++) {
         //tempnode = tempnode.next;
        }
        //nodeatindex = tempnode;
        //you can get details of the node
} 
公共类课程列表{
私人Coursenode负责人;
int-currentSize;
公共void insertAtIndex(课程c,整数索引){
Coursenode insert=新Coursenode(c,头部);
Coursenode温度=新Coursenode();
如果(索引>当前大小-1 | |索引<0){
抛出(新IndexOutOfBoundsException());
}
//tempnode=头部;
对于(int x=1;x
希望这有帮助!

有三种方法(假设索引值为正值)在链接列表的索引处插入:

  • 在头部(
    索引==0
  • 尾部之后(
    索引>=currentSize
  • 中间(在占用索引处)(
    index>0&&index
  • 也许有一种倾向认为插入尾部是另一种情况,但稍后我们会看到尾部的插入与中间的插入一样,因为尾部会向前滑动。

    如果插入在头部,则需要将插入节点的
    next
    设置为旧
    head
    ,然后将
    head
    设置为插入节点:

    private void insertAtHead(Course course) {
        Coursenode insertedNode = new Coursenode(c, head);
        head = insertedNode;
    }
    
    如果插入发生在尾部之后,通常的处理方法是抛出某种异常,例如:

    如果插入发生在占用索引处,则现有节点(以及现有节点之后的所有节点)必须向前推。这意味着插入节点的
    next
    必须设置为当前占用索引的节点,并且当前索引节点之前的节点的
    next
    必须设置为插入的节点。本质上,插入的节点融合到列表中。为此,必须遍历列表在找到占用的节点之前:

    private void insertAtOccupied(Course course, int index) {
        Coursenode previous = null;
        Coursenode current = head;
    
        for (int i = 1; i <= index; i++) {
            // Track the previous and current nodes
            //   previous = node at i - 1
            //   current = node at i
            previous = current;
            current = current.next;
        }
    
        Coursenode insertedNode = new Coursenode(c, current.next);
        previous.next = insertedNode;
    }
    
    private void insertAtOccupied(课程,整数索引){
    Coursenode previous=null;
    Coursenode电流=水头;
    用于(int i=1;i=currentSize){
    抛出新IndexOutOfBoundsException(“无法在课程列表尾部插入课程”);
    }
    else if(索引>0&&index
    我很确定在
    insertAtOccupied
    中调用
    Coursenode
    应该是
    current
    ,而不是
    current。下一步
    ,否则操作是替换,而不是插入。
    throw new IndexOutOfBoundsException("Cannot insert course after the tail of the course list");
    
    private void insertAtOccupied(Course course, int index) {
        Coursenode previous = null;
        Coursenode current = head;
    
        for (int i = 1; i <= index; i++) {
            // Track the previous and current nodes
            //   previous = node at i - 1
            //   current = node at i
            previous = current;
            current = current.next;
        }
    
        Coursenode insertedNode = new Coursenode(c, current.next);
        previous.next = insertedNode;
    }
    
    public void insertAt(Course course, int index) {
    
        if (index == 0) {
            insertAtHead(course);
        }
        else if (index >= currentSize) {
            throw new IndexOutOfBoundsException("Cannot insert course after the tail of the course list");
        }
        else if (index > 0 && index < currentSize) {
            insertAtOccupied(course, index);
        }
    }