Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何添加到LinkedList的arraylist?_Java_Arraylist_Linked List_Add - Fatal编程技术网

Java 如何添加到LinkedList的arraylist?

Java 如何添加到LinkedList的arraylist?,java,arraylist,linked-list,add,Java,Arraylist,Linked List,Add,如果这是一个愚蠢的问题,我很抱歉,但我对Java LinkedList和ArrayList是新手 我想做的是: 我有一个文本文件,我逐字逐句地浏览。我想创建一个LinkedList的Arraylist,其中文本中的每个uniqye单词在链接列表中后跟它在文本中后跟的单词 考虑这段文字:猫走向红树 我希望LinkedList的Arraylist如下所示: ArrayList<LinkedList<String>> follows = new ArrayList<>

如果这是一个愚蠢的问题,我很抱歉,但我对Java LinkedList和ArrayList是新手

我想做的是: 我有一个文本文件,我逐字逐句地浏览。我想创建一个LinkedList的Arraylist,其中文本中的每个uniqye单词在链接列表中后跟它在文本中后跟的单词

考虑这段文字:猫走向红树

我希望LinkedList的Arraylist如下所示:

ArrayList<LinkedList<String>> follows = new ArrayList<>();
the: [cat, red]
cat: [walks]
to: [the]
red: [tree]
walks: [to]
猫红

|

猫步

|

|

红树

我现在得到的是:

while(dataFile.hasNext()){
     secondWord = dataFile.next();
     nWords++;
     if(nWords % 1000 ==0) System.out.println(nWords+" words");


   //and put words into list if not already there
   //check if this word is already in the list
   if(follows.contains(firstWord)){
    //add the next word to it's linked list 
 ((LinkedList)(firstWord)).add(secondWord);
   }
   else{
      //create new linked list for this word and then add next word

      follows.add(new LinkedList<E>().add(firstWord));


  ((LinkedList)(firstWord)).add(secondWord);
   }
   //go on to next word                     
   firstWord = secondWord;
   }
这给了我很多错误。
我怎样才能做到最好?有了LinkedList,我知道哈希表和二叉树更好,但我需要使用链表

您不应该使用直接类实现,而应该使用它们的接口来简化开发。因此,在初始化变量时,应该将变量声明为List并定义类,而不是时不时地进行类型转换。由于您尚未发布相关代码来重新定义它,我可以给您举一个例子:

List<List<String>> listOfListOfString = new LinkedList<>(); //assuming Java 7 or later used
List<String> listOne = new ArrayList<>();
listOne.add("hello");
listOne.add("world");
listOfListOfString.add(listOne);
List<String> listTwo = new ArrayList<>();
listTwo.add("bye);
listTwo.add("world");
listOfListOfString.add(listTwo);
for (List<String> list : listOfListOfString) {
    System.out.println(list);
}
请注意,现在您可以将listOne或listTwo中任何一个的实现更改为LinkedList:

代码的行为也一样。无需进行任何类型转换即可使其正常工作

相关的:

对于外部列表而言,ArrayList并不是最好的数据结构,至少部分困难源于列表的不正确使用

在您的实现中,下面大概是一个LinkedList的ArrayList,声明如下:

ArrayList<LinkedList<String>> follows = new ArrayList<>();
the: [cat, red]
cat: [walks]
to: [the]
red: [tree]
walks: [to]
地图将如下所示:

ArrayList<LinkedList<String>> follows = new ArrayList<>();
the: [cat, red]
cat: [walks]
to: [the]
red: [tree]
walks: [to]
我还对您的实现进行了以下更改:

不要在下面的单词列表中添加重复的单词。请注意,对于此任务,集合将是更合适的数据结构,但您清楚地指出,需要使用LinkedList

使用String.toLowerCase将所有字符串移动到小写,以便对和进行等效处理。确保您也将其应用于firstWord的初始值,该值不会出现在您提供的代码中


请注意,此解决方案和您最初的尝试都假定标点符号已被删除。

忘记使用LinkedList或ArrayList,因此也忘记此类型转换。将变量声明为列表接口,并在初始化时决定正确的类实现。那么我该如何做呢?我很困惑。但是如果我在示例代码中的while循环中这样做,并且我希望列表listOne、listwo等具有动态名称,那么我可以通过将它们命名为与字符串firstWord相同的名称来找到它们?@user3439407 while循环不会影响问题的行为,只是提供代码作为示例。如果你想通过他们的名字找到他们,你应该使用一个映射,所以你使用了错误的方法…这不是一个hashmap吗?我必须使用链表@User3439407Map是接口,有几个实现:HashMap、LinkedHashMap、TreeMap、ConcurrentHashMap。。。看起来你还是不知道如何编程到一个接口。