• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Vector

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*
import java.util.*;
public class Java_Collection {

public static void main(String args[]){

List theList=new Vector();

theList.add(0, "Chicago");

theList.add(1, "Vegas");
theList.add(4, "Jersey");



Iterator it=theList.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
*/

}

1.why the program is throwing runtime Exception?List is sorted by index and i was expecting the answer.

2.when i search javaranch,i found lots of questions as Arrays.asList.For SCJP1.4 exam,do i need to know about Arrays class and its methods.

please help on the above two questions
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi Shiv,

what about index position 2 and 3. You directly jumped to index 4. That is why java.lang.ArrayIndexOutOfBoundsException was thrown by JVM.


Regards,
cmbhatt
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,Thanks for the reply.

Do we need to give all the index numbers in order.it may be a silly question,but Collection is entierly new for me.
Then what is the purpose of ordering since ordering is done by index?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Shiv
Then what is the purpose of ordering since ordering is done by index?



Actually the basic use (and often used) of the add(index,Object) is to provide something you call resetting the Object at given index.

When you simply use:
add(Object);
It is added sequentially in the list. But giving index, it goes to that position and sets the Object. I use "sets" because its soul purpose it that. From scratch adding object to the collection by giving it the index position as first argument is like you add to the array (clumsy way).

Collection made it convenient; taking care of indexes itself.


Regards,
cmbhatt
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The size of the vector will be verified with the index position when a new object is being added.

If the size of the Vector is less than index position then java.lang.ArrayIndexOutOfBoundsException will be thrown.

Have a look at java source.

Note: The add method inturn will call following method.



[ April 11, 2007: Message edited by: Srinivas Kalvala ]
[ April 11, 2007: Message edited by: Srinivas Kalvala ]
 
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