【问题标题】:duplicates from the ArrayListArrayList 中的重复项
【发布时间】:2014-05-23 17:00:10
【问题描述】:

“你能帮我解决以下这些问题吗...

  1. 请将文件中的所有单词拆分并存储到一个 ArrayList 中。打印 ArrayList 的内容。
  2. 使用上一题中创建的ArrayList,请创建一个只包含书中唯一单词的新ArrayList(去掉ArrayList中的所有重复项)。为简单起见,请忽略大小写差异(大写与小写)和标点符号。
  3. 请打印文件中使用的所有单词的列表 (christmas_carol.txt) 及其频率,即每个单词在文件中出现的次数。

谢谢,”

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;


public class Practice {


    public static void main (String[] args) {
        ArrayList<String> array = new ArrayList<String>();
        ArrayList<String> dup = new ArrayList<String>();

        File file = new File ("christmas_carol.txt");

        try {

        String line = FileUtils.readFileToString(file);
        String[] lines = line.split(" ");


        for (int i = 0 ; i < lines.length; i ++) {
            array.add(lines[i]);
            //System.out.println(lines[i]); //printing the contents of the file.
        }

        for (int i = 0 ; i < dup.size(); i ++){
            dup.add(line);
            if (dup.equals(lines)) {
                dup.remove(i);
                System.out.print(i);
            }
        }


        } catch (IOException ie) {
            System.err.print("No Such File Found!");
        }

    }
}

【问题讨论】:

  • 相信我不是!这只是一种古老的做法......
  • 如果是为了“练习”,你不应该有什么东西可以展示吗?
  • 坦率地说,我们并不在乎它是否是家庭作业。我们以同样的方式判断问题
  • 你的意思是一个答案...当然我会分享
  • @Whizz 不,我们的意思是你已经尝试过。

标签: java arraylist


【解决方案1】:

答案在 cmets 中解释

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;


public class Practice {


    public static void main (String[] args) {

//      ArrayList<String> array = new ArrayList<String>();
//      ArrayList<String> dup = new ArrayList<String>();

    File file = new File ("christmas_carol.txt");
    try {

        String fileContent = FileUtils.readFileToString(file);
        String[] lines = fileContent.split(" ");

//          You need not iterate and add instead you can use Arrays.asList. But if its for an exercise this loop is fine. 
//          for (int i = 0 ; i < lines.length; i ++) {
//              array.add(lines[i]);
//          }
//          
//          But better to use for-each loop as
//          for(String line : lines){
//              array.add(line);
//          }

        List<String> array = Arrays.asList(lines);

//          The easiest way to avoid duplicate is to use java.util.Set
        Set<String> uniqueWords = new HashSet<String>(array);

//          But if you want to iterate this logic is wrong. Firstly the dup list is just initialized and empty. So the size is zero and it'll never enter the loop.
//          Secondly dup.equals(lines) is comparing an arraylist with array. That'll always be false
//          for (int i = 0 ; i < dup.size(); i ++){
//              dup.add(line);
//              if (dup.equals(lines)) {
//                  dup.remove(i);
//                  System.out.print(i);
//              }
//          }
//          
//          So what you need here is again a simple loop
        List<String> dup = new ArrayList<String>();
        for(String line : array){
            if(!dup.contains(line)){
                dup.add(line);
            }
        }

//          To find frequency of a word the easiest way is to use a map which keep the word and its count. So when we encounter a duplicate just update the count.
        Map<String, Integer> wordFrequency = new HashMap<String, Integer>();
        int count;
        for(String line : array){
            count = 0;
            if(wordFrequency.containsKey(line)){
                count = wordFrequency.get(line);
            }
            wordFrequency.put(line, ++count);
        }

    } catch (IOException ie) {
        System.err.print("No Such File Found!");
    }

    }
}

【讨论】:

  • 谢谢!除了使用Set还有什么办法吗?
  • 是的。使用循环。我在下面的评论中也提到了这种方式。“所以你在这里需要的是一个简单的循环”:)
猜你喜欢
  • 2018-05-29
  • 2021-05-29
  • 2017-07-01
  • 2012-01-26
  • 2014-04-08
  • 1970-01-01
  • 2011-01-26
  • 2023-03-04
  • 2012-08-25
相关资源
最近更新 更多