• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using an ArrayList  RSS feed

 
Jenn Person
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes -- from the end.
 
Jenn Person
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks alot!! I really appreciate your help guys!!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!