Skip to content

Instantly share code, notes, and snippets.

@hamedrnik
Last active May 23, 2016 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamedrnik/993eaba7760e89f6dc2219149a5cb1f3 to your computer and use it in GitHub Desktop.
Save hamedrnik/993eaba7760e89f6dc2219149a5cb1f3 to your computer and use it in GitHub Desktop.
Convert 1D array into 3D
package com.example.converter;
import java.util.Arrays;
import java.util.regex.Matcher;
public class tset39 {
public static void main(String[] args) {
int width = 2;
int height = 2;
int depth = 2;
int[] rr1 = {1, 2, 3, 4, 5, 6, 7, 8};
System.out.println(Arrays.deepToString(tset39.arrst(rr1, width, height, depth)));
}
public static int[][][] arrst(int[] a, int width, int height, int depth)
{
int array3[][][] = new int[width][height][depth];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int z = 0; z < depth; z++) {
array3[x][y][z] = a[height * depth * x + depth * y + z];
}
}
}
return array3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment