This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Spiral Matrix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Spiral Matrix" Watch "Spiral Matrix" New topic
Author

Spiral Matrix

matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
Hello,
I have a problem with reading 2D array in spiral way. It must be read from bottom left corner - to bottom right corner - to top right corner etc like spiral. I try different approaches but still get in some step ArrayIndexOutOfBoundsException. Please help me complete "output" method.

Input example:
10 9 8 7
11 16 15 6
12 13 14 5
1 2 3 4

Ouput example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16



|OCPJP 6|
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

matej spac wrote:Hello,
I have a problem with reading 2D array in spiral way. It must be read from bottom left corner - to bottom right corner - to top right corner etc like spiral. I try different approaches but still get in some step ArrayIndexOutOfBoundsException. Please help me complete "output" method.

Well first, I don't see a program; I see a set of definitions and an output statement - admittedly with a loop, but that's not a program in any way I understand the word.

My advice (given many times):
1. Turn OFF your computer.
2. Get out a pencil and paper
3. Pick a 2D array size (which, presumably is a square), say 4x4, and select the "spiral" entries yourself BY HAND.
4. Now do the same thing with a 5x5 square, and work out the similarities with what you did in Step 3. (Note: One important thing about a program is to know when to stop).
5. If you still haven't got it, try with a 6x6 square, and (if need be) a 7x7.
6. Write down all the steps that you now understand about the problem in English (or your native language).
7. NOW turn your computer back on and try to write your program from your notes. If you run into trouble, come back and show us what you've done.

You've probably heard this before, but computers are stupid. They do exactly what they're told to (ibid: Captain Kirk, stardate 4231-point-3 et al), even with a wonderful language like Java.

Your task as a programmer, should you choose to accept it, is to translate problems into computer-speak; and that involves understanding the problem.
If you don't understand the solution, I can guarantee you: Java won't help you.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37902
    
  22
What’s a 2D array? They might have them in some languages, but they don’t in Java. There are arrays of arrays in Java™, which are better than 2D arrays anyway.
matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
Winston Gutkowski wrote:
matej spac wrote:Hello,
I have a problem with reading 2D array in spiral way. It must be read from bottom left corner - to bottom right corner - to top right corner etc like spiral. I try different approaches but still get in some step ArrayIndexOutOfBoundsException. Please help me complete "output" method.

Well first, I don't see a program; I see a set of definitions and an output statement - admittedly with a loop, but that's not a program in any way I understand the word.

I don´t wrote "I have a program", but "I have a problem"
However, I follow your steps and it turns out that you are genius - I found solution in 15 minutes
Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37902
    
  22
Yes, WG is good. Please tell us how you solved the problem.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

matej spac wrote:However, I follow your steps and it turns out that you are genius - I found solution in 15 minutes
Thanks.

You're welcome. I too would be interested in what you came up with.

Winston
matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
Matrix must be read form left bottom corner to right bottom corner etc in spiral way.
I read matrix by sides and each reading reduces items in side by 2 items.

This solution works for any type of matrix, however, one senior programmer told me "it is not ideal and you use too many variables" .



Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

matej spac wrote:This solution works for any type of matrix, however, one senior programmer told me "it is not ideal and you use too many variables" .

I'd agree with him there, but the main thing is - it WORKS, and you've thought it through.

You can always refine a program later once you've got it working.

A couple of suggestions for you:

1. Don't mix the problem (making a spiral) with its representation (how it looks, or how you print out the results). If it were me, I think I'd be looking at a method that just returns me the coordinates of each point in the spiral (possibly even all of them together as an array). You could then use them to display the spiral any way you like. Have a look at java.awt.Point to help you do this.

2. Space out your code and use indentation. Also, don't put more than one statement on a line:
if (d <= 0)
   continue;
System.out.print(matrix[delta+1+dx][dx] + " ");
flag++;

is a lot easier to read than:
if(d <= 0) continue;
System.out.print(matrix[delta+1+dx][dx] +" "); flag++;


But it's nice to see that you have a lot of comments. You might also want to read up the tutorials on using Javadoc; it is incredibly useful (one of the best programming documentation tools I've ever seen), and allows you to generate API documentation just like the ones you get for the regular Java classes.

HIH

Winston
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i admit i didn't read your code, but as Winston suggested the Point class might be useful to you. a long time ago i wrote a homework assignment that used a 2D array and a stack of points. i recently revisited it and found a bug and asked for help here at javaranch. you might want to take a look at how i used points to reference the elements in the array. the code is here

What’s a 2D array? They might have them in some languages, but they don’t in Java. There are arrays of arrays in Java™, which are better than 2D arrays anyway.


perhaps they are technically arrays of arrays but i also choose to call them two dimensional arrays.


SCJP
Visit my download page
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Winston Gutkowski wrote:1. Don't mix the problem (making a spiral) with its representation (how it looks, or how you print out the results). If it were me, I think I'd be looking at a method that just returns me the coordinates of each point in the spiral (possibly even all of them together as an array). You could then use them to display the spiral any way you like.

Another possibility along these lines would be creating and returning an iterator that would traverse the 2D array in the required way.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Martin Vajsar wrote:Another possibility along these lines would be creating and returning an iterator that would traverse the 2D array in the required way.

Yes, I rather like that. Very slick.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Spiral Matrix
 
Similar Threads
Regular expression - Greedy and Reluctant quantifiers
how this setOut method works.......
pattren
Parsing M*N matrix on Periphery
HashSet Magic