java - How to add to an arraylist of linkedlists? -


I'm sorry if this is a stupid question but I'm new to the Java LinkList and Array list.

What I want to do is this: I have a text file that I run through the word for the word. I want to create an array list of linklists, which are written by the words written in the written links, each Uniqi word is written by those words, which are followed in the text.

Consider this part of the text: Cat Red Tree

I want the linked list of linked list to be like this:

- Cat - red < / p>

|

Cat - moving

| -

|

Red - tree

What I have now is:

  while (datafile.hasNext ()) {secondWord = dataFile.next () ; NWords ++; If (nWords% 1000 == 0) System.out.println (nWords + "word"); // and put the words in the list, if not already, check // if the word is already in the list (if.contains (firstWord)) {// it's linked list (( Linked List) (first word) Add the next word .add (secondWord); } else {// Create a new link list for this word and then add the next word. add (new linked list & lt; e & gt; (). add (firstWord)); (LinkedList) (firstWord)) add (secondWord) } // go to the first word word = second word; }   

And this gives me lots of errors. How can I do this best way? (With Linkedists, I know that hashtables and binary trees are better, but I have to use linked lists)

For the purpose of your external list, the array list is not the best data structure, and the least part of your difficulty arises from the misuse of the list of lists.

In your implementation, an ArrayList of Linkedist has made such a declaration as follows:

  ArrayList & lt; Linked list & lt; String & gt; & Gt; = New Arrestist & lt; & Gt; ();   

The result of following.contains will never be true, because is in the type of linked list, not the string . First Ward is a string, and this will not be an element of , but will be the first element of an ArrayList which is an element of .

The solution below uses a map , or more specifically hashmap for the external list > Type is A map is better because while searching the first word, the revised lookup time will be O (1) using the O (n) for a (o) (1) list.

  string first word = datafile .next () toLowerCase () .; Maps & lt; String, list & lt; String & gt; & Gt; Following = new hashmap & lt; & Gt; (); Int nWords = 0; Whereas (datafile.hasNext ()) {string secondvord = datafile.Next (). ToLowerCase (); nWords ++; If (nWords% 1000 == 0) {System.out.println (nWords + "word"); } // and put the words in the list, if not already, // check that this word is already in the list if (following.containsKey (firstWord)) {// it is for the linked list Add next word list list = follows.get (first word); If (! List.contains (secondary word)) {list.add (secondWord); }} else {// Create a new link list for this word and then the next word list list = new linked list & lt; String & gt; (); list.add (secondWord); Follows.put (first ward, list); } // go to the first word word = secondword; }   

The map will look like this:

  the: [cat, red] cat: [walks] to: [the] red: runs [tree] I have also made the following changes in your implementation:  
  • Do not include duplicates in the following list. Note that for this task there will be a set more appropriate data structure, but you clearly tell it clearly that using LinkedList

  • Use String.toLowerCase () to move all the string to lower case, so that the equivalent of "D" and "D" Can be treated. (Make sure you apply it to the initial value of first word , which does not appear in the code you provided.)

    Note Both solutions and your basic effort assume that the punctuation has already been removed.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -