Ich habe ein Programm, das eine Dateiliste von Bands und Alben aufnimmt. Ich muss die Anzahl der Alben bestimmen, die jede Band macht, und dann eine Liste der Bands und der Anzahl der Alben in absteigender Reihenfolge ausdrucken. Ich habe mich umgesehen und es mit Mapping und Sammlungen gemacht. Ich möchte wissen, wie es auch ohne es geht. Hier ist, was ich bisher:Wie zähle ich mehrere doppelte Elemente in einer ArrayList?
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++;
}
}
}
}
Eingabebeispiel:
band1 -album
band2 -album
band1 -album
band3 -album
band1 -album
band2 -album
Ausgabe Beispiel:
band1: 3
band2: 2
band3: 1
wie Kann so etwas ohne die Verwendung von Collection gemacht werden? –
Sie könnten die Array-Liste manuell sortieren, aber warum sollten Sie, wenn Collections.sort() verfügbar ist? – Jason
das Problem ist, ich habe nur 'Import java.util.Scanner; importieren java.util.ArrayList; importieren Sie java.io.File; ' und ich kann andere utils nicht importieren –