English 中文(简体)
Sorting Java Multidimensional Array
原标题:Sorting Java Multidimensional Array

我试图对阵列的内容进行分类,虽然似乎正在工作(没有发生错误;正在从事某种工作),但头10个阵列却与其余各行不通。

coordSort.java

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class coordSort {
@SuppressWarnings({ "unchecked", "unused" })
public static void main (String args[]) throws IOException {

    String xCoord, yCoord;
    int coordSum;
    Scanner input = new Scanner(System.in);

    //Get x coordinate from user
    System.out.print("Enter x coordinate: ");
    xCoord = input.next();

    //Get x coordinate from user
    System.out.print("Enter y coordinate: ");
    yCoord = input.next();

    boolean sort = false;

    char[] a = xCoord.toCharArray();
    char[] b = yCoord.toCharArray();

    //validate user input is a digit 
    if ( (Character.isDigit(a[0]))  ) {     
        if(Character.isDigit(b[0]) ){
            //digits entered - begin processing all coordinate values
            sort = true;
        }
    }

    //If validation failed, inform user
    if(!sort){
        System.out.println("Please enter a positive numeric value.");
    }

    if(sort){       
        //determine SUM of user entered coordinates
        coordSum = Integer.parseInt(xCoord) + Integer.parseInt(yCoord);

        //define coordinate array
        String[][] coordUnsortedArray = new String[26][3];
        int counter;
        int commaCount;
        String xCoordIn, yCoordIn;
        int intXCoordIn, intYCoordIn, sumCoordIn, coordDiff;

        //define input file
        FileInputStream fileIn = new FileInputStream("coords.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(fileIn)); 

        for (int j = 0; j < coordUnsortedArray.length; j++){
            counter = 0;
            commaCount = 0;
            //line from file to variable
            String coordSet = reader.readLine();     

            //look for the second "," to determine end of x coordinate
            for(int k = 0; k < coordSet.length(); k++){
                if (coordSet.charAt(k) ==  , ) {
                    commaCount++;
                    counter++;
                    if (commaCount == 2){
                        break;
                    }
                }else{
                    counter++;
                }
            }

            //define x coordinate
            xCoordIn = (coordSet.substring(2,(counter - 1)));
            intXCoordIn = Integer.parseInt(xCoordIn);

            //define y coordinate
            yCoordIn = (coordSet.substring((counter),coordSet.length()));
            intYCoordIn = Integer.parseInt(yCoordIn);

            //coordinate calculations
            sumCoordIn = Integer.parseInt(xCoordIn) + Integer.parseInt(yCoordIn);
            coordDiff = sumCoordIn - coordSum;

            //load results to array
            coordUnsortedArray[j][0] = xCoordIn;
            coordUnsortedArray[j][1] = yCoordIn;
            coordUnsortedArray[j][2] = Integer.toString(coordDiff);

            //Output Array (BEFORE SORTING)
            //System.out.println((j + 1) + ") " + coordUnsortedArray[j][0] + " : " + coordUnsortedArray[j][1] + " : " + coordUnsortedArray[j][2]);                  
        }             

        System.out.println("
");

        fileIn.close();

        String[][] coordsSorted = new String[26][3];

        //Sort array coordDiff, column 3
        Arrays.sort(coordUnsortedArray, new ColumnComparator(2));

        //Print the sorted array
        for(int i = 0; i < coordUnsortedArray.length; i++){
            String[] row = coordUnsortedArray[i];
            System.out.print((i + 1) + ") ");
            for(int j = 0; j < row.length; j++) {
                //System.out.print(row[j] + " | ");
                coordsSorted[i][j] = row[j];
                System.out.print(coordsSorted[i][j] + " : ");
            }
            System.out.print("
");
        }
    }
}
}

<斯特隆>类别Coords.java 页: 1

import java.util.Comparator;

@SuppressWarnings("rawtypes")
class ColumnComparator implements Comparator {
    int columnToSort;
    ColumnComparator(int columnToSort) {
        this.columnToSort = columnToSort;
    }
    //overriding compare method
    public int compare(Object o1, Object o2) {
        String[] row1 = (String[]) o1;
        String[] row2 = (String[]) o2;
        //compare the columns to sort
        return row1[columnToSort].compareTo(row2[columnToSort]);
    }

    //overriding compare method
    public int compare1(Object o1, Object o2) {
        String[] row1 = (String[]) o1;
        String[] row2 = (String[]) o2;
        //compare the columns to sort
        return row1[columnToSort].compareTo(row2[columnToSort]);
    }
} 

我正试图在第3栏按数字顺序排列阵列。 无人驾驶的阵列有一个包含下列内容的文本档案:

a,44,67

b,31,49

c,93,6

我正在对阵列进行计算,相比之下,用户投入和阵列如下:

44,67,101

31,49,70

页: 1

我祝愿阿雷拉产生以下成果:

31,49,70

页: 1

44,67,101

问题回答

这里可能的混淆:

 return row1[columnToSort].compareTo(row2[columnToSort])

这是细微的比较,而不是数字比较。 如果你根据指示进行分类,那么你将获得与你的数字不同的结果——即“1”、“10”、“100”、“9”和1 9 100 100。

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer.parseInt,如果你能够把其余部分排除在外,就可自由地提出更多的问题。

stated子指出。 你们需要比较他们的内在价值,即你们需要在那里投放。

    int num1 = Integer.parseInt(row1[columnToSort]);
    int num2 = Integer.parseInt(row2[columnToSort]);
    if(num1 > num2)
            return 1;
    else 
            return 0;

比较法的位置 进行方法和检查。 收益报表按逆向顺序排列。

此外,在<代码>compare中添加了一些错误核对。 方法将使该守则更加有效。

在此处提供援助之后,奥基就是我们找到的解决办法。 再次感谢每个人的帮助! 希望以下法典有助于其他人。

班级法1 纽约总部

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class coordSort {
    @SuppressWarnings({ "unchecked" })
    public static void main (String args[]) throws IOException {

        String xCoordChar, yCoordChar;
        int xCoord, yCoord, coordSum;
        Scanner input = new Scanner(System.in);

        //Get x coordinate from user
        System.out.print("Enter x coordinate: ");
        xCoordChar = input.next();

        //Get x coordinate from user
        System.out.print("Enter y coordinate: ");
        yCoordChar = input.next();

        boolean sort = false;

        char[] a = xCoordChar.toCharArray();
        char[] b = yCoordChar.toCharArray();

        //validate user input is a digit 
        if ( (Character.isDigit(a[0]))  ) {     
            if(Character.isDigit(b[0]) ){
                //digits entered - begin processing all coordinate values
                sort = true;
            }
        }

        //If validation failed, inform user
        if(!sort){
            System.out.println("Please enter a positive numeric value.");
        }

        if(sort){       
            //Parse user input characters to Integers
            xCoord = Integer.parseInt(xCoordChar);
            yCoord = Integer.parseInt(yCoordChar);

            //determine SUM of user entered coordinates
            coordSum = xCoord + yCoord;

            //define coordinate array
            int[][] coordUnsortedArray = new int[26][3];
            int counter;
            int commaCount;
            String xCoordIn, yCoordIn;
            int intXCoordIn, intYCoordIn, sumCoordIn, coordDiff;

            //define input file
            FileInputStream fileIn = new FileInputStream("coords.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader    (fileIn)); 

            for (int j = 0; j < coordUnsortedArray.length; j++){
                counter = 0;
                commaCount = 0;
                //line from file to variable
                String coordSet = reader.readLine();     

                //look for the second "," to determine end of x coordinate
                for(int k = 0; k < coordSet.length(); k++){
                    if (coordSet.charAt(k) ==  , ) {
                        commaCount++;
                        counter++;
                        if (commaCount == 2){
                            break;
                        }
                    }else{
                        counter++;
                    }
                }

                //define x coordinate
                xCoordIn = (coordSet.substring(2,(counter - 1)));
                intXCoordIn = Integer.parseInt(xCoordIn);

                //define y coordinate
                yCoordIn = (coordSet.substring((counter),coordSet.length()));
                intYCoordIn = Integer.parseInt(yCoordIn);

                //coordinate calculations
                sumCoordIn = Integer.parseInt(xCoordIn) + Integer.parseInt    (yCoordIn);
                coordDiff = sumCoordIn - coordSum;

                if (coordDiff < 0){
                    coordDiff = coordDiff * (-1);
                }

                //load results to array
                coordUnsortedArray[j][0] = intXCoordIn;
                coordUnsortedArray[j][1] = intYCoordIn;
                coordUnsortedArray[j][2] = coordDiff;                           
            }             

            fileIn.close();
            System.out.print("
");
            System.out.println("Array Before Sorting:");
            System.out.println("=====================");

            //Array Before Sorting
            for(int i = 0; i < coordUnsortedArray.length; i++){
                int[] row = coordUnsortedArray[i];
                System.out.print((i + 1) + ") ");
                for(int j = 0; j < (row.length - 1); j++) {
                    coordUnsortedArray[i][j] = row[j];
                    if(j < 1){
                        System.out.print(coordUnsortedArray    [i]    [j] + ",");   
                    }else{
                        System.out.println(coordUnsortedArray    [i]    [j]);
                    }
                }
            }

            System.out.print("
");
            System.out.print("
");

            //Sort array coordDiff, column 3
            Arrays.sort(coordUnsortedArray, new ColumnComparator(2));

            System.out.println("Array After Sorting:");
            System.out.println("====================");

            //Original Array After Sorting
            for(int i = 0; i < coordUnsortedArray.length; i++){
                int[] row = coordUnsortedArray[i];
                System.out.print((i + 1) + ") ");
                for(int j = 0; j < (row.length - 1); j++) {
                    coordUnsortedArray[i][j] = row[j];
                    if(j < 1){
                        System.out.print(coordUnsortedArray[i][j] + ",");   
                    }else{
                        System.out.println(coordUnsortedArray    [i]    [j]);
                    }
                }
            }
            }
        }
}

第2类 纽约总部

import java.util.Comparator;

@SuppressWarnings("rawtypes")
class ColumnComparator implements Comparator {
int columnToSort;
ColumnComparator(int columnToSort) {
this.columnToSort = columnToSort;
}

//Compare method
public int compare(Object o1, Object o2) {
    int[] row1 = (int[]) o1;
    int[] row2 = (int[]) o2;

    int intRow1 = (row1[columnToSort]);
    int intRow2 = (row2[columnToSort]);

    return new Integer(intRow1).compareTo(new Integer(intRow2));
}
}    




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签