# Quick Question - Multi Dimensional Arrays

## Recommended Posts

Sorry if this is the wrong place to put this I couldn't really find any other suitable area.  I tried posting this question on another site but it was ignored, probably because it's a dumb question to someone who knows what they're talking about.

Any ways, I've gotten stuck in my Intro level Java class, we are going over arrays and we just started on Multi dimensional arrays.

I fucking hate multi dimensional arrays.

So my question is: Are they used very often in scripting?  I'm aware single-dimensional arrays are, but anything past that?

##### Share on other sites

Not used particularly often in high-level scripts I reckon;

But its used on occasion in games and graphical/mathematical programs I suppose

##### Share on other sites

You don't have to use them, if you don't want to.  I just don't understand how understanding multi-dimensional arrays would be harder than single-dimensional.

##### Share on other sites

Multi dimmensional arrays are using for storing data in data.

For example:

Imagine if you wanted data for an x y coordinate in a game, you could have:

mapData[x][y]

int[][] multi = new int[][] {

{1,2,3,4}

{5,6,7,8}

{9,10,11,12}

};

row 0 or multi[0] = a single array of int[]{1,2,3,4}

and row 0 column 2 would equal 3, multi[0][2] = 3 ( int[]{1,2,3,4} )

So really multidimmensional arrays are an array of arrays, if that makes sense .

As for scripting, unless you plan on making collision path detection(finding a path to some position and consider collisions when determining the path) or something similar(which I think OSBot 2 will provide anyways), you most likely won't use it.

Enums would be more efficient in storing data for a particular script(for example, agility obstacles in an agility script, or potion creation data in a herblore script).

If I went wrong anywhere, please someone correct me ;P.

Edited by Allen
##### Share on other sites

Multi dimmensional arrays are using for storing data in data.

For example:

Imagine if you wanted data for an x y coordinate in a game, you could have:

mapData[x][y]

int[][] multi = new int[][] {

{1,2,3,4}

{5,6,7,8}

{9,10,11,12}

};

row 0 or multi[0] = a single array of int[]{1,2,3,4}

and row 0 column 2 would equal 3, multi[0][2] = 3 ( int[]{1,2,3,4} )

So really multidimmensional arrays are an array of arrays, if that makes sense .

As for scripting, unless you plan on making collision path detection(finding a path to some position and consider collisions when determining the path) or something similar(which I think OSBot 2 will provide anyways), you most likely won't use it.

Enums would be more efficient in storing data for a particular script(for example, agility obstacles in an agility script, or potion creation data in a herblore script).

If I went wrong anywhere, please someone correct me ;P.

Actually this helped a LOT.  I think my teacher was making it seem harder than it was, because explained like this it makes perfect sense.

I really appreciate the help!

You don't have to use them, if you don't want to.  I just don't understand how understanding multi-dimensional arrays would be harder than single-dimensional.

I just got a little tripped up with the examples I was being shown in class.

• 1
##### Share on other sites

most scripts won't require them but it's good to know them if you want to write custom stuff such as jtable models etc

##### Share on other sites

In terms of scripting its unlikely that you have to use them.

If you're wanting to actually learn Java then multi-dimensional arrays are very useful and easy when you get the hang of them

##### Share on other sites

Multi dimmensional arrays are using for storing data in data.

For example:

Imagine if you wanted data for an x y coordinate in a game, you could have:

mapData[x][y]

int[][] multi = new int[][] {

{1,2,3,4}

{5,6,7,8}

{9,10,11,12}

};

row 0 or multi[0] = a single array of int[]{1,2,3,4}

and row 0 column 2 would equal 3, multi[0][2] = 3 ( int[]{1,2,3,4} )

So really multidimmensional arrays are an array of arrays, if that makes sense .

As for scripting, unless you plan on making collision path detection(finding a path to some position and consider collisions when determining the path) or something similar(which I think OSBot 2 will provide anyways), you most likely won't use it.

Enums would be more efficient in storing data for a particular script(for example, agility obstacles in an agility script, or potion creation data in a herblore script).

If I went wrong anywhere, please someone correct me ;P.

It would really be better to store it like this

Assuming mapWidth and mapHeight are defined variables

int[][] mapStuff = new int[mapHeight][mapHeight] { ... init stuff ... } ;

Because remember, mapStuff is actually an array in itself. What you need to understand is that an array is always an array. The concept of multidimensional arrays is just that, a concept... In fact, an array can store any item, be it primitives, (int, double, etc) or objects (classes)

For a multidimensional array the item in question being stored is simply another array. To rephrase, you are storing an array... of arrays.

That brings me back to why you would store the height (y position) then width (x position). I feel it's best to show it as an example

Let's take this for example

int[][] mapStuff = new int[3][2] {

new int[] { 0, 1 },

new int[] { 3, 1},

new int[] { 0, 3 }

}

It looks as a traditional coordinate system should... left to right is x, bottom to top is y. However, the array containing arrays is defined first To rephrase, mapStuff is an array of values containing x-coordinates. So, mapStuff[0] is an array of all x-coordinates when y is 0, and mapStuff[1] is an array of all x-coordinates when y is 1, and so on and so forth. So, to access an element, you would do mapStuff[y][x], rather than the traditional x and y.

I know it's a but weird to understand, so if you have any questions feel free to ask

Edited by fireswap
##### Share on other sites

It would really be better to store it like this

Assuming mapWidth and mapHeight are defined variables

int[][] mapStuff = new int[mapHeight][mapHeight] { ... init stuff ... } ;

Because remember, mapStuff is actually an array in itself. What you need to understand is that an array is always an array. The concept of multidimensional arrays is just that, a concept... In fact, an array can store any item, be it primitives, (int, double, etc) or objects (classes)

For a multidimensional array the item in question being stored is simply another array. To rephrase, you are storing an array... of arrays.

That brings me back to why you would store the height (y position) then width (x position). I feel it's best to show it as an example

Let's take this for example

int[][] mapStuff = new int[3][2] {

new int[] { 0, 1 },

new int[] { 3, 1},

new int[] { 0, 3 }

}

It looks as a traditional coordinate system should... left to right is x, bottom to top is y. However, the array containing arrays is defined first To rephrase, mapStuff is an array of values containing x-coordinates. So, mapStuff[0] is an array of all x-coordinates when y is 0, and mapStuff[1] is an array of all x-coordinates when y is 1, and so on and so forth. So, to access an element, you would do mapStuff[y][x], rather than the traditional x and y.

I know it's a but weird to understand, so if you have any questions feel free to ask

"So really multidimmensional arrays are an array of arrays, if that makes sense ;)"

The map data multidimensional array was merely an example. I wasn't attempting to tutor him in how map data would be stored, rather how to utilize multidimensional arrays.

I see that I left out my "new int[]"'s in my mapData multi array so I see where you thought I went wrong. I'm typing all these replies on mobile so not exactly easy to type them out.

Edited by Allen
##### Share on other sites

Teachers always call 2d arrays a table, which pisses me off. It's a damn array...of arrays.

• 1