【问题标题】:How do I count multiple duplicate elements in an ArrayList?如何计算 ArrayList 中的多个重复元素?
【发布时间】:2016-08-30 14:57:56
【问题描述】:

我有一个程序可以接收乐队和专辑的文件列表。我需要确定每个乐队制作的专辑数量,然后按降序打印出乐队列表和他们制作的专辑数量。我环顾四周,看到它使用映射和集合完成。我想知道如何在没有任何一个的情况下做到这一点。这是我目前所拥有的:

public static void processFile(String filename)
{
    String bandname = "";
    String[][] data = read_spreadsheet(filename);
    //takes the file and converts it to a 2d array
    ArrayList<String> bands = new ArrayList<String>();
    for(int rows = 0; rows < data.length; rows++)
    {
        bands.add(data[rows][0]);
    }

    for(int i = 0; i<bands.size()-1;i++)
    {
        int albumcount = 0;
        for(int j = i+1; j<bands.size();j++)
        {
            if(bands.get(i).equals(bands.get(j)))
            {
                albumcount++;
            }
        }
    }
}

输入示例:

band1 -album
band2 -album
band1 -album
band3 -album
band1 -album
band2 -album

输出示例:

band1: 3
band2: 2
band3: 1

【问题讨论】:

    标签: java for-loop arraylist count duplicates


    【解决方案1】:

    如果您对乐队名称列表(有重复)进行排序,然后计算列表中每个乐队名称的数量,您将获得每个乐队的专辑数量:

    public static void processFile(String filename)
    {
        //takes the file and converts it to a 2d array
        String[][] data = read_spreadsheet(filename);
    
        // get all the bands (with duplicates)
        ArrayList<String> bands = new ArrayList<String>();
        for(int rows = 0; rows < data.length; rows++) {
            bands.add(data[rows][0]);
        }
    
        // sort the bands alphabetically
        Collections.sort(bands);
    
        int albumCount = 1;
        String currentBand = bands.remove(0);
        while(bands.size() > 0) {
            String nextBand = bands.remove(0);
            if(currentBand.equals(nextBand)) {
                albumCount++;
            } else {
                // print the current band album count and setup for the next band
                System.out.println(currentBand + ": " + albumCount);
                currentBand = nextBand;
                albumCount = 1;
            }
        };
        // print the final band album count
        System.out.println(currentBand + ": " + albumCount);
    }
    

    【讨论】:

    • 不使用 Collection 怎么能做到这样的事情?
    • 您可以手动对数组列表进行排序,但是当 Collections.sort() 可用时为什么要这样做?
    • 问题是我只有import java.util.Scanner; import java.util.ArrayList; import java.io.File;,我无法导入其他工具
    【解决方案2】:

    没有收藏?你想用数组吗?

    String [] names = new String[data.length];
    int [] counts = new int[data.length];
    
    int j = 0;
    for (int i = 0; i < data.lenght; i++ ) {
       while (j < data.length) {
          if (data[i][0].equals(names[j])) {
             found = true;
             counts[j]++;
             break;
          } else if (names[j] == null) {
             names[j] = data[i][0];
             counts[j]=1;
             break;
          }
          j++;
       }
    }
    
    // find max count
    // println
    // nullify element
    // repeat
    
    for (int i = 0; i < j; i++) {
        int max = -1;
        int k = i;
        int pos = -1;
        while ( k < j ) {
           if ( counts[k] > max ) {
              pos = k;
              max = counts[k];
           }
           k++;
        }
        if ( pos != -1 ) { // we found 
           System.out.println ( names[pos] + ": " + counts[pos]);
           counts[pos] = -1;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-06
      • 2016-03-07
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      相关资源
      最近更新 更多