• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ryan Raina
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A possible option is to iterate through the list. Not very efficient, but then again, the indexOf() method uses loops too.

Henry
 
Ryan Raina
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do something like this:

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

 
Ryan Raina
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic