![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2004
Posts: 5
Rep Power: 0
![]() |
I have two files with ordered list of names and i have to copy them to array list which i have done already. I have use binary search to compare them.
Can anybody suggest me any online information how I can use binarysearch on strings in arraylist. thanks in advance. |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
What exactly do you need help with?
You could check the javadocs for arraylists, though, if you need some help. Also, check the collections class, which has some useful sort methods if you haven't already. Wouldn't a binary search on an arraylist be similar to a binary search for an array?
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2004
Posts: 5
Rep Power: 0
![]() |
Arraylist1= m1,m2,m3,m4,m5
arraylist2= m6 m5,m1,m7,m2,m8 This is the pseudo-code for my algorithm Let p refer to first item in list 1, and r refer to first item in list 2 while more elements in list 1 look for item at position p of list1 at or after position r in list 2 if item found at or after position r in list 2 set r to that position increment P else increment R look for item at position p before position r in list 2 if found set r to that position advance p to next position I have to do this using binarysearch...but for binarysearch the elments have to be sorted. If i sort the positions of elements gets changed . Can u suggest where i am going wrong....is that possible to do using binarysearch. just an idea will be helpful regards infyniti |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
Yes, to use a binary search your lists must be sorted. Why is it a problem that they'll be at different indexes in the arraylist? I just don't really understand what your problem is...
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2004
Posts: 5
Rep Power: 0
![]() |
Thanks for the reply...I am posting my code for the algorthim provided in my previous positng using linkedlist nothing related to binaryserch. I am unable to figure out where i am going wrong...can u help me out
The linked list contains listA= m1,m2,m3,m4,m5 listB= m6,m5,m1,m7,m2,m8 package com.my.test; import java.io.BufferedReader; import java.io.FileReader; import java.util.LinkedList; import java.util.List; class Linkedlist { public static void main(String args[]) throws Exception { long startTime = System.currentTimeMillis(); FileReader frListB = new FileReader("C:/Documents and Settings/Anjana/My Documents/doc/Second.txt"); BufferedReader brListB = new BufferedReader(frListB); FileReader frListA = new FileReader("C:/Documents and Settings/Anjana/My Documents/doc/First.txt"); BufferedReader brListA = new BufferedReader(frListA); List listA = new LinkedList(); List listB = new LinkedList(); String sListB; String sListA; int position = 0; boolean flag = false; //Copying the contents from second.txt into an LinkedList while ((sListB = brListB.readLine()) != null) { listB.add(sListB); } while ((sListA = brListA.readLine()) != null) { System.out.println("sListA =" + sListA); for (int i = position; i < listB.size(); i++) { System.out.println("listb.get(i)= " + listB.get(i)); if (sListA.equals(listB.get(i))) { position = i + 1; System.out.println("position =" + position); flag = false; break; } else { flag = true; } } if (flag == true) { System.out.println("sListA =" + sListA); for (int i = position-1 ; i>=0; i--) { System.out.println("second listb.get(i)= " + listB.get(i)); if (sListA.equals(listB.get(i))) { position = i - 1; } else { flag = false; } } } } } }Thanks in advance Infyniti |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Dec 2004
Posts: 5
Rep Power: 0
![]() |
I am done with the code.... and thanku for the help.
infyniti |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|