?
Solved

get()

Posted on 2005-04-11
5
Medium Priority
?
161 Views
Last Modified: 2010-03-31
In vectors, what is the difference between using a get() and using a iterator(). which is better?. I have used both of them.

Regards

Eugene




0
Comment
Question by:eugene007
5 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 13754412
Hi,

Vector implements the RandomAccess interface (as of JDK 1.4) so it is slightly more performant to use get with a for loop than it is to use iteration.

Performance issues aside, there might be other concerns that affect your decision. It is considered good design with collections to rely upon the most abstract interface that makes sense. In this case, it might be List or Collection. The reason is that you might one day choose to replace the concrete collection class with something else. For example, if you one day decided you needed to replace your Vector with a LinkedList then your choice to use get instead of iteration would cause a degradation in performance as LinkedLists are expensive to access in a RandomAccess manner.

There's also the possible concern of concurrent access and perchance modification of the collection while iterating. If two threads are accessing the Vector, one looping across the contents and another modifying, the for loop construction will sometimes fail, usually in a manner that is difficult to diagnose. The collection iterators are fail-fast so they will detect the concurrent modification and throw an explicit exception that can be caught and handled.

Regards,
Jim Cakalic
0
 

Author Comment

by:eugene007
ID: 13759338
so the get() method access the vector in a random access manner. what about iterator() how does it access the vector?.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 13759676
Iterator is a way to traverse through a collection,
usually by a natural sequence.
;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13760378
AFAIK, the iterator will be invalidated and it might throw an exception if another thread modifies the collection.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 total points
ID: 13761990
Hi, eugene007.

I think you have it turned a bit askew. Vector implements the RandomAccess interface -- just a marker interface -- to indicate that its implementation allows random access to elements using the get method in an efficient manner. Vector uses an Object[] to store its elements. You can directly index into the Object[] to retrieve any element with no time penalty. Contrast this with LinkedList which does not implement the RandomAccess interface because it is inefficient to use get to retrieve elements from the collection. LinkedList stores its elements as a series of connected nodes. To get to a specific element LinkedList must start at the head (1st element) and traverse all the intervening elements until it arrives at the one you requested. Accessing the last element requires going through all the other elements to get there.

How an iterator accesses the underlying collection is implementation specific. The Vector Iterator implementation keeps an index of the current element in the collection. Each time you call iterator.next() it retrieves the current element using the Vector get method, increments the index, and returns the element. The LinkedList Iterator actually keeps a reference to the node in the list that is next. When its iterator.next() is called, it sets the next reference to the node pointed to be the current node and then returns the element stored at the current node. Similarly, previous moves the next reference "backwards". The get method is not used by the LinkedList Iterator because it would be inefficient.

Jim
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question