2016-05-05 11 views
1

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 

Antwort

0

Ohne Sammlungen? Sie möchten Arrays verwenden?

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

Wenn Sie die Liste der Bandnamen (mit Dubletten) sortieren und dann zählen, wie viele der einzelnen Bandnamen in der Liste enthalten ist, werden Sie das Album Zahl für jedes Band erhalten:

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

wie Kann so etwas ohne die Verwendung von Collection gemacht werden? –

+0

Sie könnten die Array-Liste manuell sortieren, aber warum sollten Sie, wenn Collections.sort() verfügbar ist? – Jason

+0

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 –

Verwandte Themen