-
-
Save omatt/edac55b617ecc60c3229 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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