1

what is the difference between a 2D and a 3D array, from my understanding

one dimensional array

enter image description here

two dimensional array

enter image description here

but what i dont undestand is, what is a 3D array and how is it represented, if you are taking initiative ti explain me suing any particular language then please use python.

2 Answers 2

8

A one dimensional array is an array for which you have to give a single argument (called index) to access a specific value.

E.G. with the following one dimensional array

array = [0,1,2,9,6,5,8]

The array at index 1 has the value 1. The array at index 3 has value 9. If you want to update the 3rd value to 8 in the array, you should do

array[2] = 8

A two-dimensional array is simply an array of arrays. So, you have to give two arguments to access a single value.

two_dim_array = [[1,2,3],[4,5,6],[7,8,9]]

If you want to update the 'second' value, you have to do

two_dim_array[0][1] = 'something'

That is because two_dim_array[0] is a one-dimensional array, and you still have to specify an index to access a value.

From now on, you can keep going deeper with the same reasoning. As any further dimension is another level in the list. So a three dimensional array would be :

3d_array = 
[
    [
        [1,2,3,4],
        [5,6,7,8]
    ],
    [
        [9,10,11,12],
        [13,14,15,16]
    ]
]

Now to access a value you have to give .. 3 parameters. Because

3d_array[0] // is a two-dim array
3d_array[0][1] // is a one-dim array
3d_array[0][1][0] // is a value

I suggest you start doing simple exercices to get you familiar with this concept, as it is really 101 programming stuff. W3resource has great exercices to get you started.

3
  • then, what is a 3D array? could you please explain me, please? Oct 25, 2019 at 8:47
  • so, can we say that a 3D array is the array of 2D array? is my understanding right? Oct 25, 2019 at 8:54
  • Yes, a 3D array is an array of 2D arrays. A 4D array is an array of 3D arrays, etc. Oct 25, 2019 at 8:54
1

To declare a two-dimensional array, you simply list two sets of empty brackets, like this:

int numbers[][];

Here, numbers is a two-dimensional array of type int. To put it another way, numbers is an array of int arrays.

Often, nested for loops are used to process the elements of a two-dimensional array, as in this example:

for (int x = 0; x < 10; x++) 
{
    for (int y = 0; y < 10; y++) 
    {
        numbers[x][y] = (int)(Math.random() * 100) + 1
    }
}

To declare an array with more than two dimensions, you just specify as many sets of empty brackets as you need. For example:

int[][][] threeD = new int[3][3][3];

Here, a three-dimensional array is created, with each dimension having three elements. You can think of this array as a cube. Each element requires three indexes to Access.

You can nest initializers as deep as necessary, too. For example:

int[][][] threeD = 
    {  { {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, 27} } };
3
  • so, for four dimension array, can i declare the variable like this int[][][] fourD = new int[3][3][4]; int[][][] threeD = { { {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, 27} }, { {1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} }, }; Oct 25, 2019 at 8:44
  • No, for a four dimension array, you Need int[][][][] fourD = new int [4][4][4][4] . The number in [x] only make the size. Fout Dimentional Array are hard to imagine. Oct 25, 2019 at 8:58
  • U get it now ? No worries. Best regards. Oct 25, 2019 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.