• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Using an ArrayList

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was wondering about a method of ArrayList. I have an ArrayList saved in my session and I want to search through the list for a specific element. Would using lastIndexOf() work? Would it actually search element by element until it found a match?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes -- from the end.
 
Jenn Person
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok perfect, one last question then. I know it returns an integer representing the element where a match was made in the arraylist. What is the result is no match is found?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jenn Person:
... What is the result is no match is found?


"...returns -1 if the object is not found."

See the documentation:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html
 
Jenn Person
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot!! I really appreciate your help guys!!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are links to Sun's documentation. These are essential resources, and most of your answers can be found here.

1.4.2:
http://java.sun.com/j2se/1.4.2/docs/api/index.html

1.5.0:
http://java.sun.com/j2se/1.5.0/docs/api/index.html
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jenn,

As you learn Java, you should spend some time learning how to navigate the Java API docs for the version of Java you are using. Notice that several of the replies above gave links directly to the documentation for the ArrayList class. If you know how to navigate the API docs, you can easily find what a method returns in certain conditions. The docs also describe how each method work, what exceptions are thrown, and what parameters a method takes. As you can see the answers you find there will save a lot of time since you won't have to wait for us here at the Ranch to answer it for you. Besides, we often have to look at the docs ourselves to find the answers.

I strongly sugest you take the time to learn about the API docs. They will help immensely as you continue to learn how to program in Java.

HTH

Layne
 
Dinner will be steamed monkey heads with a side of tiny ads.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic