Skip to content

Instantly share code, notes, and snippets.

@omatt
Last active August 29, 2015 14:15
Show Gist options
  • Save omatt/edac55b617ecc60c3229 to your computer and use it in GitHub Desktop.
Save omatt/edac55b617ecc60c3229 to your computer and use it in GitHub Desktop.
/**
* Sort a 2d array
*
* @author Omatt
* @version 2015/02/22
*/
public class Sort{
private static final int maxRow = 5, maxCol = 5;
private static String cube[][] = new String[maxRow][maxCol];
public static void main(String[] args){
initCube();
sortArray();
}
public static void initCube(){
for(int row = 0; row < maxRow; row++){
for(int col = 0; col < maxCol; col++){
if(col == row){
cube[row][col] = "x";
} else if(col > row){
cube[row][col] = "o";
} else{
cube[row][col] = "*";
}
}
}
printArray();
}
public static void sortArray(){
System.out.println("Sorting array...\n");
String temp;
int y = 0;
for(int row = 0; row < maxRow; row++){
for(int col = y; col < maxCol; col++){
temp = cube[row][col];
cube[row][col] = cube[col][row];
cube[col][row] = temp;
}
y++;
}
printArray();
}
public static void printArray(){
System.out.println("Print 2d array");
for(int row = 0; row < maxRow; row++){
for(int col = 0; col < maxCol; col++){
System.out.print(cube[row][col]);
}
System.out.print("\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment