java two dimensional array sorting

2019-08-29 11:24发布

问题:

Write a program that prompts the user to enter a nxn matrix of double values and displays a new matrix which has the columns of the initial matrix sorted. You may use any sorting algorithm to solve the problem; please specify the name of the used sorting algorithm into your code header. Your program must implement a sorting algorithm; you cannot use the sorting methods provided in the Array class. The sorting should be implemented into a method, in which a new array is returned and the original array is intact:

public static double[][] sortCol(double[][] a)

The program should also implement a method that prints the initial and the result matrices to user. The print out should be nicely formatted. Here is a sample run:

What is the dimension of matrix? 3 
Enter a 3x3 matrix row by row: 

0.15 0.875 0.375

0.55 0.005 0.225

0.30 0.12 0.4

The column sorted array is: 

0.15 0.005 0.225

0.3 0.12 0.375

0.55 0.875 0.4

This is what I have. I believe it is almost perfect. The sorting method I used I think will sort the column but it may also be sorting the rows. However when I run the program I get this...

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextDouble(Scanner.java:2456) at Hmwk3_jrgluck.main(Hmwk3_jrgluck.java:16)

Any ideas/ help..

import java.util.Scanner;

public class sdfjasdf {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("What is the dimension of your matrix?");
        int matrixdim = input.nextInt();
        double[][] matrix = new double[matrixdim][matrixdim];

        System.out.println("Enter " + matrixdim + " rows, and " + matrixdim
                + " columns.");
        Scanner input1 = new Scanner(System.in);
        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix.length; column++)
                matrix[row][column] = input1.nextDouble();
        }
        System.out.println(sortCol(matrix));
    }

    public static double sortCol(double[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            double currentMin = matrix[i][0];
            int currentMinIndex = i;

            for (int j = i; j < matrix.length; j++) {
                if (currentMin > matrix[j][0]
                        || (currentMin == matrix[j][0] && matrix[currentMinIndex][1] > matrix[j][1])) {
                    currentMin = matrix[j][0];
                    currentMinIndex = j;
                }
            }

            if (currentMinIndex != i) {
                double temp0 = matrix[currentMinIndex][0];
                double temp1 = matrix[currentMinIndex][1];
                matrix[currentMinIndex][0] = matrix[i][0];
                matrix[currentMinIndex][1] = matrix[i][1];
                matrix[i][0] = temp0;
                matrix[i][1] = temp1;
            }
        }
        return sortCol(matrix);
    }
}

回答1:

I suspect that your locale can require commas instead dots in float number format. Try changing your data to

0,15 0,875 0,375
0,55 0,005 0,225
0,30 0,12 0,4

If that is true but you prefer to (or must) use dots instead of comma you can change locale used in Scanner by invoking

input.useLocale(new Locale("en", "US"));

or change global Locale before creating Scanner object with

Locale.setDefault(new Locale("en", "US"));

Also return type of sortCol should be aether

  • double[][] in case you want to return sorted copy of array (without changing original one). In that case you will need to first create copy of original array
  • void in case you want to sort original array (you don't have to return reference to object that you already have since you used it as methods argument)

Right now you are trying to return double by invoking again sortCol(matrix), so it again will try to return sortCol(matrix) (and so on) which will lead to stack overflow.