Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 7th, 2004, 4:38 PM   #1
Infyniti
Newbie
 
Join Date: Dec 2004
Posts: 5
Rep Power: 0 Infyniti is on a distinguished road
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.
Infyniti is offline   Reply With Quote
Old Dec 7th, 2004, 6:15 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Old Dec 8th, 2004, 11:44 AM   #3
Infyniti
Newbie
 
Join Date: Dec 2004
Posts: 5
Rep Power: 0 Infyniti is on a distinguished road
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
Infyniti is offline   Reply With Quote
Old Dec 9th, 2004, 7:50 AM   #4
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
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...
ZenMasterJG is offline   Reply With Quote
Old Dec 9th, 2004, 9:10 AM   #5
Infyniti
Newbie
 
Join Date: Dec 2004
Posts: 5
Rep Power: 0 Infyniti is on a distinguished road
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
Infyniti is offline   Reply With Quote
Old Dec 9th, 2004, 1:18 PM   #6
Infyniti
Newbie
 
Join Date: Dec 2004
Posts: 5
Rep Power: 0 Infyniti is on a distinguished road
I am done with the code.... and thanku for the help.

infyniti
Infyniti is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC