aspose file tools*
The moose likes Beginning Java and the fly likes Converting a two dimensional int array into a byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting a two dimensional int array into a byte array" Watch "Converting a two dimensional int array into a byte array" New topic
Author

Converting a two dimensional int array into a byte array

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Take a look at these lines from the code you posted:

There are a couple of things wrong here. First of all, notice how I formatted the code. I did this because a for loop only repeats the first line immediately afterwards. When I'm coding, I typically indent the line that is being repeated. I doubt this is what you intend. If you want the loop to repeat multiple lines, you should use { } to surround them. In fact, it is a good practice to use { } even if you only want to repeat a single line of code. That way you can easily add more lines of code without breaking it.

However, there slightly easier way to do this. In particular, I see that i already counts from xStart to xEnd. If you do it right, you can use i in the loop to show you which element in the array to access. If you use a little ingenuity, you can also use i to give you the y coordinate in the array as well. As a hint, what if i starts at 0. What do you want to count up to (or down to) in the for loop? Also, is there a way to make the code "calculate" the direction to move rather than using an if statement? If it helps, think about what the slope of the line means? In other words, how do you calculate slope and how can you use that information?

I hope this helps give you some ideas. Good luck.

Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by marc weber:
Two things to consider...

First, the conventional definition of slope might lead you astray in this context. Remember, we're not using a typical x-y system. Instead, the y values are reversed -- meaning that as y increases, the point moves downward rather than upward.

Second, as we move from xStart to xEnd, we are not necessarily increasing, because we don't know in which order the user entered the points. For example, consider drawLine(9, 9, 2, 2). Therefore, a for loop constructed as (int i = xStart; i < xEnd; i++) could be a very dangerous thing.


From my point of view, the direction that is defined for the y-values is a technical detail. The mathematics works exactly the same way. For example, if the slope is positive, it means that a positive change in x corresponds to a positive change in y. The only difference is how the "positive direction" is defined. If you are careful how you write the code, this difference will be invisible in the calculations. The only place it shows up is how you access the elements in the array.

Layne
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Layne Lund:
...From my point of view, the direction that is defined for the y-values is a technical detail. The mathematics works exactly the same way...

True, the mathematical definition is exactly the same. But this is a "technical detail" that can be very confusing in writing the code if you are envisioning this backwards (or upside down, as the case may be).
[ September 23, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
Guys I'll be honest with you. This is due tonight and I don't know how to complete it with what has been said so far. I'm just going to have to turn it in as is and hope I don't fail it completely.

Thanks for the help, everyone.


Greg Roberts<br />CIS Student<br />University of West Florida
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If it's not too late, I'll toss out this last suggestion.

I found it helpful to identify which x and y values were greater -- especially since we don't know the order in which coordinates might have been entered. This allowed me to set starting and ending points for my loops, knowing whether I needed to increment or decrement between them.

There are more elegant and efficient ways to do this, but I think this is a good approach to keep track of what's going on.
[ September 23, 2005: Message edited by: marc weber ]
 
 
subject: Converting a two dimensional int array into a byte array