Created
September 1, 2015 18:47
-
-
Save jweinst1/5e5255b1b16d863598a3 to your computer and use it in GitHub Desktop.
returns a slice of an array in Java
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 { | |
| public static void main(String[] args) { | |
| System.out.println(Arrays.toString(slicearray(lst, 3, 6))); | |
| } | |
| static int[] lst = {1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
| static int[] slicearray(int[] x, int start, int end) { | |
| int size = (end - start) + 1; | |
| int[] slice = new int[size]; | |
| int indexer = 0; | |
| for(int i = start; i<=end; i++) { | |
| slice[indexer] = x[i]; | |
| indexer ++; | |
| } | |
| return slice; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment