

- 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 store a 2d Array in another 2d Array in java?
Create an array to which you want to store the existing array with the same length. A 2d array is an array of one dimensional arrays therefore, to copy (or, to perform any operation on) the elements of the 2d array you need two loops one nested within the other. Where, the outer loop is to traverse through the array of one dimensional arrays and, the inner loop is to traverse through the elements of a particular one dimensional array.
Example
public class Copying2DArray { public static void main(String args[]) { int[][] myArray = {{41, 52, 63}, {74, 85, 96}, {93, 82, 71} }; int[][] copyArray =new int[myArray.length][]; for (int i = 0; i < copyArray.length; ++i) { copyArray[i] = new int[myArray[i].length]; for (int j = 0; j < copyArray[i].length; ++j) { copyArray[i][j] = myArray[i][j]; } } System.out.println(Arrays.deepToString(copyArray)); } }
Output
[[41, 52, 63], [74, 85, 96], [93, 82, 71]]
- Related Questions & Answers
- How to create a dynamic 2D array in Java?
- how to shuffle a 2D array in java correctly?
- Print a 2D Array or Matrix in Java
- How to read a 2d array from a file in java?
- Peak Element in 2D array
- C++ Perform to a 2D FFT Inplace Given a Complex 2D Array
- How to convert a 2D array into 1D array in C#?
- How to dynamically allocate a 2D array in C?
- Scatter a 2D numpy array in matplotlib
- Find the dimensions of 2D array in Java
- Colorplot of 2D array in Matplotlib
- Passing a 2D array to a C++ function
- Flatten a 2d numpy array into 1d array in Python
- How to get rows and columns of 2D array in Java?
- How to pass a 2D array as a parameter in C?
Advertisements