I\'ve just started to learn Java,and I\'ve got some questions for my code. Well,the task is to get a number and print a matrix like this:
get 3,and print:
1
(The answer there supposes you have an IDE, like Eclipse).
You can, when in doubt, always rely on a good old System.out.println(yourobject)
. Be careful about objects, never forget that you can turn them into String
and check inner objets (in your case, visited[1][2]
or matrix[2][0]
for example). BUT it sucks. Really. It might suffice for your needs, but it is really a bad habit to take.
Use a tool named SLF4J to get some additional traces. As you are only on a small method and not a complete project, I would not command you to deploy this tool yet, but keep in mind that it is a good way to both figure out what happens and have your console logging what you need, especially when the projects are getting intersting (read: bigger).
Learn a bit about a tool called debug mode
. You can find further help on the Eclipse help center, but basically the steps are:
debug
. It is a little bug (how convenient) near the classic run icon. You'll get redirected to the debug perspective
.ArrayOutOfBoundsException
and became a better person in general thanks to this good practice.Vogella has a good tutorial about how to use the debugger in a nice way. Learn to use it if you plan on keeping on Java, because this will save you a lot of time (an awful lot, believe me).