

- 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
Create new instance of a Two-Dimensional array with Java Reflection Method
A new instance of a two dimensional array can be created using the java.lang.reflect.Array.newInstance() method. This method basically creates the two-dimensional array with the required component type as well as length.
A program that demonstrates the creation of a two-dimensional array using the Array.newInstance() method is given as follows −
Example
import java.lang.reflect.Array; public class Demo { public static void main (String args[]) { int size[] = {3, 3}; int arr[][] = (int[][])Array.newInstance(int.class, size); System.out.println("The two-dimensional array is:"); for(int[] i: arr) { for(int j: i ) { System.out.print(j + " "); } System.out.println(); } } }
Output
The two-dimensional array is: 0 0 0 0 0 0 0 0 0
Now let us understand the above program. A new instance of a two-dimensional array is created using the Array.newInstance() method. A code snippet which demonstrates this is as follows −
int size[] = {3, 3}; int arr[][] = (int[][])Array.newInstance(int.class, size);
Then the array elements are displayed using the nested for loop. A code snippet which demonstrates this is as follows −
System.out.println("The two-dimensional array is:"); for(int[] i: arr) { for(int j: i ) { System.out.print(j + " "); } System.out.println(); }
- Related Questions & Answers
- Create new instance of an Array with Java Reflection Method
- Create array with Array.newInstance with Java Reflection
- Java Program to create DefaultTableModel from two dimensional array
- How to create JTable from two dimensional array in Java?
- How to create a two dimensional array in JavaScript?
- Transpose of a two-dimensional array - JavaScript
- How do I declare a two-dimensional array in C++ using new?
- Create a two-dimensional array with the flattened input as a diagonal in Numpy
- Create a two-dimensional array with the flattened input as lower diagonal in Numpy
- Initialize an Array with Reflection Utilities in Java
- Split one-dimensional array into two-dimensional array JavaScript
- Reflection Array Class in Java
- Zip two arrays and create new array of object in a reshaped form with MongoDB
- Create a two-dimensional array with the flattened input as an upper diagonal in Numpy
- Use reflection to create, fill, and display an array in Java
Advertisements