有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java更改节点的值

我目前有LinkedStack,其中有三个节点的值。这些值是使用我在下面提到的类中创建的push方法创建的。我的问题是在我按下“1,2,3”键之后;我有一个包含3个元素的列表,在我的作业中,这些元素需要使用while(current!=null)等条件从顶部节点到底部依次更改。但我的问题是,我如何在不使用Arraylist库或此类方法的情况下更改元素值,这些方法仅用于在主节点上使用setMethods来简化此类命令例如,这就是我所做的,但显然它不起作用,因为它只是指它自己,没有改变任何东西。我所做的是通过“代码应该在哪里更改列表中已经推送的值”行。有人能解释一下我在那条线上做错了什么吗?我尝试在主类上使用list对象来调用LinearNode类方法,但它不允许,因为它期望给定的变量current等于该对象。 祝福你

线性节点类

public class LinearNode<T> 
{

    private LinearNode<T> next; //se guarda la direccion del Nodo
    private T element;          //Lista vacia

    public LinearNode()
{
    next = null;
    element = null;
    }
    //-----------------------------------------------------------------
    // Creates a node storing the specified element.
    //-----------------------------------------------------------------
    public LinearNode (T elem, LinearNode reference)
    {
    next = reference;
    element = elem;
    }
    //-----------------------------------------------------------------
    // Returns the node that follows this one.
    //-----------------------------------------------------------------
    public LinearNode<T> getNext()
    {
    return next;
    }
    //-----------------------------------------------------------------
    // Sets the node that follows this one.
    //-----------------------------------------------------------------
    public void setNext (LinearNode<T> node)
    {
    next = node;
    }
    //-----------------------------------------------------------------
    // Returns the element stored in this node.
    //-----------------------------------------------------------------
    public T getElement()//asigna valor
    {
    return element;
    }

    public void setElement(T elem)
    {

        element = elem;


    }



    }

LinkedStack类

public class LinkedStack<T> implements Stack<T> {


private int count;
private LinearNode<T> top; //referencia del Nodo ( direccion) 

//-----------------------------------------------------------------
// Creates an empty stack using the default capacity.
//-----------------------------------------------------------------
public LinkedStack()
{
count = 0;
top = null;

}
//-----------------------------------------------------------------
// Removes the element at the top of this stack and returns a
// reference to it. Throws an EmptyCollectionException if the
// stack contains no elements.
//-----------------------------------------------------------------

public LinearNode getLinearNode(){
    return top;
}

    @Override
    public boolean IsEmpty()

    {
        if(top == null)
        {
        System.out.println("Stack is empty");
        }
        return top == null;
    }



    @Override
    public void Push(T element)
{

     LinearNode<T> current = new LinearNode<>(element, top);
     current.setNext(top);top a la variable de referencia next
     top = current;
     count++;


}

    @Override 
    public T Pop() 
{


    T result;
    System.out.println("Lets pop the top element!");

   if(count == 0)
   {
    System.out.println("Stack is empty");   
   }


    result = top.getElement();
    top = top.getNext(); 
    count--;
    System.out.println("The element that we have poped is: " + "'" + result + "'" + "\n");

    return result;


}

    @Override
    public String toString()
{

String result = "";
LinearNode current = top;
System.out.print("<top of stack-->" + "\n");
while (current != null)
{

result += "[" + current.getElement() + "]" + "\n"; 
current = current.getNext();
}
return result  + "<--bottom of stack>" + "\n";

}

    @Override
    public T Peek() {
        System.out.println("Lets peek the top element!");
        if(count == 0)
        {
         System.out.println("Peek failed stack is empty");
        }
        System.out.println("The element that we have peeked is: " + "[" + top.getElement()+ "]" +"\n");
        return top.getElement();

    }

    @Override
    public int Size() {
        if(count != 0)
        {
        System.out.println("Let's check the size of the list!");
        System.out.println("The size of the list is: "+ "'" + count + "'" + "\n");
        }
        if(count == 0)
        {
          System.out.println("The size of the list is...Woah.");  
          System.out.println("The list size is now: " + "'" + count + "'"  + "\n" + "Push more elements!");
        }

        return count;
    }






    }

主课

public class LSmain {

    public static void main(String[]args)
    {
     LinkedStack<Integer> list = new LinkedStack<>();   
     System.out.println("Let's make a List!");
     System.out.println("Push 3 times.");
     System.out.println("Check the size.");
     System.out.println("Peek the top element.");
     System.out.println("Pop three times.");
     System.out.println("The size now should be zero!" + "\n");
     list.Push(1);
     list.Push(2);
     list.Push(3);
     list.Size();
     System.out.println(list.toString());
     System.out.println("Change values");




     #//#

其中,代码应该更改列表中已推送的值的值

     LinearNode<Integer>current;
     current = list.getLinearNode();

     while(current != null)
     {
     current.setNext(current);
     current.setElement(5);
     }
     #//#   

     System.out.println(list.toString());

共 (0) 个答案