- 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 create a subarray from another array in Java
Use Arrays.copyOfRange() method to get a subarray.
Example
import java.util.Arrays; public class Tester { public static void main(String[] args) { int[] array = new int[] {1, 2, 3, 4, 5}; int[] subArray = Arrays.copyOfRange(array, 0, 2); System.out.println("Array: "); for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("
Sub array: "); for(int i = 0; i < subArray.length; i++) { System.out.print(subArray[i] + " "); } } }
Output
Array: 1 2 3 4 5 Sub array: 1 2
- Related Articles
- How to create an array of partial objects from another array in JavaScript?
- How to create a string from a Java Array?
- Create a new ArrayList from another collection in Java
- How to move an array element from one array position to another in Java?
- How to create an ArrayList from an Array in Java?
- How to create LabelValue Tuple from an array in Java
- How to create JTable from two dimensional array in Java?
- How can we copy one array from another in Java
- How to store a 2d Array in another 2d Array in java?
- Create Septet Tuple from another collection in Java
- Create Pair Tuple from another collection in Java
- Create Quintet Tuple from another collection in Java
- Create Triplet Tuple from another collection in Java
- Create Quartet Tuple from another collection in Java
- Create Unit Tuple from another collection in Java

Advertisements