aspose file tools*
The moose likes Java in General and the fly likes Playing around with 2D arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Playing around with 2D arrays" Watch "Playing around with 2D arrays" New topic
Author

Playing around with 2D arrays

Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45


Hey ! I'm having fun playing around with arrays.
here's the thing I'd like to do. I have a N by N grid of entries, per entry I'd like to save a list, ie, a 1D array of data. How does that work? so far I've just been dealing with basic 'type' arrays like int and String. Is it being saved as an array or object? My understanding on the saving objects into arrays is still a bit fuzzy.
Thanks a lot for the help!
J
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Have you considered a list of lists?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jake Mauve wrote:
Hey ! I'm having fun playing around with arrays.
here's the thing I'd like to do. I have a N by N grid of entries, per entry I'd like to save a list, ie, a 1D array of data. How does that work? so far I've just been dealing with basic 'type' arrays like int and String.


It's no different than anything else. (Although it's not really clear what you're asking.)


Also, note that as far as the Java language is concerned, there really is no such thing as a multidimensional array. A 2D array is just a 1D array whose elements happen to be arrays.

Is it being saved as an array or object?


Again, not clear just what you're asking. However, an array IS an object.
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

ok, lets imagine the following code :



concerning all 9 entries, I'd like to save a list of numbers in them. for the list I was thinking of using (another) array, a simple "1D" array. Does that work?

J
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Jake Mauve wrote:ok, lets imagine the following code :



concerning all 9 entries, I'd like to save a list of numbers in them. for the list I was thinking of using (another) array, a simple "1D" array. Does that work?


Sure. It will work fine. However, you are responsible for mapping the two indexes to the one index needed by the "1D" array.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jake Mauve wrote:ok, lets imagine the following code :



concerning all 9 entries, I'd like to save a list of numbers in them.


Eh?

The 9 entries hold ints. Can you please explain, clearly and precisely, what you're really trying to accomplish?
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

yes, i understand that each entry contains an 'int' type value.
what type would I need to use to be able to save a list of int's in each of the 3x3 grid entries? I'm trying to make a basic sudoku solver. the first step I thought I'd take was to get an algorithm to make a list with all the possible numbers per cell. Obviously I'm using a bigger grid than 3x3.
so I tried saving that list of possible numbers into its respective cell in the grid,

but it doesn't work because of type mismatch which seems logical. So, how does one save a 1D array into the entry of a 2D array? ^^ do I need to make it a [][][]array?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jake Mauve wrote:
what type would I need to use to be able to save a list of int's in each of the 3x3 grid entries?


Ohhhhh... Now I see!

Note that if you have int[][] arr = new int[3][3]; then what you really have is not a 2D array of ints (though we usually treat it that way in our code). What you have is a 1D array, each of whose elements is an array of ints.

So, taking that logic one step further, if by "list" you mean "list-ish thing" rather than strictly literally a java.util.List, then you would simply do


Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

awesome thanks ! xD exactly what I needed =) it will be the first option though since the 'lists' for each cell, or 'grid entry', vary in size. and thanks for repeating the thing about it not just being two 'dimensions'/array of array but an array with an array of ints in each of its entries ^^ helped me understand the final 'piece' of the puzzle =) I've just started with array so haven't even tried arraylists yet. want to master simple arrays first ^^
So now I just need to make a method to save each of those 'lists' to each empty cell and then figure out how to reduce those lists until they only have one item remaining. I know how to change an array's size by copying it into a different sized one, just need to figure out how to get around the cells with same possible values without having to research and implement brute force+backtracking xD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Playing around with 2D arrays