如何根据用户输入对字符串数组进行排序

时间:2018-11-11 08:49:36

标签: java arrays sorting columnsorting

我试图使用用户要编辑哪一列的用户输入来对String数组进行排序。但是,无法显示已排序的数组。我不知道如何修改或在哪里修改。在这里,字符串数组是由名字,姓氏和中间名首字母组成的。因此,逻辑是比较相邻的列,而前者按字母顺序比较晚,然后进行交换。如果两个数组具有相同的字符串,则比较下一行和同一列,然后再次进行交换。 (仅适用于第一列和第二列)

我也想根据用户要排序的列的输入来修改数组。我是否正确使用Scanner类?

这是我的代码:

import java.util.Scanner; 
public class Multi_dimensional_array {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String students[][]= {{"Prachaya" , "Johnson" , "M"}, {"Hafsa" , "Pillips", "R" }, { "Junnosuke", "Andews", "D"}, {"Aithima" , "Peters", "H"}, {"Jenwiji", "Davis","J" }};
    int arrlen = students.length;
    System.out.println("original array is:" );
    System.out.println();
    for(int count1 = 0; count1 < arrlen; count1++) 
    {
        for(int count2 = 0; count2 < arrlen -2; count2++) 
        {
            System.out.print(students[count1][count2] + "   \t" );
        }
        System.out.println(); 
    }
    String temp = "";


    //This is sorted array according to user's input 

    int x = 0; int y = 0; 
    int column; 
    Scanner sc =new Scanner (‭System.in‬);

    //Ask user's imput of column he/she wants to edit
    System.out.println();
    System.out.println("Please Enter the number of column you want to edit: ");
    column = sc.nextInt();

    if ( column == 1)
    {

        if( x < arrlen - 1)
        {
    for  (x = 0; x < arrlen; x++)
    {
        //swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0] 
    if (students[x][y].compareTo(students[x + 1][y])> 0) 
    {
        temp = students[x][y] ; 
        students[x][y] = students[x + 1][y];
        students[x + 1][y] = temp;
        temp = students[x][y + 1] ; 
        students[x][y + 1] = students[x + 1][y + 1];
        students[x + 1][y + 1] = temp;
        temp = students[x][y + 2] ; 
        students[x][y + 2] = students[x + 1][y + 2];
        students[x + 1][y + 2] = temp;
    } 
    x = 0; y = 0; 
    //compare the top array with bottom strings  
    for (int j = x + 2; j< arrlen; j++)
    {
        if (students[x][y].compareTo(students[x + 2][y])> 0 )
        {
            temp = students[x][y] ; 
            students[x][y] = students[x + j][y];
            students[x + j][y] = temp;
            temp = students[x][y + 1] ; 
            students[x][y + 1] = students[x + j][y + 1];
            students[x + j][y + 1] = temp;
            temp = students[x][y + 2] ; 
            students[x][y + 2] = students[x + j][y + 2];
            students[x + j][y + 2] = temp;
        }
    }
    }
        }
    else 
    {
        if( x < arrlen - 1)
        {
    for  (x = 0; x < arrlen; x++)
    {
        if (students[x][y].compareTo(students[x + 1][y]) == 0)
        {
        if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0) 
        {
        temp = students[x][y] ; 
        students[x][y] = students[x + 1][y];
        students[x + 1][y] = temp;
        temp = students[x][y + 1] ; 
        students[x][y + 1] = students[x + 1][y + 1];
        students[x + 1][y + 1] = temp;
        temp = students[x][y + 2] ; 
        students[x][y + 2] = students[x + 1][y + 2];
        students[x + 1][y + 2] = temp;
        }
        x = 0; y = 0; 
        for (int j = x + 2; j< arrlen; j++)
        {
            if (students[x][y].compareTo(students[x + 2][y])> 0 )
            {
                temp = students[x][y] ; 
                students[x][y] = students[x + j][y];
                students[x + j][y] = temp;
                temp = students[x][y + 1] ; 
                students[x][y + 1] = students[x + j][y + 1];
                students[x + j][y + 1] = temp;
                temp = students[x][y + 2] ; 
                students[x][y + 2] = students[x + j][y + 2];
                students[x + j][y + 2] = temp;
                }
            }
        }
    } 

        }
        }
    }

    if ( column == 2)
        y = 1; 
    {

        if( x < arrlen - 1)
        {
    for  (x = 0; x < arrlen; x++)
    {
        //swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0] 
    if (students[x][y].compareTo(students[x + 1][y])> 0) 
    {
        temp = students[x][y] ; 
        students[x][y] = students[x + 1][y];
        students[x + 1][y] = temp;
        temp = students[x][y + 1] ; 
        students[x][y + 1] = students[x + 1][y + 1];
        students[x + 1][y + 1] = temp;
        temp = students[x][y + 2] ; 
        students[x][y + 2] = students[x + 1][y + 2];
        students[x + 1][y + 2] = temp;
    } 
    x = 0; y = 1; 
    //compare the top array with bottom strings  
    for (int j = x + 2; j< arrlen; j++)
    {
        if (students[x][y].compareTo(students[x + 2][y])> 0 )
        {
            temp = students[x][y] ; 
            students[x][y] = students[x + j][y];
            students[x + j][y] = temp;
            temp = students[x][y + 1] ; 
            students[x][y + 1] = students[x + j][y + 1];
            students[x + j][y + 1] = temp;
            temp = students[x][y + 2] ; 
            students[x][y + 2] = students[x + j][y + 2];
            students[x + j][y + 2] = temp;
        }
    }
    }
        }
    else 
    {
        if( x < arrlen - 1)
        {
    for  (x = 0; x < arrlen; x++)
    {
        if (students[x][y].compareTo(students[x + 1][y]) == 0)
        {
        if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0) 
        {
        temp = students[x][y] ; 
        students[x][y] = students[x + 1][y];
        students[x + 1][y] = temp;
        temp = students[x][y + 1] ; 
        students[x][y + 1] = students[x + 1][y + 1];
        students[x + 1][y + 1] = temp;
        temp = students[x][y + 2] ; 
        students[x][y + 2] = students[x + 1][y + 2];
        students[x + 1][y + 2] = temp;
        }
        x = 0; y = 1; 
        for (int j = x + 2; j< arrlen; j++)
        {
            if (students[x][y].compareTo(students[x + 2][y])> 0 )
            {
                temp = students[x][y] ; 
                students[x][y] = students[x + j][y];
                students[x + j][y] = temp;
                temp = students[x][y + 1] ; 
                students[x][y + 1] = students[x + j][y + 1];
                students[x + j][y + 1] = temp;
                temp = students[x][y + 2] ; 
                students[x][y + 2] = students[x + j][y + 2];
                students[x + j][y + 2] = temp;
                }
            }
        }
    } 

        }
        }
    }

    if ( column == 3)
        y = 2; 
    {

        if( x < arrlen - 1)
        {
    for  (x = 0; x < arrlen; x++)
    {
        //swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0] 
    if (students[x][y].compareTo(students[x + 1][y])> 0) 
    {
        temp = students[x][y] ; 
        students[x][y] = students[x + 1][y];
        students[x + 1][y] = temp;
        temp = students[x][y + 1] ; 
        students[x][y + 1] = students[x + 1][y + 1];
        students[x + 1][y + 1] = temp;
        temp = students[x][y + 2] ; 
        students[x][y + 2] = students[x + 1][y + 2];
        students[x + 1][y + 2] = temp;
    } 
    x = 0; y = 2; 
    //compare the top array with bottom strings  
    for (int j = x + 2; j< arrlen; j++)
    {
        if (students[x][y].compareTo(students[x + 2][y])> 0 )
        {
            temp = students[x][y] ; 
            students[x][y] = students[x + j][y];
            students[x + j][y] = temp;
            temp = students[x][y + 1] ; 
            students[x][y + 1] = students[x + j][y + 1];
            students[x + j][y + 1] = temp;
            temp = students[x][y + 2] ; 
            students[x][y + 2] = students[x + j][y + 2];
            students[x + j][y + 2] = temp;
        }
    }
    }
        }
    else 
    {
        if( x < arrlen - 1)
        {
    for  (x = 0; x < arrlen; x++)
    {
        if (students[x][y].compareTo(students[x + 1][y]) == 0)
        {
        if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0) 
        {
        temp = students[x][y] ; 
        students[x][y] = students[x + 1][y];
        students[x + 1][y] = temp;
        temp = students[x][y + 1] ; 
        students[x][y + 1] = students[x + 1][y + 1];
        students[x + 1][y + 1] = temp;
        temp = students[x][y + 2] ; 
        students[x][y + 2] = students[x + 1][y + 2];
        students[x + 1][y + 2] = temp;
        }
        x = 0; y = 2; 
        for (int j = x + 2; j< arrlen; j++)
        {
            if (students[x][y].compareTo(students[x + 2][y])> 0 )
            {
                temp = students[x][y] ; 
                students[x][y] = students[x + j][y];
                students[x + j][y] = temp;
                temp = students[x][y + 1] ; 
                students[x][y + 1] = students[x + j][y + 1];
                students[x + j][y + 1] = temp;
                temp = students[x][y + 2] ; 
                students[x][y + 2] = students[x + j][y + 2];
                students[x + j][y + 2] = temp;
                }
            }
        }
    } 

        }
        }
    }

    System.out.println();
    System.out.println("modified array is:");
    System.out.println(); 
    for(int count1 = 0; count1 < arrlen -1; count1++) 
    {
        for(int count2 = 0; count2 < arrlen -2; count2++) 
        {
            System.out.print(students[count1][count2] + "   \t" );
        }

        System.out.println(); 
    }

} 
}

0 个答案:

没有答案