- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Get a slice of a primitive array in Java?
You can get a part of a Java array in between two specified indexes in various ways.
By Copying contents:
One way to do so is to create an empty array and copy the contents of the original array from the start index to the endIndex.
Example
import java.util.Arrays; public class SlicingAnArray { public static int[] sliceArray(int array[], int startIndex, int endIndex ){ int size = endIndex-startIndex; int part[] = new int[size]; //Copying the contents of the array for(int i=0; i<part.length; i++){ part[i] = array[startIndex+i]; } return part; } public static void main(String args[]){ int intArray[] = {12, 14, 58, 225, 56, 96 , 3, 45, 8 }; intArray = sliceArray(intArray, 3, 7); System.out.println(Arrays.toString(intArray)); } }
Output
[225, 56, 96, 3]
Using the copyOfRange() method
The copyOfRange() method of the java.util.Arrays class accepts an array, two integers representing start and end indexes and returns a slice of the given array which is in between the specified indexes.
Example
Live Demo
import java.util.Arrays; public class SlicingAnArray { public static void main(String args[]){ int intArray[] = {12, 14, 58, 225, 56, 96 , 3, 45, 8 }; intArray = Arrays.copyOfRange(intArray, 3, 7); System.out.println(Arrays.toString(intArray)); } }
Output
[225, 56, 96, 3]
Using Java8 stream
Live Demo
import java.util.Arrays; import java.util.stream.IntStream; public class SlicingAnArray { public static int[] sliceArray(int array[], int startIndex, int endIndex ){ int size = endIndex-startIndex; int part[] = new int[size]; IntStream stream = IntStream.range(startIndex, endIndex).map(i->array[i]); part = stream.toArray(); //Copying the contents of the array for(int i=0; i<part.length; i++){ part[i] = array[startIndex+i]; } return part; } public static void main(String args[]){ int intArray[] = {12, 14, 58, 225, 56, 96 , 3, 45, 8 }; intArray = sliceArray(intArray, 3, 7); System.out.println(Arrays.toString(intArray)); } }
Output
[225, 56, 96, 3]
- Related Articles
- How to get the size of an array or a slice in Golang?
- Get the name of a primitive type in Java
- Is array a primitive data type in Java?
- How to detect duplicate values in primitive Java array?
- How to use $slice operator to get last element of array in MongoDB?
- How to convert Wrapper value array list into primitive array in Java?
- How to convert an array of objects to an array of their primitive types in java?
- MongoDB aggregate $slice to get the length of the array
- Aggregate a $slice to get an element in exact position from a nested array in MongoDB?
- Program to convert Primitive Array to Stream in Java
- How to get the Checksum of a Byte Array in Java?
- Is an array a primitive type or an object in Java?
- How to get the primitive value of string in Javascript?
- What would getPackage() return for a primitive or array in unnamed package in Java?
- Java Program to wrap a Primitive DataType in a Wrapper Object

Advertisements