2D数组整数列按降序排列

时间:2014-12-05 04:36:57

标签: java sorting multidimensional-array selection-sort

我遇到了" //克林顿的代表从最高到最低的顺序#34;我意识到它目前处于升序状态(我对此更为熟悉),但它仍然没有足够的时间循环甚至正确地执行升序。我希望第三列按降序排列 - >的Integer.parseInt(初级[行] [2])。

import java.io.*;
public static void main(String[] args) throws IOException
{
    //Read text file
    FileReader fr = new FileReader("primary1.txt");
    BufferedReader br = new BufferedReader(fr);

    //2D array
    String[][] primary = new String[44][5];

    //Section break 
    System.out.println("1. The file contents are:\n");

    //Add column titles
    System.out.println("States\t\t\t\tCandidate#1\tVotes\t\tCandidate#2\tVotes");

    //Set delimiter as "/"
    String line; 
    int i=0;
    while((line=br.readLine())!=null){
        primary[i]=line.split("/");
        i++;
    }

    //Print text file
    for(int row=0; row<primary.length; row++){
        for(int col=0; col<primary[row].length; col++){
            //Add space between columns
            System.out.print(primary[row][col] + "\t\t");
        }

        //Newline
        System.out.println("");
    }        

    //Clinton's delegates in order from highest to lowest
    int temp=0;

    for(int row=0; row<primary.length-1; row++){
        //Parse Integer
        int delC = Integer.parseInt(primary[row][2]);
        int delC1 = Integer.parseInt(primary[row+1][2]);

        if(delC > delC1){
            temp=delC1;
            delC1=delC;
            delC=temp;
        }
        System.out.println(temp);
    }
}

2 个答案:

答案 0 :(得分:0)

我认为您的代码看起来像尝试冒泡排序。如果这是你想要做的,那么你需要有2个for循环来迭代内容并交换它们。由于您使用的是String数组,因此需要交换数组元素。不仅仅是数组中的数字用于显示。请参阅以下修改后的代码。

我看到你标记了选择排序。如果您需要,请告诉我。下面的冒泡排序,因为你看起来像这样做。

public static void main(String[] args) throws IOException {
    // Read text file
    FileReader fr = new FileReader("primary1.txt");
    BufferedReader br = new BufferedReader(fr);

    // 2D array
    String[][] primary = new String[44][5];

    // Section break
    System.out.println("1. The file contents are:\n");

    // Add column titles
    System.out.println("States\t\t\t\tCandidate#1\tVotes\t\tCandidate#2\tVotes");

    // Set delimiter as "/"
    String line;
    int i = 0;
    while ((line = br.readLine()) != null) {
        primary[i] = line.split("/");
        i++;
    }

    // Print text file
    for (int row = 0; row < primary.length; row++) {
        for (int col = 0; col < primary[row].length; col++) {
            // Add space between columns
            System.out.print(primary[row][col] + "\t\t");
        }

        // Newline
        System.out.println("");
    }

    // Clinton's delegates in order from highest to lowest
    int temp = 0;
    // bubble sort
    for (int row = 0; row < primary.length - 1; row++) {
        for (int k = row + 1; k < primary.length; k++) {
            // Parse Integer
            int delC = Integer.parseInt(primary[row][2]);
            int delC1 = Integer.parseInt(primary[k][2]);
            int delO = Integer.parseInt(primary[row][4]);

            if (delC >= delC1) {
                // swap contents
                swap(primary, row, k);
                temp = delC1;
                delC1 = delC;
                delC = temp;
            }
        }
        // System.out.println(temp);
    }

    // Print output
    for (int row = 0; row < primary.length; row++) {
        System.out.println(primary[row][2]);
    }
}

private static void swap(String[][] primary, int row, int k) {
    String[] temp = primary[k];
    primary[k] = primary[row];
    primary[row] = temp;
}

答案 1 :(得分:0)

试试这个:

public static void main(String[] args) throws IOException {
    //Read text file
    FileReader fr = new FileReader("primary1.txt");
    BufferedReader br = new BufferedReader(fr);

    //2D array
    String[][] primary = new String[44][5];

    //Section break 
    System.out.println("1. The file contents are:\n");

    //Add column titles
    System.out.println("States\t\t\t\tCandidate#1\tVotes\t\tCandidate#2\tVotes");

    //Set delimiter as "/"
    String line; 
    int i=0;
    while((line=br.readLine())!=null){
        primary[i]=line.split("/");
        i++;
    }

    //Print text file
    for(int row=0; row<primary.length; row++){
        for(int col=0; col<primary[row].length; col++){
            //Add space between columns
            System.out.print(primary[row][col] + "\t\t");
        }
        //Newline
        System.out.println();
    }        

    //Clinton's delegates in order from highest to lowest
    for(int row=0; row<primary.length-1; row++){            
        for(int row1=row+1; row1<primary.length; row1++) {
            //Parse Integer
            int delC = Integer.parseInt(primary[row][2]);
            int delC1 = Integer.parseInt(primary[row1][2]);
            if(delC < delC1){
                String[]tmpprimary = primary[row];
                primary[row] = primary[row1];
                primary[row1] = tmpprimary;               
            }
        }                
    }
    System.out.println("\n**************************** Order Descending *******************************");
    System.out.println("States\t\t\t\tCandidate#1\tVotes\t\tCandidate#2\tVotes");
    for(int row=0; row<primary.length; row++){
        for(int col=0; col<primary[row].length; col++){
            //Add space between columns
            System.out.print(primary[row][col] + "\t\t");
        }
        //Newline
        System.out.println();
    } 
}