Należy pamiętać o tym, że tablica 2 wymiarowa != nie oznacza prostokątnej tablicy. Przykładowo:
1, 2
3, 5, 7,
1
Można więc powiedzieć, że jest to lista tablic 1 wymiarowych:
int[] t1 = {1,2};
int[] t2 = {3, 4, 5};
int[] t3 = {1}
Jeżeli chcesz wyciągnąć pojedynczy wiersz możesz zatem zrobić coś takiego:
Integer[][] tablica = new Integer[][] {
{1, 2},
{4, 5, 6}
};
Integer[] row1 = tablica[1];
W tym przypadku row1 = {4,5,6}
i wystarczy posortować tą tablicę 1 wymiarową.
Podam Ci przykład sortowania wybranego wiersza, kolumny i całej tablicy:
package net.forum4programmers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TableSorter {
/**
* Sortowanie całej tablicy.
*
* @param array sortowana tablica
*/
@SuppressWarnings("unchecked")
public void sort(final Object[][] array) {
this.assertNotNull(array, "Przekazana tablica nie może być nullem");
final List tmpList = this.createList(array);
Collections.sort(tmpList);
int index = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = tmpList.get(index);
index ++;
}
}
}
/**
* Sortuje wybrany wiersz tablicy.
*
* @param array tablica
* @param rowIndex nr wybranego wiersza
*/
public void sortRow(final Object[][] array, final int rowIndex) {
this.assertNotNull(array, "Przekazana tablica nie może być nullem");
this.assertBetween(rowIndex, 0, array.length - 1, "Nieprawidłowy " +
"numer wiersza " + rowIndex);
Arrays.sort(array[rowIndex]);
}
private List<Object> createList(final Object[][] array) {
final List<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
list.add(array[i][j]);
}
}
return list;
}
/**
* Sortuje wybraną kolunnę tablicy.
*
* @param array tablica
* @param columnIndex nr kolumny
*/
@SuppressWarnings("unchecked")
public void sortColumn(final Object[][] array, final int columnIndex) {
this.assertNotNull(array, "Przekazana tablica nie może być nullem");
final List column = this.createColumnList(array, columnIndex);
Collections.sort(column);
int index = 0;
for (int i = 0; i < array.length; i++) {
if (this.hasColumn(array[i], columnIndex)) {
array[i][columnIndex] = column.get(index);
index ++;
}
}
}
private List<Object> createColumnList(final Object[][] array,
final int columnIndex) {
final List<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.length; i++) {
if (this.hasColumn(array[i], columnIndex)) {
list.add(array[i][columnIndex]);
}
}
return list;
}
private boolean hasColumn(final Object[] array, final int columnIndex) {
return array != null && columnIndex >= 0 && columnIndex < array.length;
}
private void assertNotNull(final Object o, final String message) {
if (o == null) {
throw new IllegalArgumentException(message);
}
}
private void assertBetween(final double value, final double min,
final double max, final String message) {
if (value < min || value > max) {
throw new IllegalArgumentException(message);
}
}
/**
* Drukuje tablicę w konsoli.
*
* @param array tablica
*/
public static void print(final Object[][] array) {
if (array == null) {
return;
}
System.out.println("------------------------------------------------");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
System.out.println("------------------------------------------------");
}
/**
* Main dla testów.
*
* @param args argumenty
*/
public static void main(final String[] args) {
final TableSorter sorter = new TableSorter();
// tworze testową tablicę
final Integer[][] array = new Integer[][] {
{1, 2},
{4, 2, 1, 5, 10},
{100, 0, 44, 1},
{5, 9, 8},
{7},
};
System.out.println("TABLICA:");
print(array);
System.out.println("Sortuje kolumne 3");
sorter.sortColumn(array, 2);
print(array);
// sortuje 1 i 2 wiersz
sorter.sortRow(array, 1);
sorter.sortRow(array, 2);
System.out.println("Po posortowaniu wiersz 2 i 3");
print(array);
System.out.println("Sortuje całość");
sorter.sort(array);
print(array);
}
}
GL & HF.