• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

would like some help understanding field.size() in array list class

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay -- for the life of me I can't seem to figure this out (which probably means that it's simple and I'm missing the obvious). It's been a long time since I've been here, and I'm getting back into Java. I'm just putting together a little 'horse racing' program while I'm learning Java, and I have a class called Race that creates an array list of thorougbred horses called field, and asks the user to enter then names of the each horse in the field, with a maximum of 14 horses.

The problem occurs with the current code that I have:




And here's the output:

First, enter the names of the horses in the field, up to a maximum of 14 horses; enter 'quit' to stop entries.

current size of field: 0
Enter the name of horse number 1: horse 1

The name of the horse at index 0:horse 1
current size of field: 1
Enter the name of horse number 2: horse 2

The name of the horse at index 1:horse 2
current size of field: 2
Enter the name of horse number 3: horse 3

The name of the horse at index 2:horse 3
current size of field: 3
Enter the name of horse number 4: horse 4

The name of the horse at index 3:horse 4
current size of field: 4
Enter the name of horse number 5: horse 5

The name of the horse at index 4:horse 5
current size of field: 5
Enter the name of horse number 6: horse 6

The name of the horse at index 5:horse 6
current size of field: 6
Enter the name of horse number 7: horse 7

The name of the horse at index 6:horse 7
current size of field: 7
Enter the name of horse number 8: horse 8

The name of the horse at index 7:horse 8
current size of field: 8
Enter the name of horse number 9: horse 9

The name of the horse at index 8:horse 9
current size of field: 9
Enter the name of horse number 10: horse 10

The name of the horse at index 9:horse 10
current size of field: 10
Enter the name of horse number 11: horse 11

The name of the horse at index 10:horse 11
current size of field: 11
Enter the name of horse number 12: horse 12

The name of the horse at index 11:horse 12
current size of field: 12
Enter the name of horse number 13: horse 13

The name of the horse at index 12:horse 13
current size of field: 13
Enter the name of horse number 14: horse 14

The name of the horse at index 13:horse 14
current size of field: 14
Enter the name of horse number 15: horse 15

The name of the horse at index 14:horse 15
There are 15 horses in this race.
Name of horse at index 0:horse 1
Name of horse at index 1:horse 2
Name of horse at index 2:horse 3
Name of horse at index 3:horse 4
Name of horse at index 4:horse 5
Name of horse at index 5:horse 6
Name of horse at index 6:horse 7
Name of horse at index 7:horse 8
Name of horse at index 8:horse 9
Name of horse at index 9:horse 10
Name of horse at index 10:horse 11
Name of horse at index 11:horse 12
Name of horse at index 12:horse 13
Name of horse at index 13:horse 14
Name of horse at index 14:horse 15


the statement of the index position and current size was for me, so I could see what was going on.

What I don't understand is the while loop. not that the class doesn't compile and run (you can see that it does), it's the output. Why does the <= sign allow one more entry and increase the size of the field to 15?

Less than or equal to 14 should give a maximum field size of 14, right, With the starting object at index position at zero and going up to 13, for a total size of 14 thoroughbred objects
if I just use




or a for loop, then the output is fine; it allows a max of 14 entries and prints the results. I thought it had something to do with the size being zero based, but that doesn't seem to matter -- unless it does matter and I'm missing it.
Can anyone tell me why the comparison I'm using produces this output? a field of 14 horses shouldn't matter whether it's zero or 1 based, as long as the size of the field is 14, so why the extra entry with this while condition?





 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>Less than or equal to 14 should give a maximum field size of 14, right, With the starting object at index position at zero and going up to 13
Nope.
Size tells you how many items there are in the list. They are numbered from 0 to size-1.
You are using a while(condition) { body } loop.
This checks the condition first, and then evaluates the body as long as the condition is true. That could mean the body is never evaluated.
You get to 14 elements in the list, so size is 14. size <=14 evaluates to true, and it enters the block to add the 15th horse.
On the next iteration size is 15 (which is greater than 14) and the loop exits.

Contrast this to a

do { body } while (condition)
This syntax always executes once, THEN checks to see if it should go back and do it again.
The check happens at the END instead of the beginning.

 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Stefan -- like I said, it was simple so I missed it. DOH!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic