aspose file tools*
The moose likes Beginning Java and the fly likes Find all indexes of target valuein an array Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Find all indexes of target valuein an array Java" Watch "Find all indexes of target valuein an array Java" New topic
Author

Find all indexes of target valuein an array Java

ryan wellhouser
Greenhorn

Joined: Apr 08, 2009
Posts: 2
Hi everyone.
I have an assignment which requires me to create a method that will search through an array for a target value and print all the indexes it occurs at in a new array. The parts im having the most trouble with is creating a new array to hold the values of the indexes that are found. Any ideas? Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The parts im having the most trouble with is creating a new array to hold the values of the indexes that are found.

Are you required to use an array, or can you use a java.lang.List instead ?


[My Blog]
All roads lead to JavaRanch
ryan wellhouser
Greenhorn

Joined: Apr 08, 2009
Posts: 2
Unfortunately you are required to create a separate array.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Do you really have to create a separate array? If the assignment is just to print the indexes that match, can you just iterate through the array and print out an index as you find equal values? Or you can build a String that holds the output, and print the String at the end of the method.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If you don't know what size the array is going to be, it will be annoying. You can still make an array whose size if as big as the input array. Otherwise, I think you'll have to loop twice, once to get the number of matching indexes, and another one to store the indexes. Can you figure this out ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find all indexes of target valuein an array Java