Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Multi-Dimensional Arrays

I'm currently doing some online classes to learn Java. I feel like the book I'm reading has a ton of examples without enough explanations. Right now I'm trying to understand arrays and multidimensional arrays. Right now there's a discussions that came up in the class. It assumes the following:

PHP Code

<?php 
int
[][][] x = new int[12][5][2] 

How many elements are in the array?
What is x.length?
What is x[2].length?
What is x[0][0].length?


Now, I know there are 120 elements in the array because 12 5 2 = 120.
However, I'm not understanding why the .length gives me the following numbers: 12, 5, 2.
I feel like I could have guessed the answers and got it correctly. But guessing doesn't quite help me understand arrays. In order for me to get the answers, I just created a class in eclipse to print the length.

Can someone provide some more insight into multi-dimensional arrays?

PHP Code

<?php 
public class multidim {

    public static 
void main(String[] args) {
        
        
int[][][] x = new int[12][5][2];

        
System.out.println(x.length);
        
System.out.println(x[2].length);
        
System.out.println(x[0][0].length);

    }

} 

[Image: E0PaFmi.gif]
Quick Love for Quick Love
You are simply accessing the length of each different dimension in your 3d array.

1d Array = 1 row, several columns
2d Array = several rows, several columns. So picture a 2d table
3d Array = several 2d tables (picture them in-line behind one another)

Keep in mind you have initialised the 3d array so that each row is the same length, in Java the rows can all be different lengths.


I'd check the 3d array section of this article, might explain it better but you need to see it visually and it might help you to understand.

https //www geeksforgeeks org/multidimensional-arrays-in-java/
(Mar 15, 2019 08:33 PM)k45h Wrote:  You are simply accessing the length of each different dimension in your 3d array.

1d Array = 1 row, several columns
2d Array = several rows, several columns. So picture a 2d table
3d Array = several 2d tables (picture them in-line behind one another)

Keep in mind you have initialised the 3d array so that each row is the same length, in Java the rows can all be different lengths.


I'd check the 3d array section of this article, might explain it better but you need to see it visually and it might help you to understand.

https //www geeksforgeeks org/multidimensional-arrays-in-java/

This is perfect. THank you!

[Image: E0PaFmi.gif]
Quick Love for Quick Love
When you try to conceptualize multidimensional arrays it's quite useful to think of them like you would a coordinate system.
1D: Just a line of numbers like a list, 1 row, multiple columns.
2D: Think of a chess board or a coordinate system: multiple rows, multiple columns which can be addressed with (x/y) coordinates
3D: Like a 3d coordinate system (a cube for example) which can address with (x/y/z) coordinates
[Image: 0DvFxnA.gif]
Donate: 3ABKRghau93ziRaDfVwiiwtjky3i1gLrKn


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by pzychonautJun 8, 2023 05:03 PM 5 178 Jun 8, 2023 05:03 PM
Last Post: padrino
[C] by EmacsSucksButILikeItJun 4, 2022 11:03 PM 1 222 Jun 4, 2022 11:03 PM
Last Post: unazed
[Java] by PsychoJul 3, 2021 11:15 PM 24 1,975 Jul 3, 2021 11:15 PM
Last Post: Lookdadon