- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 Map multi-dimensional arrays to a single array in java?
A two-dimensional array is nothing but an array of one dimensional arrays. Therefore to map a two dimensional array into one dimensional arrays.
Create arrays equal to the length of the 2d array and, using for loop store the contents of the 2d array row by row in the arrays created above.
Example
public class Mapping_2DTo1D { public static void main(String args[]) { int [][] array2D = {{7, 9, 8, 5}, {4, 5, 1, 8}, {9, 3, 2, 7}, {8, 1, 0, 9}}; int [] myArray1 = new int[array2D[0].length]; int [] myArray2 = new int[array2D[0].length]; int [] myArray3 = new int[array2D[0].length]; int [] myArray4 = new int[array2D[0].length]; for (int i = 0; i < array2D[0].length; ++i) { myArray1[i] = array2D[0][i]; myArray2[i] = array2D[1][i]; myArray3[i] = array2D[2][i]; myArray4[i] = array2D[3][i]; } System.out.println(Arrays.deepToString(array2D)); System.out.println(Arrays.toString(myArray1)); System.out.println(Arrays.toString(myArray2)); System.out.println(Arrays.toString(myArray3)); System.out.println(Arrays.toString(myArray4)); } }
Output
[[7, 9, 8, 5], [4, 5, 1, 8], [9, 3, 2, 7], [8, 1, 0, 9]] [7, 9, 8, 5] [4, 5, 1, 8] [9, 3, 2, 7] [8, 1, 0, 9]
- Related Articles
- Java Program to convert array to String for one dimensional and multi-dimensional arrays
- Dump Multi-Dimensional arrays in Java
- How to initialize multi-dimensional arrays in C#?
- How to define multi-dimensional arrays in C#?
- Does Java support multi-dimensional Arrays?
- Java Program to Multiply to Matrix Using Multi-Dimensional Arrays
- How to define multi-dimensional arrays in C/C++?
- Java Program to Add Two Matrix Using Multi-Dimensional Arrays
- Multi Dimensional Arrays in Javascript
- Single dimensional array in Java
- Flattening multi-dimensional arrays in JavaScript
- Sort in multi-dimensional arrays in JavaScript
- Golang Program to Multiply to Matrix Using Multi-Dimensional Arrays
- How do we use multi-dimensional arrays in C#?
- How to access elements from multi-dimensional array in C#?

Advertisements