簡體   English   中英

如何制作使用方法的鏈表列表

[英]How do I make an array of Linked Lists that uses methods

我必須編寫一個哈希表,該哈希表使用數組索引中的鏈接來將多個值存儲在同一位置而不是線性探測。 但是,在此測試中,我的鏈接列表數組似乎實例中充滿了空值,但是當我嘗試調用鏈接列表方法時,我得到了NullPointerException 我和我的教授似乎都找不到原因。

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

import Hash.*;
import LinkedList.*;

public class Main {

    public static void main(String[] args) {
LList<Integer>[] testArray = (LList<Integer>[]) new LList<?>[5];
        for(int i=0;i<5;i++)
            System.out.println(testArray[i]);

        System.out.println(testArray[0]);
        System.out.println(testArray[0].size());
        testArray[0].add(40);
        }
    }

然后將鏈表類封裝為LinkedList;

public class LList<T> implements I_LList<T> {
    protected int numElements;
    protected boolean found;

    protected LLNode<T> current;
    protected LLNode<T> previous;
    protected LLNode<T> list;

    public LList(){
        list = null;
        current = null;
        numElements = 0;
    }
    public void add(T element){
        System.out.println("LList add()");
        LLNode<T> newNode = new LLNode<T>(element);
        newNode.setLink(list);
        list = newNode;
        numElements++;
    }

您實例化LList對象的數組,但不實例化每個LList對象。

LList<Integer>[] testArray = (LList<Integer>[]) new LList<?>[5];
for(int i=0;i<5;i++)
{
    testArray[i] = new LList<Integer>(); // add this line
    System.out.println(testArray[i]);
}

您的NullPointerException來自System.out.println(testArray[0].size()); 當您嘗試調用null對象的size()時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM