This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Spiral Matrix

matej spac
Greenhorn
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

Winston Gutkowski
Bartender
Posts: 10527
64
• 2
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.

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

Campbell Ritchie
Sheriff
Posts: 50225
79
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
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
Posts: 50225
79
Yes, WG is good. Please tell us how you solved the problem.

Winston Gutkowski
Bartender
Posts: 10527
64
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
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
Posts: 10527
64
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
Posts: 4481
3
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.

Martin Vajsar
Sheriff
Posts: 3752
62
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
Posts: 10527
64
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