作者:淘特网
注:转载请注明出处.
开发工具:Netbeans 4.1
说明:
此程序大体流程
1。打开一个含有多个重复记录的电话号码的文件。
2。系统按行读取文件并添加到向量vectors中,注意此时将电话号码转为LONG型。
3。新建一个LONG型数组,大小为vectors.size。并将vectors数据复制到这个数组中。
4。对上面的数组进行排序。
5。新建一个LONG型数组二,大小为vectors.size。
6。循环遍历数组一,并对比是否数组二中已有数据,如没有,则添加到数组二,否则跳过。
测试一万条记录的执行时间为不到2S。
/*
* Main.java
*
* Created on 2006年2月13日, 下午2:07
*/
package phoneutil;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author http://www.tot.name
*/
public class Main extends javax.swing.JFrame {
private Vector vectors=new Vector();
Long[] resultArr=null;
/** Creates new form Main */
public Main() {
initComponents();
this.setSize(405,400);
this.setTitle("电话号码查询");
this.setResizable(false);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
jFileChooser1 =new JFileChooser();
desktopPane = new javax.swing.JDesktopPane();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextArea2 = new javax.swing.JTextArea();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();
editMenu = new javax.swing.JMenu();
cutMenuItem = new javax.swing.JMenuItem();
copyMenuItem = new javax.swing.JMenuItem();
pasteMenuItem = new javax.swing.JMenuItem();
deleteMenuItem = new javax.swing.JMenuItem();
helpMenu = new javax.swing.JMenu();
contentMenuItem = new javax.swing.JMenuItem();
aboutMenuItem = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.BorderLayout());
jTextArea1.setColumns(35);
jTextArea1.setRows(15);
jTextArea1.setTabSize(4);
jScrollPane1.setViewportView(jTextArea1);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jTextArea2.setColumns(35);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
jPanel1.add(jScrollPane2, java.awt.BorderLayout.SOUTH);
jPanel1.setBounds(0, 0, 400, 280);
desktopPane.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
getContentPane().add(desktopPane, java.awt.BorderLayout.CENTER);
fileMenu.setText("文件");
fileMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileMenuActionPerformed(evt);
}
});
openMenuItem.setText("打开");
openMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openMenuItemActionPerformed(evt);
}
});
fileMenu.add(openMenuItem);
saveMenuItem.setText("保存");
saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveMenuItemActionPerformed(evt);
}
});
fileMenu.add(saveMenuItem);
//saveAsMenuItem.setText("Save As ...");
//fileMenu.add(saveAsMenuItem);
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
editMenu.setText("Edit");
cutMenuItem.setText("Cut");
editMenu.add(cutMenuItem);
copyMenuItem.setText("Copy");
editMenu.add(copyMenuItem);
pasteMenuItem.setText("Paste");
editMenu.add(pasteMenuItem);
deleteMenuItem.setText("Delete");
editMenu.add(deleteMenuItem);
menuBar.add(editMenu);
helpMenu.setText("Help");
contentMenuItem.setText("Contents");
helpMenu.add(contentMenuItem);
aboutMenuItem.setText("About");
aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutMenuItemActionPerformed(evt);
}
});
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
pack();
}
// </editor-fold>//GEN-END:initComponents
private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
JOptionPane.showMessageDialog(null,"Find Phone Num by repeated.","关于",JOptionPane.WARNING_MESSAGE);
}//GEN-LAST:event_aboutMenuItemActionPerformed
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
String strFileOpen="";
String lineStr=null;
String returnStr=null;
StringBuffer sbf=new StringBuffer();
StringBuffer bf2=new StringBuffer();
Long[] intArr=null;
//jFileChooser1 =new JFileChooser();
try{
if(this.jFileChooser1.APPROVE_OPTION==this.jFileChooser1.showOpenDialog(this)){
strFileOpen=this.jFileChooser1.getSelectedFile().getPath();
this.setTitle(strFileOpen);
try{
File files=new File(strFileOpen.toString());
vectors.removeAllElements();
InputStreamReader read =new InputStreamReader(new FileInputStream(files),"gb2312");
//BufferedReader br=new BufferedReader(new FileReader(filepath.toString()));
BufferedReader br=new BufferedReader(read);
while((lineStr=br.readLine())!=null){
//returnStr=returnStr+line+"\n\r";
//line=br.readLine();
//处理字符并添加到向量中
vectors.add(new Long(lineStr));
//sbf.append("\n\r");
}
intArr=new Long[vectors.size()];
resultArr=new Long[vectors.size()];
vectors.copyInto(intArr);
Arrays.sort(intArr);
int startIndex=1;
resultArr[0]=intArr[0];
//System.out.println(resultArr[0]+":"+intArr[0]);
for(int j=1;j<vectors.size();j++){
//System.out.println(resultArr[startIndex-1]+":"+intArr[j]);
if(resultArr[startIndex-1].compareTo(intArr[j])==0){
bf2.append(intArr[j]+"\n\r");
}
else{
sbf.append(intArr[j]+"\n\r");
resultArr[startIndex]=intArr[j];
startIndex++;
}
//startIndex++;
}
jTextArea1.setText("处理后的结果为:\n\r"+sbf.toString());
jTextArea2.setText("重复的电话号码有:\n\r"+bf2.toString());
}
catch(IOException e){
System.out.println("file open error"+e.getMessage());
}
//this.statubar.setText("Open File:"+this.filename);
}
else
{
return ;
}
}
catch(Exception e){
//this.statubar.setText("Error Open:"+e.getMessage());
}
}//GEN-LAST:event_openMenuItemActionPerformed
private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
File fileName=null;
String strFileOpen=null;
PrintWriter output=null;
try{
if(this.jFileChooser1.APPROVE_OPTION==this.jFileChooser1.showSaveDialog(this)){
strFileOpen=this.jFileChooser1.getSelectedFile().getPath();
this.setTitle(strFileOpen);
fileName = jFileChooser1.getSelectedFile();
this.setTitle("保存至:"+fileName.getName());
try{
output = new PrintWriter(fileName);
for(int i=0;i<resultArr.length;i++){
if(resultArr[i]!=null){
output.write(resultArr[i]+"\r\n");
}
}
jTextArea1.setText("成功保存至:\n\r"+strFileOpen);
}
catch(IOException e){
}
finally{
output.close();
output = null;
}
}
}
catch(Exception e){
}
}//GEN-LAST:event_openMenuItemActionPerformed
private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
// TODO 将在此处添加您的处理代码:
}//GEN-LAST:event_fileMenuActionPerformed
private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
System.exit(0);
}//GEN-LAST:event_exitMenuItemActionPerformed
//交换位置
private void swap(int loc1, int loc2){
Object tmp = vectors.elementAt(loc1);
vectors.setElementAt(vectors.elementAt(loc2),loc1);
vectors.setElementAt(tmp,loc2);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
private javax.swing.JFileChooser jFileChooser1;
// 变量声明 - 不进行修改//GEN-BEGIN:variables
protected javax.swing.JMenuItem aboutMenuItem;
protected javax.swing.JMenuItem contentMenuItem;
protected javax.swing.JMenuItem copyMenuItem;
protected javax.swing.JMenuItem cutMenuItem;
protected javax.swing.JMenuItem deleteMenuItem;
protected javax.swing.JDesktopPane desktopPane;
protected javax.swing.JMenu editMenu;
protected javax.swing.JMenuItem exitMenuItem;
protected javax.swing.JMenu fileMenu;
protected javax.swing.JMenu helpMenu;
protected javax.swing.JPanel jPanel1;
protected javax.swing.JScrollPane jScrollPane1;
protected javax.swing.JScrollPane jScrollPane2;
protected javax.swing.JTextArea jTextArea1;
protected javax.swing.JTextArea jTextArea2;
protected javax.swing.JMenuBar menuBar;
protected javax.swing.JMenuItem openMenuItem;
protected javax.swing.JMenuItem pasteMenuItem;
protected javax.swing.JMenuItem saveAsMenuItem;
protected javax.swing.JMenuItem saveMenuItem;
// 变量声明结束//GEN-END:variables
}
附图: