Last active
March 2, 2016 11:45
-
-
Save bigmeech/8d3cbecf03082749b873 to your computer and use it in GitHub Desktop.
gets primary and secondary array
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
| import java.util.Arrays; | |
| class Main { | |
| private static int[][] twoDInput = { | |
| {1,2,3}, | |
| {4,5,6}, | |
| {7,8,9} | |
| }; | |
| public static void main(String[] args) { | |
| getPrimaryDiagonals(twoDInput); | |
| } | |
| public static void getPrimaryDiagonals(int[][] twoD){ | |
| int[] primaryDiagonalValues = new int[3]; | |
| int[] secondaryDiagonalValues = new int[3]; | |
| for(int i = 0;i<twoD.length;i++){ | |
| for(int j = 0;j<twoD[i].length;j++){ | |
| if(i == 0){ | |
| primaryDiagonalValues[i] = twoD[i][0]; | |
| secondaryDiagonalValues[i] = twoD[i][2]; | |
| } | |
| if(i == 1){ | |
| primaryDiagonalValues[i] = twoD[i][1]; | |
| secondaryDiagonalValues[i] = twoD[i][1]; | |
| } | |
| if(i == 2){ | |
| primaryDiagonalValues[i] = twoD[i][2]; | |
| secondaryDiagonalValues[i] = twoD[i][0]; | |
| } | |
| } | |
| } | |
| logit("Primary Diagonals: "+ Arrays.toString(primaryDiagonalValues)); | |
| logit("Secondary Diagonals: "+ Arrays.toString(secondaryDiagonalValues)); | |
| } | |
| public static void logit(String msg){ | |
| System.out.println(msg); | |
| } | |
| public static void logit(String[] msg){ | |
| System.out.println(msg); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment