Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

algorithm to detect index of objects

Thread view: 
usgog@yahoo.com - 07 Apr 2007 01:51 GMT
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...
jt - 07 Apr 2007 02:53 GMT
> I have a List of Object A, B, C, D, E, F, G, .etc. I want to do the
> following scenario (Java 5 preferred):
[quoted text clipped - 5 lines]
> 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.

Signature

There are 10 types of people in this world.  Those who understand binary
and those who don't.

usgog@yahoo.com - 07 Apr 2007 04:27 GMT
> u...@yahoo.com wrote:
> > I have a List of Object A, B, C, D, E, F, G, .etc. I want to do the
[quoted text clipped - 19 lines]
> There are 10 types of people in this world.  Those who understand binary
> and those who don't.

Sorry, actually it is Iterable<Object>. I know how to do it in
ArrayList or List but not Iterable...Please advise.
Daniel Pitts - 07 Apr 2007 04:04 GMT
On Apr 6, 5:51 pm, "u...@yahoo.com" <u...@yahoo.com> wrote:
> I have a List of Object A, B, C, D, E, F, G, .etc. I want to do the
> following scenario (Java 5 preferred):
[quoted text clipped - 5 lines]
> 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
usgog@yahoo.com - 07 Apr 2007 04:28 GMT
> On Apr 6, 5:51 pm, "u...@yahoo.com" <u...@yahoo.com> wrote:
>
[quoted text clipped - 10 lines]
> 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.
Luc The Perverse - 07 Apr 2007 06:39 GMT
>> On Apr 6, 5:51 pm, "u...@yahoo.com" <u...@yahoo.com> wrote:
>>
[quoted text clipped - 13 lines]
> Sorry, actually it is Iterable<Object>. I know how to do it in
> ArrayList or List but not Iterable...Please advise.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Iterable.html

If you know how to do it in an ArrayList or a List then you know how to do
it in its generic class :)

Notice that the functionality that you are familiar with comes from Iterable
class.  The only difference is that you do not know how it is stored.

--
LTP

:)
Lew - 07 Apr 2007 13:02 GMT
> 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.

Signature

Lew

Daniel Pitts - 07 Apr 2007 22:32 GMT
On Apr 6, 8:28 pm, "u...@yahoo.com" <u...@yahoo.com> wrote:

> > On Apr 6, 5:51 pm, "u...@yahoo.com" <u...@yahoo.com> wrote:
>
[quoted text clipped - 13 lines]
> 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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.