What are some practical uses of 2D and 3D arrays in Java?
December 5, 2019 1:41 PM   Subscribe

I know how to code them, but I'd like to try a few real-world examples to get a better understanding of their utility.
posted by markcmyers to Computers & Internet (9 answers total) 1 user marked this as a favorite
 
You could use a 2D array to store the pixels in a picture or the land/water types in a map.
posted by clawsoon at 1:58 PM on December 5, 2019


One calculation we did in my scientific computing class was a "simple" simulation of the finite difference method applied to model diffusion. Diffusion is a common type of physics where the the quantity changes based on the difference between adjacent values. It is used in calculations of temperature, water pollution, gas pressures, and many others. You calculate it by setting a grid of points that represent the space your solution covers, as well as some boundary conditions, such as fixed values at the outermost points. Then you iterate, transferring "fluxes" that are proportional to the difference between the adjacent points. If you choose boundary conditions that aren't changing, then the system will converge to a "steady state" where none of the quantities are changing much. This is a classic type of simulation, not used much anymore because it's suited to rectilinear shapes and the real world is curved. But it's a great use of a matrix, and a ton of things you use every day were simulated during their design using this method. The hard part is probably going to be writing out your data in a form you can visualize.
posted by wnissen at 2:01 PM on December 5, 2019


Implement Conway's Game of Life?
posted by crocomancer at 2:01 PM on December 5, 2019


Lots of image manipulation involves 2D arrays, particularly for grayscale or indexed color images. You might find some 2d array usage in the source code for ImageJ plugins. (although the one that I picked at random just used 1D arrays and did the row offset math manually).
posted by qxntpqbbbqxl at 2:03 PM on December 5, 2019


Matrix based mathematics and all the things that depend on such things like linear algebra can use arrays as their core data structure.

On a more fundamental basis, arrays are the fastest/easiest random access data structure assuming your array fits in memory. This wasn't a Java example but I remember 20+ years ago being a co-op at Compuserve Network Services where I was tasked with doing some rudimentary analysis on enormous (for the time) logs of telephone access numbers of the network. I can still remember the joy when I realized that I could construct a million place array so that I could use the telephone numbers as array indexes as way to count activities. It was way, way, way faster than the previous person's implementation which was keeping a ledger in a random access disk file.
posted by mmascolino at 5:01 PM on December 5, 2019


There have been some good suggestions, so I will try for some that weren't mentioned, though I will probably come across like Charlie Brown seeing a ducky in the clouds. 2D array: any number of board games such as chess, checkers or go. A crossword-puzzle or word-search puzzle generator.

A 3D array could easily be used to record the time-history of one of these games, though there are clearly more memory-effective ways to do it. A (short) animation can be stored as an array of 2D arrays (a 3D array).

Speaking of word-search generators, I long-ago wrote a program to produce word-search puzzles for my daughter's elementary-school class for the holidays. The general idea was that you entered the words you wanted hidden, and it did some kind of Markov thingie to generate additional chaff. I should have known it would randomly several obvious obscenities which enraged the other parents. Philistines. Why did they hate Christmas?
posted by Gilgamesh's Chauffeur at 6:06 PM on December 5, 2019 [1 favorite]


You can represent a directed graph as an adjacency matrix, which is an efficient data structure if your graph has many edges.
posted by J.K. Seazer at 6:11 PM on December 5, 2019 [2 favorites]


both are directly applicable to physical problems with the same dimensionality — e.g. a 3d physical space could be quantized and modeled with a 3d array.

outside of physical problems, N-dimensional time-series data can be modeled with an N+1 dimension array — e.g. 1d data which varies over time can be modeled as a 2d array where one of the dimensions is time quanta. 2d data which varies over time can likewise be modeled as a 3: array. this idea is a basis of the conceptual model of a time-series database: . google’s bigtable database is an implementation of that model (see section 2 of the paper). google’s spanner database also implements this concept, with some additional bells and whistles around how exactly the time dimension is handled.

linear algebra implementations make frequent use of 2 and 3 dimensional arrays to model matrices and tensors (though most of the code i know abstracts the array away behind a class which makes it easier to handle). an example of this is the tensorflow model of, well, tensors. since i’m already writing like a curmudgeonly bastard, i’ll note that tensorflow’s description of tensors doesn’t exactly match the mathematical definition, and that’s because they are also bastards who decided to make some simplifications for their own sake. we live in the shadows of selfish giants, standing on each other’s shoulders.

many computer science data structures boil down to 2 and 3 dimension arrays, but not usually as a rule. what i mean is, it may be convenient to implement a data structure using an array, but not required. as mentioned above, an adjacency matrix illustrates this. it is an implementation of a graph, but you could implement a graph without a 2d array — an adjacency list is an example of this. they have different strengths and weaknesses.

posted by =d.b= at 12:13 AM on December 7, 2019


Minecraft stores data in 3d arrays called chunks.
posted by Green With You at 5:52 AM on December 7, 2019


« Older Stage fright is making me frightened!   |   Tall women’s pantsuits or maybe a unicorn? Newer »
This thread is closed to new comments.