有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java获取文件并拆分为两个LinkedList,以确定第二个列表中的数字是否在第一个列表中

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

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

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)

坦率地说,我不知道这意味着什么,当我查到它时,我更加困惑了


共 (1) 个答案

  1. # 1 楼答案

    初始化数据结构

    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);
    }