Created
January 16, 2021 13:02
-
-
Save BT-ICD/9c2b23e08131ae1fe575bff12d1d8e17 to your computer and use it in GitHub Desktop.
getColumn particular column as one dimension integer array from two dimension integer 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
public class ArrayDemo1 { | |
public static void main(String[] args) { | |
int[][] arr2D = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 5, 3 } }; | |
int[] result = getColumn(arr2D,1); | |
System.out.println(result.length); | |
for(int data: result){ | |
System.out.println(data); | |
} | |
} | |
/** | |
* Write a static method getColumn, which returns a one-dimensional array containing the elements of a single column in a two-dimensional array. | |
* The elements in the returned array should be in the same order as they appear in the given column. | |
* The notation arr2D[r][c]represents the array element at row r and column c | |
* */ | |
static int[] getColumn(int [][] arr2D, int columnIndex){ | |
int length = arr2D.length; | |
System.out.println("length of array is :" + length); | |
int[] result = new int[length]; | |
for (int i = 0; i <arr2D.length ; i++) { | |
result[i]= arr2D[i][columnIndex]; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment