Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Linear Search with an ArrayList

807591Apr 14 2008 — edited Apr 14 2008
Hello everyone, got a quick question. I have an ArrayList that I am trying to search through and print what I find. For example here is my code:
public void read(Scanner in){
		while(in.hasNextLine()){
			// reads in lines from the file
			String l = in.nextLine();
			String n = in.nextLine();
			
			// adds items to the ArrayList
			
			byKey.add(new DisneyItem(l,n));
		}
		
		Collections.sort(byKey);
		}
byKey is my ArrayList, and it is taking in lines from a .txt file that has a location and a name, (l = location, n = name). Here is where I run into trouble, I need to run a Linear Search to search through the ArrayList by location(l), and the print the name(n). My Linear Search looks like this,
 	public String lookUp(String l){
			for(int i = 0; i < byKey.size(); i++){
			
				if(byKey.getKey() == l)
return byKey[i].getVale

}
}
I'm a little lost with getting my linear search set up, could anybody maybe point me in the right direction with what to do?

Thanks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 12 2008
Added on Apr 14 2008
7 comments
1,656 views