11 Replies - 14778 Views - Last Post: 01 March 2009 - 07:19 PM Rate Topic: -----

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

ArrayList search

Posted 01 March 2009 - 03:51 PM

Feeling drained today, cant think. Just wanted to ask if it looks like i got somthing simple right, cos i dont have an ide on me. Need to search an ArrayList. I get the input from a JTextField. The search i came up with just looks very primative to other searchesi have done. I dont want to sort the ArrayList so binary is no good here. Just some friendly suggestions would be nice. :D
Cheers
	private void searchBtn_actionPerformed() {
		String lName = lastNameTf.getText();
		passengers = air.getList();
		
			 for(Passenger a: passengers) {
			 	if(a.getLastName().equals(lName)) {
			 		
			 	}
			 }
	}


Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList search

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: ArrayList search

Posted 01 March 2009 - 03:56 PM

Looks like it will do what you want it to do, if the list isnt sorted then traversing the list is the only way to check if it contains the desired value.

Just make sure that,if it isn't a field, you give the passengers list a type.
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: ArrayList search

Posted 01 March 2009 - 04:16 PM

kk, looks good. And what would be the best way to delete the Passenger from another button? I have it so far like this
	private void searchBtn_actionPerformed() {
		
		String lName = lastNameTf.getText();
		passengers = air.getList();
		textArea.setText("");
		
			 for(Passenger a: passengers) {
			 	if(a.getLastName().equalsIgnoreCase(lName)) {
			 		JOptionPane.showMessageDialog(null, "Passenger Found");
			 		textArea.append(a.toString() + "\n");
			 		lastNameTf.setText("");
			 	}
			 	else {
			 		JOptionPane.showMessageDialog(null, "Passenger not found");
			 		lastNameTf.setText("");
			 	}
			 }
	}
	
	private void deleteBtn_actionPerformed() {
		
	}


So basically, it finds the Passenger and prints it to a JTextArea. Now because delete is an independant button, what would be the best way to delete the Passenger from the search result if requested?
Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: ArrayList search

Posted 01 March 2009 - 04:19 PM

What about using

Collections.sort(ArrayList);

??
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: ArrayList search

Posted 01 March 2009 - 04:23 PM

Dont really want the ArrayList sorted, otherwise i would just perform a basic binary search on it. Searching should be kool now anyways, just using a simple illiteration. Just thinking how i could get my Passenger from the search action event. I suppose i could get it to return a passenger but then this means my delete action event would need to invoke the search action event in order to get the passenger, which would be a bit of a mess.
Was This Post Helpful? 0
  • +
  • -

#6 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: ArrayList search

Posted 01 March 2009 - 04:27 PM

Quote

Just thinking how i could get my Passenger from the search action event. I suppose i could get it to return a passenger but then this means my delete action event would need to invoke the search action event in order to get the passenger, which would be a bit of a mess.


Well you could make a seperate search method and call it from the delete/search methods. Also you could return the index of the passenger in the list that way you can then delete this index or use this index to get the passenger to use as appropriate.
Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: ArrayList search

Posted 01 March 2009 - 06:08 PM

I have put the search in its own method. Not quite getting to grips with the returning of an index in an ArrayList. This is the method
private int searchMethod(String lastName) {
		
		String lName = lastName;
		passengers = air.getList();
		
		for(Passenger a: passengers) {
			 	if(a.getLastName().equalsIgnoreCase(lName)) {	
		 		//return a.get();
		 		}
		}
			 	
			 		
	}


Just not too sure what the return statement should be.
Was This Post Helpful? 0
  • +
  • -

#8 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: ArrayList search

Posted 01 March 2009 - 06:13 PM

If you want to return an index then better to use the normal for loop than the for each type for loop. Then you can just return the current loop variable when you find the object, or if not found return something like -1
Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: ArrayList search

Posted 01 March 2009 - 06:57 PM

I'm half dead! Cant think for myself
Ok, made a few changes, still having a few difficulties with my delete. So i have my search
private int searchMethod(String lastName) {
		
		String lName = lastName;
		passengers = air.getList();
 
			for(int x=0; x<passengers.size(); x++) {
				
				Passenger singPas = passengers.get(x);
				
				 	if(singPas.getLastName().equalsIgnoreCase(lName)) {	
			 		return x;
			 		}
			}
			
		return -1;	 	
			 		
	}




And then i use this in my search action performed method
private void searchBtn_actionPerformed() {
		
		String lName = lastNameTf.getText();
		textArea.setText("");
		int result = searchMethod(lName);
		
			 if(result==-1) {
				JOptionPane.showMessageDialog(null, "Passenger not found");
				lastNameTf.setText("");
			 } 
			 else {
		 		JOptionPane.showMessageDialog(null, "Passenger Found");
		 		textArea.append(passengers.get(result).toString() + "\n");
		 		lastNameTf.setText(""); 	
			}
			 
	}



Now for my delete, i did try making my result variable a class variable, and then done this
private void deleteBtn_actionPerformed() {
		
		if(result == 0) {
			JOptionPane.showMessageDialog(null, "Please search for a Passenger");
		}
		else {
			passengers.remove(result);
		}
	}

But it didnt seem to work. I dont know if that is the right approach or if somthing is wrong in the coding? Any advise would be great.
cheers
Was This Post Helpful? 0
  • +
  • -

#10 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: ArrayList search

Posted 01 March 2009 - 07:10 PM

In the delete method just make another call to the search method to get the index, unless you mean for the person to always perform a search before they delete.
Was This Post Helpful? 0
  • +
  • -

#11 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: ArrayList search

Posted 01 March 2009 - 07:14 PM

Yeah, the person has to perform the search before the delete. Thats whats causing the problem.
Was This Post Helpful? 0
  • +
  • -

#12 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: ArrayList search

Posted 01 March 2009 - 07:19 PM

So in your code are you going to have the delete button disabled until the search button is clicked? Or else this requirement will cause problems.

If so then just create a field to store the result of the search,

Which is what you do, try checking against -1 as 0 is still a valid index.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1