-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStringArrayUtils.java
More file actions
53 lines (50 loc) · 2.01 KB
/
StringArrayUtils.java
File metadata and controls
53 lines (50 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.dtcc.exams.arrays;
public class StringArrayUtils {
/**
* @param arrayToBeSpliced - array to be evaluated
* @param startingIndex - starting index of array to be spliced
* @param endingIndex - ending index of array
* @return an array with all elements between `startingIndex` and `endingIndex`
*/
public static String[] getSubArray(String[] arrayToBeSpliced, int startingIndex, int endingIndex) {
int index = 0;
String[] splicedArray;
if(startingIndex >= 0 && endingIndex >= 0 && startingIndex < arrayToBeSpliced.length && endingIndex < arrayToBeSpliced.length){
splicedArray = new String[endingIndex-startingIndex];
for (int i = startingIndex; i < endingIndex; i++) {
splicedArray[index] = arrayToBeSpliced[i];
index++;
}
}
else if(startingIndex > arrayToBeSpliced.length && endingIndex > arrayToBeSpliced.length){
throw new IndexOutOfBoundsException();
}
else{
throw new IllegalArgumentException();
}
return splicedArray;
}
/**
* @param arrayToBeSpliced - array to be evaluated
* @param startingIndex - starting index of array to be spliced
* @return an array all elements between after `startingIndex`
*/
public static String[] getEndingArray(String[] arrayToBeSpliced, int startingIndex) {
int index = 0;
String[] splicedArray;
if(startingIndex >= 0 && startingIndex < arrayToBeSpliced.length){
splicedArray = new String[arrayToBeSpliced.length-startingIndex];
for (int i = startingIndex; i < arrayToBeSpliced.length; i++) {
splicedArray[index] = arrayToBeSpliced[i];
index++;
}
}
else if(startingIndex > arrayToBeSpliced.length){
throw new IllegalArgumentException();
}
else{
throw new IndexOutOfBoundsException();
}
return splicedArray;
}
}