Wrox Programmer Forums > Java Huh? points[i] = new Point(coords[i][0],coords[i][1]);
 | Search | Today's Posts | Mark Forums Read
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
 This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0 Read more about Ivor Horton's Beginning Java, Java 7 Edition or buy the book from your favorite retailer Download the code for Ivor Horton's Beginning Java, Java 7 Edition
 Welcome to the p2p.wrox.com Forums. You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
October 25th, 2012, 03:11 AM
Authorized User
 Points: 49, Level: 1
 Activity: 0%

Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Huh? points[i] = new Point(coords[i][0],coords[i][1]);

//Please skip down to the section where there is the line of code:
// points[i] = new Point(coords[i][0],coords[i][1]);

import Geometry.*; // Import the Point and Line classes

public class TryPackage {
public static void main(String[] args) {
double[][] coords = { {1.0, 0.0}, {6.0, 0.0}, {6.0, 10.0},
{10.0,10.0}, {10.0, -14.0}, {8.0, -14.0}};
// Create an array of points and fill it with Point objects
Point[] points = new Point[coords.length];
for(int i = 0; i < coords.length; ++i)
//The Line below I am having a hard time understanding. Why 0 and 1?
//What about the rest of the elements in the array? How are they accounted for?
points[i] = new Point(coords[i][0],coords[i][1]);

// Create an array of lines and fill it using Point pairs
Line[] lines = new Line[points.length - 1];
double totalLength = 0.0; // Store total line length here
for(int i = 0; i < points.length - 1; ++i) {
lines[i] = new Line(points[i], points[i+1]); // Create a Line
totalLength += lines[i].length(); // Add its length
System.out.println("Line "+(i+1)+' ' +lines[i] + " Length is " + lines[i].length());
}
// Output the total length
System.out.println("\nTotal line length = " + totalLength);
}
}

Last edited by alex001; October 25th, 2012 at 03:16 AM..
October 25th, 2012, 05:27 PM
Friend of Wrox
 Points: 1,515, Level: 15
 Activity: 0%

Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

Quote:
 Originally Posted by alex001 //Please skip down to the section where there is the line of code: // points[i] = new Point(coords[i][0],coords[i][1]); //and read the comments above it. import Geometry.*; // Import the Point and Line classes public class TryPackage { public static void main(String[] args) { double[][] coords = { {1.0, 0.0}, {6.0, 0.0}, {6.0, 10.0}, {10.0,10.0}, {10.0, -14.0}, {8.0, -14.0}}; // Create an array of points and fill it with Point objects Point[] points = new Point[coords.length]; for(int i = 0; i < coords.length; ++i) //The Line below I am having a hard time understanding. Why 0 and 1? //What about the rest of the elements in the array? How are they accounted for? points[i] = new Point(coords[i][0],coords[i][1]); // Create an array of lines and fill it using Point pairs Line[] lines = new Line[points.length - 1]; double totalLength = 0.0; // Store total line length here for(int i = 0; i < points.length - 1; ++i) { lines[i] = new Line(points[i], points[i+1]); // Create a Line totalLength += lines[i].length(); // Add its length System.out.println("Line "+(i+1)+' ' +lines[i] + " Length is " + lines[i].length()); } // Output the total length System.out.println("\nTotal line length = " + totalLength); } }

[code]
for(int i = 0; i < coords.length; ++i) {
points[i] = new Point(coords[i][0],coords[i][1]);
[code]

I can see where you're getting confused. Remember that coords is an array of pairs of co-ordinates. That is an array of arrays.

coords[0] is the pair {1.0, 0.0}, a 2 element array.

coords[0][0] is the double 1.0
coords[0][1] is the double 0.0

You could represent visually as a matrix like this
Code:
```         [0]   [1]
[0]   {  1.0,  0.0 }
[1]   {  6.0,  0.0 }
[2]   {  6.0, 10.0 }
[3]   { 10.0, 10.0 }
...```
Remember that the line you're getting confused about is /within/ a for loop.

When you see:

Code:
`coords[i][0]`
That means, on the first iteration of the loop, coords[0][0], which is the double 1.0. On the next iteration of the loop you'll be looking at coords[1][0] and coords[1][1].

Or visually

Code:
```            [0]   [1]
i==[0]   {  1.0,  0.0 }
i==[1]   {  6.0,  0.0 }
i==[2]   {  6.0, 10.0 }
i==[3]   { 10.0, 10.0 }```
__________________
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
October 26th, 2012, 01:12 AM
Authorized User
 Points: 49, Level: 1
 Activity: 0%

Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!!!

Your explanation really helped me sort things out. Thank you!

 Similar Threads Thread Thread Starter Forum Replies Last Post "Oracle 11g included on the DVD"...huh? monicaroman BOOK: Beginning Oracle Application Express ISBN: 9780470388372 1 May 9th, 2010 12:08 AM Points, Level & Activity... Oh, my! SerranoG Forum and Wrox.com Feedback 1 January 2nd, 2009 02:27 PM Get the mouse coords? worldspawn C# 3 April 14th, 2007 08:23 PM Image Map Coords interrupt Javascript How-To 1 March 8th, 2005 09:59 AM