This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Getting value of all the indices of all objects  from an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Getting value of all the indices of all objects  from an arraylist" Watch "Getting value of all the indices of all objects  from an arraylist" New topic
Author

Getting value of all the indices of all objects from an arraylist

Ryan Raina
Greenhorn

Joined: Jun 16, 2010
Posts: 28
Hello everyone
A little help!
I have a scenario where i need to get the index values of some objects present in the array list and if i use indexof() method of arraylist class it returns me only the first found object index but i have duplicate values in it . So it only return me first object index every time but none other. kindly c the code below :-


I am passing x and c dynamically and there values are 10 and 5 respectively:- here is my output

values in list are :- 3032232144
size of array list is 10
item removed is 3 and it's position is 5
item now present on 5 position is 2
now size of array list in 9
values in list are :- 303222144
index of element is 0
total no of duplicated values are 2


*********it only returned index of first 3 present on second last line of output but no other 3's index and i know thet indexof() method returns only first index so is there any way or any other method call that can solve my problem.*************************************

thanks in advance


[HENRY: Added Code Tags]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

A possible option is to iterate through the list. Not very efficient, but then again, the indexOf() method uses loops too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ryan Raina
Greenhorn

Joined: Jun 16, 2010
Posts: 28
Henry Wong wrote:A possible option is to iterate through the list. Not very efficient, but then again, the indexOf() method uses loops too.

Henry



I am actually iterating the list and also i have moved this line in if statement of indexFind() method:- System.out.println("index of element is " + q); but output is :-
values in list are :- 3422310020
size of array list is 10
item removed is 1 and it's position is 5
item now present on 5 position is 0
now size of array list in 9
values in list are :- 342230020
index of element is 0
index of element is 0
total no of duplicated values are 2


index of element is still o for both 3 and it sholud be"
index of element is 0
index of element is 4
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
You can do something like this:

using a new temp variable that is incrementing for each iteration:



SCJP 6.0 (88%)
Ryan Raina
Greenhorn

Joined: Jun 16, 2010
Posts: 28
Dragos Nica wrote:You can do something like this:

using a new temp variable that is incrementing for each iteration:




thanks for your reply it worked



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting value of all the indices of all objects from an arraylist
 
Similar Threads
cannot insert into linkedlist after delete
More Array sorting trouble
I can NOT compile first class below
get fifth element from last for a Singly linked list