将文件拆分为两个LinkedLists,以确定第二个List中是否有第一个List中的数字



我应该取一个单独行中有整数的文件,并将其扫描到列表中。然后我要把这个表分解成两个链表。最后我需要说明第二个列表中的数字是否在第一个列表中

我理解(至少我认为我理解)第二部分。我觉得我知道如何解决整个问题,因为我找不到逻辑上的缺陷,但命令似乎不起作用。

import java.util.*;
import java.io.*;
public class FindKeys {
private static LinkedList<Integer> foo;
private static LinkedList<Integer> list1;
private static LinkedList<Integer> list2;
private static TreeMap<Integer, Boolean> list3;
private static int firstValue;
private static int numberA;
private static int numberB;
public FindKeys() {
}
public void main(String [] args) {
    foo = new LinkedList<Integer>();
    read(foo);
    listA(foo);
    listB(foo);
    discover(list1);
    print(list3);
}
private void read(LinkedList<Integer> foo){
    Scanner scan;
    int number;
    try{
        scan = new Scanner( new FileReader("Stuff.txt"));
    }
    catch(FileNotFoundException e){
        System.err.println("FileNotFoundException: " + e.getMessage());
        return;
    }
    catch(ArrayIndexOutOfBoundsException f){
        System.err.println("ArrayIndexOutOfBoundsException: " + f.getMessage());
        return;
    }
    while(scan.hasNextInt()){
        number = scan.nextInt();
        foo.add(number);
    }
}
private void listA(LinkedList<Integer> foo){

    firstValue = foo.getFirst();
    for( int i = 1; i<firstValue; i++){
        System.out.println(foo.get(i));
        int temp = foo.get(i);
  //This is where the issue is.
        list1.set(i,temp);
    }
}
private void listB(LinkedList<Integer> foo){

    firstValue = foo.getFirst();
    for( int i = firstValue; i<foo.size(); i++){
        numberB = foo.get(i);
        list2.add(numberB);
    }
}
private void discover(LinkedList<Integer> list1){
    for(int i=0; i<list1.size(); i++){           
        if(list2.contains(list1.get(i))){
            boolean a = true;
            list3.put(list1.get(i),a);
            return;
        }
        boolean b = false;
        list3.put(list1.get(i),b);
    }
}
private void print(TreeMap<Integer, Boolean> list3){
    for(int numberA : list1){
        System.out.printf("%3i   %b" , numberA, list3);
    }
}
}

当我试图将第一个列表中的数字添加到第二个列表中时,问题就出现了。问题似乎是LinkedList list1的大小为0。我以为这是我的一个简单的误解,我不知道如何改变列表的大小,或者如何在每次添加一个新元素时再添加一个,但我在谷歌的前20页和这个网站上都找不到这些。话虽如此,我也要假设这里有人与搜索之神有这种神奇的联系,可能会给我指出正确的方向,或者只是告诉我为什么我不能添加到列表中。它只是告诉我有一个问题,因为:

java.lang.NullPointerException
at FindKeys.listA(FindKeys.java:58)
at FindKeys.main(FindKeys.java:21)
java.lang.NullPointerException
at FindKeys.listA(FindKeys.java:58)
at FindKeys.main(FindKeys.java:21)

老实说,我不知道这是什么意思,当我查它的时候,我更困惑了

初始化数据结构

private static LinkedList<Integer> list1 = new LinkedList<Integer>();
private static LinkedList<Integer> list2 = new LinkedList<Integer>();
private static TreeMap<Integer, Boolean> list3 = new TreeMap<Integer, Boolean>();

main方法

public static void main(String [] args) {
    FindKeys fk = new FindKeys();
    foo = new LinkedList<Integer>();
    fk.read(foo);
    fk.listA(foo);
    fk.listB(foo);
    fk.discover(list1);
    fk.print(list3);
}

相关内容

最新更新