algorithm to detect index of objects

U

usgog

I have a List of Object A, B, C, D, E, F, G, .etc. I want to do the
following scenario (Java 5 preferred):
1. Revert the list.
2. Get the first appearance of Object D if any.
3. If there is any appearance of Object B before the first appearance
of Object D, return true. else, return false.

I am not sure what algorithms I should use in this case, especially 2
& 3...
 
J

jt

I have a List of Object A, B, C, D, E, F, G, .etc. I want to do the
following scenario (Java 5 preferred):
1. Revert the list.
2. Get the first appearance of Object D if any.
3. If there is any appearance of Object B before the first appearance
of Object D, return true. else, return false.

I am not sure what algorithms I should use in this case, especially 2
& 3...

Don't take my word for it, but it sounds to me like you want to use an
ArrayList.

Here's a link to the Java Docs for it

http://java.sun.com/javase/6/docs/api/index.html?java/util/ArrayList.html

You should be able to find most of what you need in there.
 
D

Daniel Pitts

I have a List of Object A, B, C, D, E, F, G, .etc. I want to do the
following scenario (Java 5 preferred):
1. Revert the list.
2. Get the first appearance of Object D if any.
3. If there is any appearance of Object B before the first appearance
of Object D, return true. else, return false.

I am not sure what algorithms I should use in this case, especially 2
& 3...

Do your own homework.
Hint: Look at java.util.List
 
U

usgog

Do your own homework.
Hint: Look at java.util.List

Sorry, actually it is Iterable<Object>. I know how to do it in
ArrayList or List but not Iterable...Please advise.
 
L

Lew

Sorry, actually it is Iterable<Object>. I know how to do it in
ArrayList or List but not Iterable...Please advise.

Do you know the for () loop?

What exactly is an Iterable?

You can iterate through a loop using an Iterator and getting the
iterator.next() the way a List would get the element at index i.

Instead of
for ( int i = 0; i < yourList.size(); ++i )

it would look more like
for ( Iterator <T> iter = yourIterable.iterator(); iter.hasNext(); )

There is also the new style for () that looks pretty much the same for either
idiom:

for ( T t : collection )

Have you tried actually reading your textbook and lecture notes for these
concepts?

Iterable and every other part of the Java API is documented at
<http://java.sun.com/javase/6/docs/api/>

Get to know the API docs. Get to love the API docs.
 
D

Daniel Pitts

Sorry, actually it is Iterable<Object>. I know how to do it in
ArrayList or List but not Iterable...Please advise.

Actually, if you only have an Iterable, then there is no concept of
order. For there to be a concept of order, you much either have a
SortedSet or a List

Please restate your question so that it makes sense... What exactly
are you trying to do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top