Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
|
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 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 January 30th, 2004, 10:49 AM
Registered User
 
Join Date: Jan 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stroing Objects in Vector at indexed value

Hi Author!

i am reading chap 13 in Java2 sdk1.4.

in page 512, it was written that "The index value must be less than or equal to the size of the Vector..." . again in the next paragraph it was written "If you specify an index value argument that is negative, or greater than or equal to the size of the Vector, the method will through an ArrayIndexOutOfBoundsException".

is it confusing or am i getting confused???

Thanq,
Hanuman
 
Old January 30th, 2004, 11:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

An index value for a vector cannot be negative, nor can it be greater than the size of the vector, otherwise it will throw an exception.

Cheers

Martyn
 
Old February 5th, 2004, 11:19 PM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The containers in programming, like array, vector or other datastructures are zerobased. That means that the first element is number '0'. So if a vector has two elements ( or objects ), the first one has the index( number ) '0', and the second has the index '1'. The third element has the number 3 - 1 = 2, so in a Vector that have 100 elements the last element has the number 99 ( again, because you begin to count from zero ). So if a Vector has 100 elements, and you try to axcess element number 100, you are trying to axcess .....nothing, then: ArrayIndexOutOfBoundsException
hope you understand this
Rune

 
Old February 6th, 2004, 08:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think the confusion is due to the fact that the book mistakenly says that a valid index must be <= the array size. This, as you both have mentioned, is incorrect since the largest valid index value is the array size - 1.


I think it's a typo in the book -- check if an errata page for the book exists. If so, check to see if this is already listed there. If not, submit it as a new error.


Also, Rune: There are several programming languages that begin numerically indexed arrays as 1, not 0, so you don't want to generalize that "containers in programming... are zero based." Numerically indexed arrays and vectors in Java, C, and C++ certainly are zero based, but by no means are ALL containers in ALL languages zero based.

In fact, it's REALLY REALLY easy to write a simple wrapper class around a java (or C++, etc..) vector that creates a 1-based vector.


Take care,

Nik
http://www.bigaction.org/
 
Old February 10th, 2004, 02:04 AM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry Nik
I did generalize to much. I don' know all programming language, but some, as you say C, C++ and Java and took a shortcut and made a too fast generalization. But that write a wrapper class that is not zero-based is not a good argument.
Generalization is infact a common trap. Too many people go into it. Thanks for telling me, so I could see it, and be more carefull next time!
Rune






Similar Threads
Thread Thread Starter Forum Replies Last Post
Stroing binary data using OLADB aliirfan84 .NET Framework 2.0 0 May 19th, 2007 07:42 AM
having problem Using Indexed properties with Multi kris_rathi79 Struts 0 June 27th, 2006 07:56 AM
"validator" mis-indexed? jemptymethod BOOK: Professional Java Development with the Spring Framework 1 August 1st, 2005 09:17 AM
Need Help : Indexed View upermadi SQL Server 2000 3 September 29th, 2004 06:51 AM
Indexed View jemacc SQL Server 2000 2 July 21st, 2004 05:11 AM





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