

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to declare Java array with array size dynamically?
To declare array size dynamically read the required integer value from the user using Scanner class and create an array using the given value:
Example
import java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray { public static void main(String args[]) { System.out.println("Enter the required size of the array :: "); Scanner s = new Scanner(System.in); int size = s.nextInt(); int myArray[] = new int [size]; System.out.println("Enter the elements of the array one by one "); for(int i = 0; i<size; i++) { myArray[i] = s.nextInt(); } System.out.println("Contents of the array are: "+Arrays.toString(myArray)); } }
Output
Enter the required size of the array :: 5 Enter the elements of the array one by one 78 96 45 23 45 Contents of the array are: [78, 96, 45, 23, 45]
- Related Questions & Answers
- How to declare an Array Variables in Java?
- How to add items to an array in java dynamically?
- How to declare a static String array in Java
- Declare dynamically in SAP ABAP
- How to declare an array in C#?
- how can I declare an Object Array in Java?
- How to declare, create, initialize and access an array in Java?
- How do I declare and initialize an array in Java?
- How to dynamically allocate a 2D array in C?
- How to define an array size in java without hardcoding?
- How to extend the size of an array in Java
- How to declare an empty string array in C#?
- How to declare a two-dimensional array in C#
- Declare a const array in C#
- Replace a C# array with a new array of different size
Advertisements