How to Split an Array in Java

Learn to split an array in Java using different ways. We will learn to split the array into equal parts, at the specified index and of equal lengths.

1. Arrays.copyOfRange() API

The copyOfRange() creates a new array of the same type as the original array, and contains the items of the specified range of the original array into a new array. Note that this method internally uses System.arraycopy() to copy the array items.

public static T[] copyOfRange(T[] original, int from, int to)

These are the method parameters.

  • original – the array from which a range is to be copied
  • from – the initial index of the range to be copied, inclusive
  • to – the final index of the range to be copied, exclusive. (This index may lie outside the array.)

An important point to note is that to index may lie outside the length of the array. Such index locations are filled with the default value of the type of array.

For example, for int, long and double types, the additional indices will be filled with zeros. For a boolean array, such indices will be filled with false and for object arrays, such positions will be filled with null.

It will throw IllegalArgumentException if from is bigger than to.

2. Splitting Array at Specified Index

Let’s say we are dividing an array in such a way that we should get two arrays of defined lengths. In such a case, we must use the copyOfRange() API to create two new arrays from the original array.

The first new array will be having the items starting from zero to the specified index, and the second new array will have items from the specified index to the end of the original array.

int[] original = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int givenIndex = 3;

splitArrayPart1 = Arrays.copyOfRange(original, 0, givenIndex);
splitArrayPart2 = Arrays.copyOfRange(original, givenIndex, original.length);

System.out.println(Arrays.toString(splitArrayPart1));  //[0, 1, 2]
System.out.println(Arrays.toString(splitArrayPart2));  //[3, 4, 5, 6, 7, 8, 9]

2. Splitting Array in Two Equal Parts

Splitting the array in half is very much similar to the first example. We only have to find the split position ourselves and that is the middle of the array.

int[] original = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int splitSize = original.length / 2;

int[] splitArrayPart1 = Arrays.copyOfRange(original, 0, splitSize);
int[] splitArrayPart2 = Arrays.copyOfRange(original, splitSize, original.length);

System.out.println(Arrays.toString(splitArrayPart1));  //[0, 1, 2, 3, 4]
System.out.println(Arrays.toString(splitArrayPart2));  //[5, 6, 7, 8, 9]

3. Splitting Array into N Parts

This is a bit tricky. Here we have to iterate over the array length but in chunks of a specified number. Then we have to use copyOfRange() method to create new array instances from those copied items.

We must keep special attention if there are remaining items after splitting the array equally. We need to create a new array of these remainder items.

For example, our original array contains 10 items. If we try to split the array in such a way that any new array must not contain more than 3 items. So in this case, there will be 4 arrays after the splitting procedure. 3 Arrays will have 3 items each, and 4th array will have only one item.

The given below is a method that does all the work described above.

public static <T extends Object> List<T[]> splitArray(T[] array, int splitSize) {

	int numberOfArrays = array.length / splitSize;
	int remainder = array.length % splitSize;

	int start = 0;
	int end = 0;

	List<T[]> list = new ArrayList<T[]>();
	for (int i = 0; i < numberOfArrays; i++) {
	  end += splitSize;
	  list.add(Arrays.copyOfRange(array, start, end));
	  start = end;
	}

	if(remainder > 0) {
	  list.add(Arrays.copyOfRange(array, start, (start + remainder)));
	}
	return list;
}

Let us test this method with our original array and divide such that there must be at most 3 items in an array.

List<Integer[]> arrayParts = splitArray(ArrayUtils.toObject(original), 3);

for(Integer[] o: arrayParts) {
  System.out.println(Arrays.toString(o));
}

//Output

[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]

4. Conclusion

In this tutorial, we learned to split an array in Java for different usecases. We learned to use the Arrays.copyOfRange() API to split the arrays into any number of parts.

There are other ways for array splitting as well, such that converting the array to List and the split the list. Such methods create unnecessary temporary variables without giving any clear advantage.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode