Database Searching Algorithms
Hi..
I have to do some research about database searching algorithms.
Would someone here help me and give a list of that algorithms and which one is the best.
What is the name of this algorithms which when u type in a key such "a" then it display a list of information that started with "a".
I guess I have more time than I thought... :)
Here is a full working example all in one class...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MAIN
{
DefaultListModel model_my_list = new DefaultListModel();
JList my_list = new JList(model_my_list);
DefaultListModel model_list_of_finds = new DefaultListModel();
JList list_of_finds = new JList(model_list_of_finds);
JTextField search_box = new JTextField(20);
JTextField add_box = new JTextField(20);
public MAIN()
{
JFrame frame = new JFrame( "Search test" );
frame.setSize( 500, 500 );
//
Container container = frame.getContentPane();
GridBagLayout gbl = new GridBagLayout();
container.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
//
Action actionAdd = new AbstractAction("ADD")
{
public void actionPerformed(ActionEvent evt)
{
model_my_list.add( model_my_list.size(), add_box.getText() );
add_box.setText("");
}
};
JButton buttonAdd = new JButton(actionAdd);
buttonAdd.setPreferredSize( new Dimension( 100, 30 ) );
gbc.gridx = 2;
gbc.gridy = 2;
gbl.setConstraints(buttonAdd, gbc);
container.add( buttonAdd );
Action actionSearch = new AbstractAction("SEARCH")
{
public void actionPerformed(ActionEvent evt)
{
search( search_box.getText() );
}
};
JButton buttonSearch = new JButton(actionSearch);
buttonSearch.setPreferredSize( new Dimension( 100, 30 ) );
gbc.gridx = 2;
gbc.gridy = 4;
gbl.setConstraints(buttonSearch, gbc);
container.add( buttonSearch );
//
JLabel add_label = new JLabel( "Add element" );
gbc.gridx = 1;
gbc.gridy = 1;
gbl.setConstraints(add_label, gbc);
container.add( add_label );
gbc.gridx = 1;
gbc.gridy = 2;
gbl.setConstraints(add_box, gbc);
container.add( add_box );
JLabel search_label = new JLabel( "Search keyword" );
gbc.gridx = 1;
gbc.gridy = 3;
gbl.setConstraints(search_label, gbc);
container.add( search_label );
gbc.gridx = 1;
gbc.gridy = 4;
gbl.setConstraints(search_box, gbc);
container.add( search_box );
//
JLabel all_label = new JLabel( "All elements" );
gbc.gridx = 1;
gbc.gridy = 5;
gbl.setConstraints(all_label, gbc);
container.add( all_label );
JScrollPane all_scroll = new JScrollPane( my_list );
gbc.gridx = 1;
gbc.gridy = 6;
gbl.setConstraints(all_scroll, gbc);
container.add( all_scroll );
//
JLabel found_label = new JLabel( "Searched elements" );
gbc.gridx = 2;
gbc.gridy = 5;
gbl.setConstraints(found_label, gbc);
container.add( found_label );
JScrollPane found_scroll = new JScrollPane( list_of_finds );
gbc.gridx = 2;
gbc.gridy = 6;
gbl.setConstraints(found_scroll, gbc);
container.add( found_scroll );
//
frame.pack();
frame.setResizable( false );
frame.setVisible( true );
}
public void search( String searchWord )
{
model_list_of_finds.clear(); //empty
for ( int i = 0; i < model_my_list.size(); i++ )
{
String text = (String)model_my_list.get(i);
if ( text.startsWith( searchWord ) )
{
model_list_of_finds.add( model_list_of_finds.size(), text );
continue;
}
if ( text.endsWith( searchWord ) )
{
model_list_of_finds.add( model_list_of_finds.size(), text );
continue;
}
String keyword[] = text.split( searchWord );
if (keyword.length > 1 ) //if there was a split... there was a find
{
model_list_of_finds.add( model_list_of_finds.size(), text );
continue;
}
}
}
public static void main ( String args[] )
{
new MAIN();
}
}