
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create 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("\nSub 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
Advertisements