0

Is it possible to put an array inside of a two dimensional array? For explanation: I need a two-dimensional byte array, but I also need three byte values for each cell. It could look like this:

+-------------+-------------+
| 123,234,125 | 255,109,167 |
+-------------+-------------+
| 172,144,134 | 145,212,124 |
+-------------+-------------+

Or in a more appropriate format:

[ [ [ 123,234,125 ] , [ 255,109,167 ] ] , [ [ 172,144,134 ] , [ 145,212,124 ] ] ]

This would be a two-dimensional array containing arrays of three bytes. Is this possible in Java?

3

5 Answers 5

3

Totally possible.

Here's an example:

byte[][][] myAwesomeByteArray = 
            { 
                { 
                    { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } 
                },
                { 
                    { 9, 10, 11 }, { 12, 13, 14 }, { 15, 16, 17 } 
                },
                { 
                    { 18, 19, 20 }, { 21, 22, 23 }, { 24, 25, 26 } 
                } 
            };
System.out.println(Arrays.deepToString(myAwesomeByteArray));

Output:

[[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]
Sign up to request clarification or add additional context in comments.

1 Comment

@HussainAkhtarWahid :D
2

Yes, of course this is possible. You can either use a 3D array:

byte[][][] array = new byte[w][h][3];
array[0][0][0] = 123;
array[0][0][1] = 234;
array[0][0][2] = 125;

Or use a 2D array of ints. ints are 4 bytes, which is enough for your requirements:

int[][] array = new int[w][h];
array[0][0] = (123 << 16) | (234 << 8) | (125);

Comments

2

There are two basic ways to do what you are trying to do. Here are two examples to give a try to.

int [][][] firstArray = new int[][][] {{{ 123,234,125 } , { 255,109,167 }} , {{ 172,144,134 } , { 145,212,124 }}};

int [][][] secondArray = new int[2][2][3];

The first sample assumes that you know exactly what the content is for the arrays and the second gives you a bit more flexibility in stuffing the data.

Hope it helps, let me know if you need more.

Comments

0

You can try this code to work but with few modifications you can change this according to your requirement. All the values inserted here is static, you can make this to dynamic & insert. Change int to byte as per your requirement.

public static void main(String args[])
{
    /*Data Insertion*/
    int a[]=new int[3];
    a[0]=123;
    a[1]=234;
    a[2]=125;
    int a1[]=new int[3];
    a1[0]=255;
    a1[1]=109;
    a1[2]=167;
    int a2[]=new int[3];
    a2[0]=172;
    a2[1]=144;
    a2[2]=134;
    int a3[]=new int[3];
    a3[0]=145;
    a3[1]=212;
    a3[2]=124;
    Object s[][]=new Object[2][2];
    s[0][0]=a;
    s[0][1]=a1;
    s[1][0]=a2;
    s[1][1]=a3;
    /*Data Retrieval*/
    for(int i=0;i<s.length;i++)
    {
        for(int j=0;j<2;j++)
        {
            int sg[]=(int[])s[i][j];       
            System.out.println("Value: "+sg[0]+" "+sg[1]+" "+sg[2]);
        }
    }
}

Comments

0

As others pointed out, it is possible.

When doubting, think other way: is there a reason this should not be possible? In mathematics and programming, it generally goes this way: whatever is not expressly forbidden, is allowed.

An array can contain any type elements. So why not arrays?

Now consider the arrays within an array: what type are their elements? See previous paragraph, and then go as far/deep as you want, as it states nowhere how far/deep you are allowed to go. Apart from issues like memory constraints...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.