- 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
Can you change size of Array in Java once created?
An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation.
The position of the elements in the array is called as index or subscript. The first element of the array is stored at the index 0 and, the second element is at the index 1 and so on.
Each element in an array is accessed using an expression which contains the name of the array followed by the index of the required element in square brackets.
For example, if an array of 6 elements is created with name myArray, you can access the element of the array at index 3 as −
System.out.println(myArray[3]); //25
Size of an array
In Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −
The size of an array is fixed, if you create an array using the new keyword you need to specify the length/size of it in the constructor as −
int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] = 5687; myArray[3] = 1457; myArray[4] = 4554; myArray[5] = 5445; myArray[6] = 7524;
You can also directly assign values within flower braces separating them with commas (,) as −
int myArray = {1254, 1458, 5687, 1457, 4554, 5445, 7524}; //size 7
If you create an array by initializing its values directly, the size will be the number of elements in it.
Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.
Still if you try to assign value to the element of the array beyond its size a run time exception will be generated.
Example
In the following example we are creating an array with 7 elements and alter we are trying to assign value to the 8th element.
public class Test { public static void main(String[] args) { int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] = 5687; myArray[3] = 1457; myArray[4] = 4554; myArray[5] = 5445; myArray[6] = 7524; myArray[7] = 4238; System.out.println(Arrays.toString(myArray)); } }
Run time error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at Test.main(Test.java:13)
Changing the size of an array
You can change the size of the existing array by reassigning it to the new one as −
Example
import java.util.Arrays; public class Test { public static void main(String[] args) { int myArray[] = new int[7]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] = 5687; myArray[3] = 1457; myArray[4] = 4554; myArray[5] = 5445; myArray[6] = 7524; System.out.println(Arrays.toString(myArray)); myArray = new int[8]; myArray[0] = 1254; myArray[1] = 1458; myArray[2] = 5687; myArray[3] = 1457; myArray[4] = 4554; myArray[5] = 5445; myArray[6] = 7524; myArray[7] = 3165; System.out.println(Arrays.toString(myArray)); } }
Output
[1254, 1458, 5687, 1457, 4554, 5445, 7524] [1254, 1458, 5687, 1457, 4554, 5445, 7524, 3165]
In this scenario the earlier array object will be left out for garbage collection. Thus, when used extensively this approach causes memory issues therefore, it is not recommended.
- Related Articles
- Can you pass the negative number as an Array size in Java?
- How to change the size of dots in dotplot created by using ggplot2 in R?
- Can you create an array of Generics type in Java?
- Can I change the size of UIActivityIndicator in Swift?
- How to change JLabel size in Java?
- How many types of JDialog boxes can be created in Java?
- Can you assign an Array of 100 elements to an array of 10 elements in Java?
- When can a .class file get created in Java?
- How to change/increase the heap size of the Java Virtual Machine in Java?
- How to change the color and size of the axes labels of a plot created by using plot function in R?
- How to extend the size of an array in Java
- How can I change the font size of ticks of axes object in Matplotlib?
- How many ways a String object can be created in java?
- Difference between length of Array and size of ArrayList in Java
- Java Program to double the size of an array
