Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Playing around with 2D arrays

 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered a list of lists?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21501
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic