Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7be367c

Browse files
Array utils
1 parent 7a86a91 commit 7be367c

File tree

3 files changed

+172
-14
lines changed

3 files changed

+172
-14
lines changed

‎src/com/deepak/data/structures/Utils/ArrayUtils.java

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package com.deepak.data.structures.Utils;
66

7+
import java.lang.reflect.Array;
8+
79
/**
810
* Utilities for Arrays
911
*
@@ -18,24 +20,104 @@ public class ArrayUtils {
1820
* @return
1921
*/
2022
public static <T> boolean isEmpty(T[] array) {
21-
return false; // TODO
23+
return array == null || array.length == 0;
24+
}
25+
26+
/**
27+
* Method to convert array to string
28+
*
29+
* @param array
30+
* @return {@link String}
31+
*/
32+
public static String toString(String[] array) {
33+
return String.join(",", array);
34+
}
35+
36+
/**
37+
* Method to convert stream of items to array
38+
*
39+
* @param items
40+
* @return {@link T[]}
41+
*/
42+
@SafeVarargs
43+
public static <T> T[] toArray(final T... items) {
44+
return items;
45+
}
46+
47+
/**
48+
* Method to clone a array
49+
*
50+
* @param array
51+
* @return {@link T[]}
52+
*/
53+
public static <T> T[] clone(final T[] array) {
54+
if (array == null) {
55+
return null;
56+
}
57+
return array.clone();
58+
}
59+
60+
/**
61+
* Method to check if two arrays are of same length
62+
*
63+
* @param array1
64+
* @param array2
65+
* @return {@link boolean}
66+
*/
67+
public static boolean isSameLength(final Object[] array1, final Object[] array2) {
68+
return getLength(array1) == getLength(array2);
69+
}
70+
71+
/**
72+
* Method to get length of the array
73+
*
74+
* @param array
75+
* @return {@link int}
76+
*/
77+
public static int getLength(final Object array) {
78+
if (array == null) {
79+
return 0;
80+
}
81+
return Array.getLength(array);
2282
}
2383

84+
/**
85+
* Method to check if two arrays are of same type
86+
*
87+
* @param array1
88+
* @param array2
89+
* @return {@link boolean}
90+
*/
91+
public static boolean isSameType(final Object array1, final Object array2) {
92+
if (array1 == null || array2 == null) {
93+
throw new IllegalArgumentException("The Array must not be null");
94+
}
95+
return array1.getClass().getName().equals(array2.getClass().getName());
96+
}
97+
98+
/**
99+
* Method to do a binary search on int array
100+
*
101+
* @param array
102+
* @param size
103+
* @param value
104+
* @return {@link int}
105+
*/
24106
public static int binarySearch(int[] array, int size, int value) {
25107
int lo = 0;
26-
int hi = size - 1;
27-
while (lo <= hi) {
28-
final int mid = (lo + hi) >>> 1;
29-
final int midVal = array[mid];
30-
if (midVal < value) {
31-
lo = mid + 1;
32-
} else if (midVal > value) {
33-
hi = mid - 1;
34-
} else {
35-
return mid;// value found
36-
}
37-
}
38-
return ~lo;// value not present
108+
int hi = size - 1;
109+
while (lo <= hi) {
110+
final int mid = (lo + hi) >>> 1;
111+
final int midVal = array[mid];
112+
if (midVal < value) {
113+
lo = mid + 1;
114+
} else if (midVal > value) {
115+
hi = mid - 1;
116+
} else {
117+
return mid;
118+
}
119+
}
120+
return ~lo;
39121
}
40122

41123
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* ArrayUtilsTest.java
4+
*/
5+
package com.deepak.data.structures.Utils;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test cases for array utilities
12+
*
13+
* @author Deepak
14+
*/
15+
public class ArrayUtilsTest {
16+
17+
/**
18+
* Test case to check if array is empty
19+
*/
20+
@Test
21+
public void testIsEmpty() {
22+
String[] strArray = {};
23+
Assert.assertTrue(ArrayUtils.isEmpty(strArray));
24+
Integer[] intArray = {1, 2};
25+
Assert.assertFalse(ArrayUtils.isEmpty(intArray));
26+
}
27+
28+
/**
29+
* Test case to check toString for array
30+
*/
31+
@Test
32+
public void testToString() {
33+
String[] strArray = {"Apple", "Ball", "Cat", "Dog"};
34+
Assert.assertEquals(ArrayUtils.toString(strArray), "Apple,Ball,Cat,Dog");
35+
}
36+
37+
/**
38+
* Method to test cloning of array
39+
*/
40+
@Test
41+
public void testClone() {
42+
String[] strArray = {"Apple", "Ball", "Cat", "Dog"};
43+
String[] newArray = ArrayUtils.clone(strArray);
44+
Assert.assertTrue(strArray.length == newArray.length);
45+
}
46+
47+
/**
48+
* Method to test if two arrays are of same length
49+
*/
50+
@Test
51+
public void testIsSameLength() {
52+
String[] array1 = {"A", "B", "C"};
53+
String[] array2 = {"X", "Y", "Z"};
54+
Assert.assertTrue(ArrayUtils.isSameLength(array1, array2));
55+
String[] array3 = {"A", "B", "C"};
56+
String[] array4 = {"X", "Y"};
57+
Assert.assertFalse(ArrayUtils.isSameLength(array3, array4));
58+
}
59+
60+
/**
61+
* Method to test if two arrays are of same type
62+
*/
63+
public void testIsSameType() {
64+
String[] array1 = {"A", "B", "C"};
65+
String[] array2 = {"X", "Y", "Z"};
66+
Assert.assertTrue(ArrayUtils.isSameType(array1, array2));
67+
Integer[] array3 = {1, 2, 3};
68+
Assert.assertFalse(ArrayUtils.isSameType(array1, array3));
69+
}
70+
71+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.deepak.data.structures.Utils;
2+
3+
public class CollectionUtilsTest {
4+
5+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /