?
Solved

How do i find all objects which match a search cirteria in a binary search?

Posted on 2009-02-15
4
Medium Priority
?
177 Views
Last Modified: 2013-11-23
I am using a binary search (collections.binarySearch) to search through a collection for a certain object paratmeter string, now if it exists, it will only find 1 result, but what if there are more objects with that parameter string, how can i print them all?
0
Comment
Question by:area96
  • 2
4 Comments
 
LVL 6

Expert Comment

by:javaexperto
ID: 23645803
IF you know how many objects are int your list, you can call binarySearch method and then print the values using the index + 1... index + number of objects.

If you don't know get the index of your first and last object, for example:

 

      int s = al.indexOf("b"); //here al is an ArrayList
      int f = al.lastIndexOf("b");
        
        for(int i = s; i <= f; i++) {
            System.out.print(al.get(s));
        }

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 23646827
try subList()

Collections.binarySearch(list.subList(startIndex, endIndex), ....

0
 

Author Comment

by:area96
ID: 23649130
Thanks for the help guys, It hasnt entirely solved my problem though, as a binary search could drop you anywhere within the block of the array which matches the String that is being searched for, I would need to traverse back through the array until the string does not equal what im lookign for, then work forwards printing out every result..

Im so close I just need come help with the coding of this..

Either that or i havent fully understood your advice above.
Any more help would be greatly appreciated!  Thanks!
0
 
LVL 6

Accepted Solution

by:
javaexperto earned 1000 total points
ID: 23650282
What you want to do is an inneficient way to achieve your goal. Use the code I wrote above. If you really wanto to use binary search I give you the code.
al is your List and "c" is the String that you are looking for. Please note that if your String is in the beggining of the List or at the end, it will throw an index Exception. If it is the case you would never should use binarySearch.
Good luck

    	int index = Collections.binarySearch(al, "c");
    	int cont = index;
    	for(int i = 0; i < al.size(); i++) {
    		    		
    		if(al.get(index - i) == "c") {
    			cont--;
    		} else {
    			break;
    		}
    	}
    	for(int i = 0; i < al.size(); i++) {
    		if(al.get(cont + 1) == "c") {
    			System.out.print(al.get(cont + 1));
    			cont++;
    		}
    	}

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question