collection questions

Hi,
i have below collection questions. please advise



why array list implements RandomAccess and cloneable serializeable
why RandomAccess in marker interface and how is interface useful without methods
which collection is good for retrieval,insertion, deletion etc
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
From the docs:

public interface RandomAccess
Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

Like it says, the idea is you can mark a collection as being quick to access things in a random order.
So you can get to element 100 as quickly as you can reach element 2.

So when would that not be true?  Here's another List implementation that uses a simple linked list:

public class MyLinkedList {
    private int m_Value ;
    private MyLinkedList m_Next ;

    public MyLinkedList(int value, MyLinkedList next) {
        m_Value = value ;
        m_Next = next ;
    }
}

Open in new window


MyLinkedList a = new MyLinkedList(10, null) ;
MyLinkedList b=  new MyLinkedList(20, a) ;

Open in new window


I hope you can see that
b is the list "20, 10, null".        (b has value 20, with m_Next set to a that has value 10).

To look up the values in this list the code would be:

public Integer getIndex(MyLinkedList list, int position) {
     while (position != 0 && list != null) {
         position-- ;
         list = list.m_Next ;
     }
     if (list == null)
        throw new ArrayIndexOutOfBoundsException("List doesn't have an element in position " + position) ;

     return list.m_Value ;
}

Open in new window


Since that requires a loop, it will be slower for the 100th element than the 1st, so not random access.

Make sense?

Doug
0
gudii9Author Commented:
b is the list "20, 10, null".        (b has value 20, with m_Next set to a that has value 10).

i did not understand above line. please advise
so this is example which is not implementing random access so it is slow right?

b has a  so to reach a through b requires more loop like that to go to deeper elements like 100 deep. so without randome access it is slow right.
please advise my understanding is correct on what you mentioned
0
dpearsonCommented:
so this is example which is not implementing random access so it is slow right?
Yes that's right.  You need to walk through the list one element at a time which is slower than jumping right to the element you need.

b has a  so to reach a through b requires more loop like that to go to deeper elements like 100 deep. so without randome access it is slow right.
Yes that's it.

please advise my understanding is correct on what you mentioned
I think you got it all.

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.