Java获取方法移动到前面而不是后退

时间:2012-11-27 03:45:10

标签: java

我有一个add方法,它将最新的文本字符串插入到列表的末尾,我希望它作为第一个文本字符串插入。任何帮助表示赞赏。这是方法:

 public void add (Magazine mag)
  {

  MagazineNode node = new MagazineNode (mag);
  MagazineNode current;

  if (list == null)
     list = node;
  else
  {
     current = list;
     while (current.next != null)
        current = current.next;
     current.next = node;
  }
   }

2 个答案:

答案 0 :(得分:4)

假设我理解你想要的,可能会关闭,我认为你想要以下方法。

public void add_first (Magazine mag)
{
  MagazineNode node = new MagazineNode (mag);

  // make the new first node point to the current root
  node.next=list;

  // update the root to the new first node
  list=node;
}

答案 1 :(得分:0)

public void add (Magazine mag)
{
  MagazineNode node = new MagazineNode (mag);    
  node.next = list;
  list = node;
}