- 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
Java Program to double the size of an array
To double the size of an array, let us first create an array −
int arr[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
Now, get the length of the above array −
int len = arr.length;
Double the size of the above array −
intnewArray[] = new int[len*2];
Now, newArray[] will be having twice the length of the array arr[].
Example
public class Demo { public static void main (String args[]) { int arr[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; System.out.println("Length of initial array = " + arr.length); int len = arr.length; int newArray[] = new int[len*2]; System.arraycopy(arr, 0, newArray, 0, len); System.out.println("Length of new array = "+newArray.length); } }
Output
Length of initial array = 10 Length of new array = 20
Advertisements
To Continue Learning Please Login
Login with Google