无法访问最后一个链表数组元素。

huangapple 未分类评论48阅读模式
英文:

Cannot access last linked list array element

问题

public class ListOfNVersion03PartA
{   
    private int thisNumber;              // the number stored in this node
    private ListOfNVersion03PartA next;  // forms a linked list of objects
    
    private final int nodeID;            // a unique ID for each object in the list
    
    private static int nodeCount = 0;    // the number of list objects that have been created
    
    /**
     * @param  num   the value to be stored in this object
     */
    public ListOfNVersion03PartA(int num)
    {
        thisNumber = num;
        next = null;

        ++nodeCount;
        nodeID = nodeCount;

    } // constructor(int num)

    /**
     * @param  num   the multiple values to be stored in the list, in that order
     */
    public ListOfNVersion03PartA(int [] num)
    {
        this(num[0]);  // in this context, "this" invokes the other constructor        

        for (int i=1 ; i<num.length ; ++i)
            insertLast(num[i]);

    } // constructor(int [] num)

    /**
     * @return     the number of elements stored in this list 
     */
    public int getListSize()
    {
        return nodeCount;

    } // method getListSize

    /**
     * @return     the last element in the list
     */
    public int getLast()
    {
        int y =  next[nodeCount-1];
        return y;

    } // method getLast

    /**
     * prints this object
     */
    public void printNode()
    {
        System.out.print("[" + nodeID + "," + thisNumber + "]->");

    } // method printListNode

    /**
     * prints the tail of a list
     */
    private void printListTail()
    {
        printNode();

        if ( next != null )
            next.printListTail();

    } // method printListTail

    /**
     * prints the contents of the list, in order from first to last
     */
    public void printList()
    {
        printNode();

        if ( next != null )
            next.printListTail();

    } // method printList

    /**
     * This method is NOT examinable in this test.
     * 
     * prints the contents of the list, in order from first to last, and
     * then moves the cursor to the next line
     */
    public void printlnList()
    {
        printList();
        System.out.println();

    } // method printlnList

    /**
     * @return     the number of times the element occurs in the list
     * 
     * @param  element   the element to be counted
     */
    public int countElement(int element)
    {
        return 999;

    } // method countElement 

    /**
     * @return     the number of times the replacement was made
     * 
     * @param  replaceThis   the element to be replaced
     * @param  withThis      the replacement
     */
    public int replaceAll(int replaceThis, int withThis)
    {
        return 999;

    } // method replaceAll

    /**
     * @return a reference to the first object in the list that contains the parameter value, or null if it is not found
     * 
     * @param  findThis   the value to be found
     */
    public ListOfNVersion03PartA findUnSorted(int findThis)
    {  
        // This algorithm is known as "linear search"

        if ( thisNumber == findThis )
            return this;

        if ( next != null )
            return next.findUnSorted(findThis);

        return null;

    } // method findUnSorted 

    /**
     * @return     the reference to the object containing the smallest element in the list
     */
    public ListOfNVersion03PartA minRef()
    {
        // add and/or modify code to complete the method 
        ListOfNVersion03PartA minOfTail;

        if ( next == null )
            return this;

        minOfTail = next.minRef();

        if ( thisNumber <= minOfTail.thisNumber )
            return this; 
        else
            return minOfTail;

    } // method minRef

    /**
     * Inserts an element in the last position. The pre-existing elements in the
     * list are unaffected.
     * 
     * @param  newElement   the element to be inserted
     */
    public void insertLast(int newElement)
    { 
        if ( next == null )
            next = new ListOfNVersion03PartA(newElement);
        else
            next.insertLast(newElement);

    } // method insertLast

} // class ListOfNVersion03PartA
英文:

I new in Java and trying to run some tests. I cannot use the .getLast() function as its showing error and also cannot seem to create the method. What am I doing wrong.
Here is my partial code. I am trying to create the getLast method which is failing.

Here is the error The type of the expression must be an array type but it resolved to ListOfNVersion03PartA.

 */
public class ListOfNVersion03PartA
{   
    private int thisNumber;              // the number stored in this node
    private ListOfNVersion03PartA next;  // forms a linked list of objects

    private final int nodeID;            // a unique ID for each object in the list

    private static int nodeCount = 0;    // the number of list objects that have been created

    /**
     * @param  num   the value to be stored in this object
     */
    public ListOfNVersion03PartA(int num)
    {
        thisNumber = num;
        next = null;

        ++nodeCount;
        nodeID = nodeCount;

    } // constructor(int num)

    /**
     * @param  num   the multiple values to be stored in the list, in that order
     */
    public ListOfNVersion03PartA(int [] num)
    {
        this(num[0]);  // in this context, &quot;this&quot; invokes the other constructor        

        for (int i=1 ; i&lt;num.length ; ++i)
            insertLast(num[i]);

    } // constructor(int [] num)

    /**
     * @return     the number of elements stored in this list 
     */
    public int getListSize()
    {
        return nodeCount;

    } // method getListSize

    /**
     * @return     the last element in the list
     */
    public int getLast()
    {
    	int y =  next[nodeCount-1];
        return y;

    } // method getLast

    /**
     * prints this object
     */
    public void printNode()
    {
        System.out.print(&quot;[&quot; + nodeID + &quot;,&quot; + thisNumber + &quot;]-&gt;&quot;);

    } // method printListNode

    /**
     * prints the tail of a list
     */
    private void printListTail()
    {
        printNode();

        if ( next != null )
            next.printListTail();

    } // method printListTail

    /**
     * prints the contents of the list, in order from first to last
     */
    public void printList()
    {
        printNode();

        if ( next != null )
            next.printListTail();

    } // method printList

    /**
     * This method is NOT examinable in this test.
     * 
     * prints the contents of the list, in order from first to last, and
     * then moves the cursor to the next line
     */
    public void printlnList()
    {
        printList();
        System.out.println();

    } // method printlnList

    /**
     * @return     the number of times the element occurs in the list
     * 
     * @param  element   the element to be counted
     */
    public int countElement(int element)
    {
        return 999;

    } // method countElement 

    /**
     * @return     the number of times the replacement was made
     * 
     * @param  replaceThis   the element to be replaced
     * @param  withThis      the replacement
     */
    public int replaceAll(int replaceThis, int withThis)
    {
        return 999;

    } // method replaceAll

    /**
     * @return a reference to the first object in the list that contains the parameter value, or null if it is not found
     * 
     * @param  findThis   the value to be found
     */
    public ListOfNVersion03PartA findUnSorted(int findThis)
    {  
        // This algorithm is known as &quot;linear search&quot;

        if ( thisNumber == findThis )
            return this;

        if ( next != null )
            return next.findUnSorted(findThis);

        return null;

    } // method findUnSorted 

    /**
     * @return     the reference to the object containing the smallest element in the list
     */
    public ListOfNVersion03PartA minRef()
    {
        // add and/or modify code to complete the method 
        ListOfNVersion03PartA minOfTail;

        if ( next == null )
            return this;

        minOfTail = next.minRef();

        if ( thisNumber &lt;= minOfTail.thisNumber )
            return this; 
        else
            return minOfTail;

    } // method minRef

    /**
     * Inserts an element in the last position. The pre-existing elements in the
     * list are unaffected.
     * 
     * @param  newElement   the element to be inserted
     */
    public void insertLast(int newElement)
    { 
        if ( next == null )
            next = new ListOfNVersion03PartA(newElement);
        else
            next.insertLast(newElement);

    } // method insertLast

   

} // class ListOfNVersion03PartA

huangapple
  • 本文由 发表于 2020年5月29日 05:14:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/62074630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定