1

我有一个对象数组,假设每个对象都是网格类型。

每个 Grid 对象都有 x 和 y 坐标,

Grid temp = new Grid(3, 5);
// temp.x returns x, temp.y returns y.

现在我有几个 Grid 数组

Grid[] theArray1 = new Grid[5];
Grid[] theArray2 = new Grid[5];
Grid[] theArray3 = new Grid[5];

我用 Grid 对象填充数组并使用 Arrays.sort 对它们进行排序。

我现在加入已排序的数组以形成长度为 5+5+5=15 的数组。

我现在想按“子数组”的第一个元素(theArray 中的元素 0、5 和 10)对 theArray 进行排序

我怎么能做到这一点?此外,如果有更简单的方法可以达到相同的结果,那就太好了。但我必须从 3 个数组开始,因为它们是通过 for 循环的迭代获得的。

编辑:

示例:假设我按 X 协调排序,先小一点。为简单起见,我将每个 Grid[] 长度设为 3 而不是 5。

Grid[] theArray1 = new Grid[]{new Grid(2, 1), new Grid(4, 1), new Grid(0, 1)};
Grid[] theArray2 = new Grid[]{new Grid(4, 2), new Grid(3, 1), new Grid(7, 1)};
Grid[] theArray3 = new Grid[]{new Grid(1, 7), new Grid(5, 3), new Grid(10, 1)};

我想要结束的是一个数组/数组列表,当打印时,打印如下:

for (int i = 0; i <= theArray.length-2; i++) {
    StdOut.println(theArray[i] + ", " + theArray[i+1] + ", " + theArray[i+2] + "\n");
}

// Output:

(0, 1), (2, 1), (4, 1) //this is theArray1
(1, 7), (5, 3), (10, 1) //this is theArray3
(3, 1), (4, 2), (7, 1) //this is theArray2

首先,我对每个 theArray(1, 2 和 3) 进行排序,使 x 坐标最低的元素排在第一位,然后是第二小,然后是最大的。

然后我按照每个数组的第一个元素的大小排列这些数组。theArray3 在 theArray2 之前,因为第一个元素的 x 坐标是 1 但在 theArray2 中是 3

4

5 回答 5

1

您可以创建一个二维数组而不是 3 个单独的数组。然后你可以先对二维数组进行排序,以使它们相互关联,然后加入它们。

    Grid[][] twoD;

    /* ... */

    // sort each array separately
    for(int i=0; i<twoD.length; i++){
        Arrays.sort(twoD[i], /* COMPARATOR */);
    }

    // sort the arrays based on x coordinate of first element
    Arrays.sort(twoD, new Comparator<Grid[]>() {
        public int compare(Grid[] a, Grid[] b) {
            return Integer.compare(a[0].x, b[0].x);
        }
    });

    /* NOW JOIN THEM */

如果实际上只有 3 个数组,你真的可以检查哪个是最小的,先添加,等等。

于 2013-09-24T21:05:31.763 回答
1

假设Gridimplements Comparable<Grid>,对每个数组进行排序并添加到二维数组。然后使用 对网格数组的数组进行排序,Arrays.sort(Grid[][], GridArrayComparator)例如GridArrayComparator

class GridArrayComparator implements Comparator<Grid[]> {
   public int compare(Grid[] grids1, Grid[] grids2) {
       if (grids1.length > 0 && grids1.length > 0) {
           return grids1[0].compareTo(grids2[0]);
       } else if (grids1.length > 0) {
           return 1;
       } else if (grids2.length > 0) {
           return -1;
       } else {
           return 0;
       }
   }
}

然后将二维数组复制到一维数组。

于 2013-09-24T21:39:12.123 回答
1

这可能有点重量级,但有效。假设你已经合并了你的数组,它必须被排序(theArray),你可以在你的数组上创建一个特殊的列表视图

private static class BlockBasedListView<T> extends AbstractList<T[]> {

    private int blockSize;
    private T[] array;

    public BlockBasedListView(T[] array, int blockSize) {
        this.array = array;
        this.blockSize = blockSize;
    }

    @Override
    public T[] get(int index) {
        return Arrays.copyOfRange(array, index * blockSize, index * blockSize + blockSize);
    }

    @Override
    public T[] set(int index, T[] element) {
        T[] previousElement = get(index);
        System.arraycopy(element, 0, array, index * blockSize, blockSize);
        return previousElement;
    }

    @Override
    public int size() {
        return array.length / blockSize;
    }

}

这是一个简单的列表实现,其中一个元素是blockSize原始数组的一个元素块(大小为 )。请注意,这是您的数组的视图:写入此列表会影响原始数组。

然后,您创建一个块的比较器Grid

private static class CompareByFirstElement implements Comparator<Grid[]> {

    @Override
    public int compare(Grid[] o1, Grid[] o2) {
        return o1[0].x - o2[0].x;
    }
}

theArray最后,您可以按如下方式排序:

BlockBasedListView<Grid> blockBasedList = new BlockBasedListView<Grid>(theArray, 5);
Collections.sort(blockBasedList, new CompareByFirstElement());
于 2013-09-24T21:49:17.220 回答
0

当您加入已排序的数组时,您将创建一个大小为 15 的新数组。新数组具有添加到其中的值,但与子数组无关。因此,如果您应用排序,它将在整个数组上。如果需要排序,则必须使用另一个大小为 5 的数组变量,对其进行排序,然后写回主数组。

于 2013-09-24T21:00:39.867 回答
0

您可以使用 Comparable,也许这会有所帮助..

import java.util.Arrays;

public class Grid implements Comparable<Grid> {

    public static void main(String[] args) {
        Grid a = new Grid( 2, 15 ); // 3
        Grid b = new Grid( 1, 1 ); // 2
        Grid c = new Grid( 0, -13 ); // 1
        Grid d = new Grid( 4, 0 ); // 4

        Grid gridArray[] = new Grid[] { a, b, c, d };
        System.out.println( "Printing non sorted:" );
        for ( Grid grid : gridArray ) {
            System.out.println( grid.getX() + ", " + grid.getY() );
        }
        System.out.println( "Sorting..." );
        Arrays.sort( gridArray );

        System.out.println( "Printing sorted:" );
        for ( Grid grid : gridArray ) {
            System.out.println( grid.getX() + ", " + grid.getY() );
        }
    }

    private int x, y;

    public Grid(int x, int y) {
        this.setX( x );
        this.setY( y );
    }

    @Override
    public int compareTo(Grid grid) {
        // ascending order
        return this.getX() - grid.getX();
        // descending order
        // return grid.getX()-this.getX();
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

}

现在 Arrays.sort 将使用自然排序(可比较),请参阅:http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort(java.lang.Object[])

这是执行给定的 main 方法后的输出:

Printing non sorted:
2, 15
1, 1
0, -13
4, 0
Sorting...
Printing sorted:
0, -13
1, 1
2, 15
4, 0
于 2013-09-24T21:02:00.723 回答