0

我想对多维字符串数组进行冒泡排序,但它似乎不起作用

public class Array {
    public static void main( final String args[] ) {
        final String[][] arrDataIn = 
        {
            { "abc", "9", "5" },
            { "xyz", "3", "4" },
            { "lmn", "7", "3" },
            { "hij", "1", "3" }
        };

        final Array bs = new Array();

        final String arrSorted[][] = bs.sort( arrDataIn, 0, 1 );
        for ( int y = 0; y < arrSorted.length; y++ ) {
            System.out.println( "Printing Row Number :" + y );

            for ( int z = 0; z < arrSorted[y].length; z++ )
                System.out.println( "Value : " + arrSorted[y][z] );
        }
    }

    public String[][] sort( final String arrDataIn[][], final int iCol, final int iOrder ) {
        final String arrTmp[] = new String[ arrDataIn[0].length ];

        for ( int z = 0; z < arrDataIn.length; z++ ) {
            for ( int i = 0; i < arrDataIn.length - ( z + 1 ); i++ ) {
                if ( arrDataIn[i].compareTo( arrDataIn[i + 1] ) < 0 ) {
                    for ( int j = 0; j < arrDataIn[i].length; j++ ) {
                        arrTmp[j] = arrDataIn[i][j];
                        arrDataIn[i][j] = arrDataIn[i + 1][j];
                    }

                    for ( int m = 0; m < arrTmp.length; m++ )
                        arrDataIn[i + 1][m] = arrTmp[m];
                } else if ( arrDataIn[i].compareTo( arrDataIn[i + 1] ) < 0 ) {
                    for ( int j = 0; j < arrDataIn[i].length; j++ ) {
                        arrTmp[j] = arrDataIn[i][j];
                        arrDataIn[i][j] = arrDataIn[i + 1][j];
                    }

                    for ( int m = 0; m < arrTmp.length; m++ )
                        arrDataIn[i + 1][m] = arrTmp[m];
                }
            }

        }

        return arrDataIn;
    }
}

输出错误显示:

error: cannot find symbol
if (arrDataIn[i].compareTo(arrDataIn[i+1])<0)
                ^
  symbol:   method compareTo(String[])
  location: class String[]
error: cannot find symbol
else if (arrDataIn[i].compareTo(arrDataIn[i+1])<0)
                     ^
  symbol:   method compareTo(String[])
  location: class String[]

我尝试将其更改为:

if (arrDataIn[i][iCol] > arrDataIn[i+1][iCol] && (iOrder == 0))

else if (arrDataIn[i][iCol] < arrDataIn[i+1][iCol] && (iOrder == 1))

它仍然是同样的错误

4

1 回答 1

2

arrDataIn[i]compareTo()是一个字符串数组,并且数组对象上没有方法

首先,你必须意识到你想做什么。可能您想按字典顺序对数组进行排序

from:
  {
    { "2", "4", "5" }, // if possible
    { "3", "1" },
    { "2", "4" }
  }
to:
  {
    { "2", "4" },
    { "2", "4", "5" },
    { "3", "1" }
  }

您只需要compareTo( String[] sa1, String[] sa2 )根据compareTo() 描述实现方法。

于 2012-04-25T09:11:40.067 回答