1

I've been asked to do something weird and I need to make a class that is a word set (for a spell checker) and I have to do it using a linked list.

What I've tried for the constructor is this:

public WordSet(LinkedList<String> list) {
        
    LinkedList<String> wordSet = list;
        
    }

But this doesn't let me reference the wordset in the rest of the class. BTW this class doesn't have a main or anything like that its essentially just a data structure which wraps around a linked list (no I have no idea why they want me to do it).

Can someone tell me what I'm doing wrong here?

As an example of a method in this class, one is:

public void insertWord(String s){
}

where I have to add a word to the wordset, now I know that linked lists have this functionality already in them but I don't know how to reference a linked list from a constructor because of course the linked list hasn't been instantiated, and can't be because this has no Main() method and I can't just go referencing it from the Class that does have a main method because that's messy.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
4Ves
  • 53
  • 8
  • Please provide a [mre] which demonstrates what you describe. – Yunnosch Feb 16 '21 at 10:47
  • 2
    The `wordSet` must be defined as the class field. Then you assign the passed parameter through the constructor to the instance field using `this.wordSet = list`. – Nikolas Charalambidis Feb 16 '21 at 10:47
  • That's it thanks. I knew I was being really stupid there but I'm just coming back to OOP after cooking my brain with Ocaml. – 4Ves Feb 16 '21 at 10:49
  • Does it really _have_ to be a LinkedList? It's almost always better to use ArrayList. See https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist-in-java – k314159 Feb 16 '21 at 10:58
  • It really does have to be a linked list. I have literally no idea why either. – 4Ves Feb 16 '21 at 11:01

2 Answers2

5

Create a LinkedinList as a class atribute then try to initialitate it to the constructor so u can after use it when u create an object of the current class

 public class WordSet {




 private LinkedList<String> list;

    public WordSet() {
        list = new LinkedList<>();
    }

    public void insertWord(String s){
        list.add(s);
    }
Dren
  • 1,239
  • 1
  • 8
  • 17
1

What you can do is something like this. First create a class that will have reference variable of your list and then a method for inserting new words. When creating a new object, we want user to "provide" a list on which he/she will work later. Meaning each user will have different list - which is why our constructor has argument of type List.

public class Main {

    List<String> words;

    public Main(List<String> words) {
        this.words = words;
    }

    public void insertWord(String s){
        words.add(s);
    }

}

You then create your own list and put that same list inside constructor. Once you have constructed an object, you can insert new words inside your list.

class Test{

    public static void main(String[] args) {
        List<String> myWords = new LinkedList<>();
        myWords.add("table");
        myWords.add("window");
        myWords.add("car");

        Main obj = new Main(myWords);
        obj.insertWord("carpet");

        //shows all your words
        System.out.println(myWords);
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Stefan
  • 969
  • 6
  • 9