• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Some Simple(??) Java problems.

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is a small program i've typed out of Ivor Horton's Beginning Java 2. I have some queries regarding it, and if anybody could help me out, that'd be great.

It's a program that generates the temperatures at random, vales between -10C and 35C.

Well, what's going on in the line wiht the code:-
for (int j = 0; j < temperature[i].length; j++)
why the need for
temperature[i]
couldn't we have just said:-
for (int j = 0; j < temperature.length; j++)

Also, what's going on, on the line with the code:-
System.out.println("Average temperature at location " + (i+1) + " = " + average/(float)temperature[i].length);
why the need for the +1
Wouldn't :-
System.out.println("Average temperature at location " + (i) + " = " + average/(float)temperature[i].length);
have been ok??
Cheers in advance
(Marilyn added code tags)
[ January 16, 2003: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its a two dimentiale arrey temperature[10][365]
its means 10 rows and each row have 365 colum that why
they take temprature[i].lenght its length must be 365 they must have to fill ...
some data from nested loop to (45.0*Math.random() - 10.0);
if u take temprature.lenght but it has ten rows its size must be ten then others 355 value will be empty..
loop mostly start for the 0 then initial but line dont start with the 0...
public class WeatherFan
{ // outermost bracket
public static void main (String[] args)
{
float [] [] temperature = new float [10] [365]; // Temperature array
// Generate tempartures
for (int i = 0; i < temperature.length; i++)
System.out.println("here u can see the length of these"+temperature.length+" "+temperature[i].length);
for (int i = 0; i < temperature.length; i++)
for (int j = 0; j < temperature[i].length; j++)
temperature [i][j] = (float)(45.0*Math.random() - 10.0);
// Calculate the average per location
for (int i=0; i < temperature.length; i++)
{
float average = 0.0f; // place to store the average
for (int j = 0; j < temperature[0].length; j++)
average += temperature[i][j];
// output the average temperature for the current location
System.out.println("Average temperature at location " + (i+1) + " = " + average/(float)temperature[i].length);
System.out.println("Average temperature at location " + (i) + " = " + average/(float)temperature[i].length);
}
}
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what's going on in the line with the code:

for ( int j = 0; j < temperature[ i ].length ; j++ )

why the need for temperature[ i ]

couldn't we have just said:

for ( int j = 0; j < temperature.length ; j++ )

??


You are seeing an array of arrays. In other words temperature[0] is an array
temperature[1] is a different array.
etc.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, what's going on, on the line with the code:

System.out.println("Average temperature at location " + ( i + 1) + " = " + average/(float)temperature[ i ].length );

why the need for the +1

Wouldn't :

System.out.println("Average temperature at location " + ( i ) + " = " + average/(float)temperature[ i ].length );

have been ok??


I guess they just didn't want to print
Average temperature at location 0 = 45 (or whatever number they came up with)
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn de Queiroz:
Well, what's going on in the line with the code:

for ( int j = 0; j < temperature[ i ].length ; j++ )

why the need for temperature[ i ]

couldn't we have just said:

for ( int j = 0; j < temperature.length ; j++ )

??


You are seeing an array of arrays. In other words temperature[0] is an array
temperature[1] is a different array.
etc.


OK, but, how does temperature[ i ].length refer to the length of the second dimension, 365? I think it does, but I can't see why.
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of it this way:
temperature[i]IS an array since you are talking about an array of arrays, it is the "ith" element of the the temperature array. And you already know that all arrays have a field containing their length, therefore you can just call temperature[i].length to get the length of the array.
[ January 15, 2003: Message edited by: Eric Fletcher ]
[ January 15, 2003: Message edited by: Eric Fletcher ]
[ January 15, 2003: Message edited by: Eric Fletcher ]
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Below is a small program i've typed out of Ivor Horton's Beginning Java 2. I have some queries regarding it, and if anybody could help me out, that'd be great.
It's a program that generates the temperatures at random, vales between -10C and 35C.

code:
--------------------------------------------------------------------------------
public class WeatherFan{ // outermost bracketpublic static void main (String[] args) { float [] [] temperature = new float [10] [365]; // Temperature array // Generate tempartures for (int i = 0; i < temperature.length; i++) for (int j = 0; j < temperature[i].length; j++) temperature [i][j] = (float)(45.0*Math.random() - 10.0); // Calculate the average per location for (int i=0; i < temperature.length; i++) { float average = 0.0f; // place to store the average for (int j = 0; j < temperature[0].length; j++) average += temperature[i][j]; // output the average temperature for the current location System.out.println("Average temperature at location " + (i+1) + " = " + average/(float)temperature[i].length); } }}// outermost bracket
--------------------------------------------------------------------------------

Well, what's going on in the line wiht the code:-
for (int j = 0; j < temperature[i].length; j++)
why the need for
temperature[i]
couldn't we have just said:-
for (int j = 0; j < temperature.length; j++)
Also, what's going on, on the line with the code:-
System.out.println("Average temperature at location " + (i+1) + " = " + average/(float)temperature[i].length);
why the need for the +1
Wouldn't :-
System.out.println("Average temperature at location " + (i) + " = " + average/(float)temperature[i].length);
have been ok??
Cheers in advance
(Marilyn added code tags)


------Question 1: Why have the line
for (int j = 0; j < temperature[i].length; j++)
The reason we say temperature[i].length and not temperature.length is that the compiler only sees a one dimensional array first temperature[i]. By adding the second subscript you are telling the compiler that each of those inital elements are also arrays, so temperature[i][j] would mean that each of the elements temperature[i] is an array and contain j elements (An array of arrays so to speak. Similarly temp[i][j][k] would be an aray of arrays of arrays, and so on)
So to get the length of the first array in the multidimensional array, you have to say temperature[i].length.
In the line,
System.out.println("Average temperature at location " + (i+1) + " = " + average/(float)temperature[i].length);
the reason they have i+1 and not i is that i is the loop counter which you have initialized with 0. So if you had used i and not i+1, in the output you would have
Average temperature at location 0=...
Average temperature at location 1=...
Average temperature at location 2=...
Average temperature at location 4=...
If you used i+1, then he output would be
Average temperature at location 1=...
Average temperature at location 2=...
Average temperature at location 3=...
Average temperature at location 4=...
Hope its all clear now
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK
So, what does this do??
temperature [i][j] = (float)(45.0*Math.random() - 10.0);
I have been racking my brains to suss what is going on here. I mean, ok, we're generating random values, but what is the raealtion of these randomly generated values, to the array of arrays [i][j] ?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
temperature is an array of arrays of floats, so
temperature[i] is the i'th array of floats, so
temperature[i][j] is the j'th float in the i'th array
You can also think of temperature as a two-dimensional matrix of 10 x 365 floats. In the two nested for loops, every single of those floats gets assigned a random value.
Did that help?
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of it as a table.
float [][] temperature = new float[10][365]; // Temperature array

In this table of floats you have 10 rows and 365 columns.

So the maximum length of the table (number of rows) is 10.
The maximum length of each row in the table (number of columns) is 365.



temperature[ i ].length is the length of the i'th row of the table. Each row in this particular table is the same length, 365. So this line could be written as:



This would fill up the 10th row with floats in each of the 365 columns.

However, writing it this way could be more dangerous. If you for any reason change the size of the array you could get an ArrayIndexOutOfBoundsException.
[ January 16, 2003: Message edited by: Marilyn de Queiroz ]
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I've understood this code, thanks to the explanations given by ALL the people who have contributed to this thread.
Thanks to each and every one of you, folks!
Just one thing though, should I memorise the procedures used here, or just get an overall picture of how arrays can be manipulated?
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion understanding is much more important than memorization most of the time.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic