简体   繁体   English

并行阵列和文件读取

[英]Parallel Arrays and reading from a file

I need help making a parallel array I need to read in from a textfile of strings and create a array of strings that adds each name once and increments repeated strings in an array of ints..... any ideas of how i can fix this code to do that? 我需要帮助制作一个并行数组,我需要从字符串的文本文件中读取并创建一个字符串数组,该字符串数组将每个名称添加一次,并在整数数组中增加重复的字符串.....关于如何解决此问题的任何想法代码来做到这一点?

    Scanner sc=new Scanner(new File("Cat.txt"));
    String category=sc.nextLine();
    int total=sc.nextInt();
    int[]totcat=new int[total];
    String[]names=new String[total];
    while(sc.hasNextLine())
    {
        String x=sc.nextLine();
        boolean b=false;
        int size=0;
        for(int i=0;i<names.length;i++)
        {
            if(x.equals(names[i]))
            {
                b=true;
                totcat[i]++;
            }
        }
        if(!b)
        {
            names[size]=x;
            totcat[p]++;
            size;
        }
    }

The problem is the line if(names[j].equals(null)) . 问题是if(names[j].equals(null)) This will never evaluate to true , since for that to happen, names[j] would have to be null , and so it would instead throw a NullPointerException . 这永远不会评估为true ,因为要做到这一点, names[j]必须为null ,因此它将抛出NullPointerException The correct way to write it would be if(names[j] == null) . 正确的写法是if(names[j] == null)

A more elegant way would be to have another variable to keep track of how many strings you have in your array, so that if you don't find a repeated string (your if(!b) block), you can just add the string at the index indicated by the size variable, instead of having to go through the whole array looking for a null space. 一种更优雅的方法是使用另一个变量来跟踪数组中有多少个字符串,这样,如果找不到重复的字符串(您的if(!b)块),则只需添加字符串即可在由size变量指示的索引处,而不需要遍历整个数组以寻找null空间。

It sounds like you need to read a file with Strings that are delimited by line breaks, and you want to end up with a set of all Strings found in the file as well as a count of the number of times each String occurs in the file. 听起来您需要读取带有以换行符分隔的字符串的文件,并且最终要获得文件中找到的所有字符串的集合以及文件中每个字符串出现的次数的计数。

Rather than trying to build an array-based structure, I'd recommend using a Map. 建议不要使用基于数组的结构,而建议使用Map。 As you read each line in the source file you check the map to see if the String value already exists in the map. 在阅读源文件中的每一行时,您会检查映射以查看字符串值是否已存在于映射中。 If so, you increment it's value (an int), if not, you add the String to the Map. 如果是这样,则增加其值(一个int值),否则,将String添加到Map中。

Something like this: 像这样:

Map<String, Integer> map = new HashMap<>();

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
  String line;
  while ((line = reader.readLine()) != null) {
    if(map.containsKey(line)) {
      Integer lineCount = map.get(line);
      map.put(line, lineCount++);
    } else {
      map.put(line, 1);
    }
  }
} catch(IOException ioe) {
  // Handle exception
}

Something to watch our for here is memory limitations. 这里需要注意的是内存限制。 If the file you are reading is very large and/or your Java stack size is small, you may encounter OutOfMemoryExceptions. 如果您正在读取的文件很大和/或Java堆栈大小很小,则可能会遇到OutOfMemoryExceptions。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM