Wrox Programmer Forums
|
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
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
 
Old February 14th, 2013, 03:46 AM
Registered User
 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Nested Collection Based Loops

See page 124.
I want to loop through my 2 dimensional by nesting a collection based for loop within another collection based for loop.
In the book we create:
float[][] temp = new float [10][365]; //we fill this 2D Array with random float values

//I want an outer loop to go through the 1st dimension and the inner loop to go through the 2nd dimension:


float average = 0;
for(float[] locations:temperature){//cycle through the outer loop
for(float[] temps:temperature[locations]){//cycle through the inner loop
average += temps;
}
System.out.println(average);
average = 0;
}


The inner loop returns an error saying an int is required not a float[]
 
Old February 14th, 2013, 12:10 PM
Registered User
 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's not so much going through the collection based for loop that I can't figure out, I have that, it's nesting them inside each other and accessing the elements in the 2nd dimension with the inner loop that I can't figure out.
 
Old February 15th, 2013, 12:52 AM
Registered User
 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

// I got it, the inner loop is just float not float[]
// I was trying to go too many dimensions deep in the inner loop
// that took 90 minutes to figure out



Code:
public class WeatherFan2{
    static int count = 0;
    static float average = 0;

    public static void main(String[]args){
        float[][] sample = new float[10][10];
        for(int x = 0;x<10;x++){
            for(int y = 0;y<10;y++){
                sample[x][y]= (float)((Math.random()*100)+1);
                
            }
        }
        //we now have a 10x10 2D array
        loop:
        for(float[] temperature:sample){
            for(float t:temperature){
                average +=t;
            }
            System.out.println(average/sample[count].length);       
        average =0;
        }
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Loops from XML to Flat Fiel Conversion chilly XSLT 26 April 19th, 2010 07:12 PM
errors with nested Do While Loops hddavie BOOK: Beginning ASP 3.0 0 August 5th, 2009 04:20 PM
Nested Loops in SQL Query values from table/set miamikk SQL Server 2000 1 August 31st, 2007 12:35 AM
weird program flow with nested loops zayasv Intro Programming 2 November 17th, 2005 06:19 AM
problem with nested loops ptaylor2005 Beginning PHP 4 April 27th, 2005 07:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.