- 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
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 Articles
- How to create a dynamic 2D array in Java?
- how to shuffle a 2D array in java correctly?
- How to read a 2d array from a file in java?
- Print a 2D Array or Matrix in Java
- How to sort a 2D array in TypeScript?
- How to convert a 2D array into 1D array in C#?
- How to dynamically allocate a 2D array in C?
- Peak Element in 2D array
- Find the dimensions of 2D array in Java
- How to get rows and columns of 2D array in Java?
- C++ Perform to a 2D FFT Inplace Given a Complex 2D Array
- Scatter a 2D numpy array in matplotlib
- Colorplot of 2D array in Matplotlib
- Golang program to print a 2D array?
- Swift Program to Print a 2D Array

Advertisements