JAVA几类常见程序代码.doc_第1页
JAVA几类常见程序代码.doc_第2页
JAVA几类常见程序代码.doc_第3页
JAVA几类常见程序代码.doc_第4页
JAVA几类常见程序代码.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA几类常用算法代码1、查询数据库import java.sql.*;public class SearchDemoprivate String dbURL=jdbc:microsoft:sqlserver:/202.115.26.181:1433;/ 数据库标识名private String user=devon;/ 数据库用户private String password=book;/ 数据库用户密码public SearchDemo()tryClass.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver); /加载驱动器Connection con=DriverManager.getConnection(dbURL,user,password); /获取连接String sqlStr=select * from users where age20; /SQL查询语句Statement st=con.createStatement(); /获取Statement对象ResultSet rs=st.executeQuery(sqlStr); /执行查询String name,sex,email; /查询结果int age;while (rs.next() /遍历ResultSetname=rs.getString(name); /获取数据age=rs.getInt(age);sex=rs.getString(sex);email=rs.getString(email);System.out.println(Name: +name+tAge:+age+tSex:+sex+tEmail:+email); /在控制台输出数据con.close(); /关闭连接catch(Exception ex)ex.printStackTrace(); /输出出错信息public static void main(String args)new SearchDemo();2、 冒泡排序publicclassBubbleSort publicvoidsort(int a) inttemp =0; for(inti = a.length -1; i 0; -i) booleanisSort=false; for(intj =0; j i; +j) if(aj +1 aj) temp = aj; aj = aj +1; aj +1 = temp; isSort=true; if(!isSort)break; 3、 快速排序publicstaticvoidquickSort(inta,intstart,intend) inti,j; i = start; j = end; if(a=null)|(a.length=0) return; while(ij) while(ij&ai=aj) /以数组start下标的数据为key,右侧扫描 j-; if(ij) /右侧扫描,找出第一个比key小的,交换位置 inttemp = ai; ai = aj; aj = temp; while(ij&aiaj) /左侧扫描(此时aj中存储着key值) i+; if(i1) /递归调用,把key前面的完成排序 quickSort(a,0,i-1); if(end-j1) quickSort(a,j+1,end); /递归调用,把key后面的完成排序 4、 递归的二分查找 1 public static int binarySearch(int srcArray, int des) 2 3 int low = 0; 4 int high = srcArray.length-1; 5 while(low = high) 6 int middle = (low + high)/2; 7 if(des = srcArraymiddle) 8 return middle; 9 else if(des );this.next.print();elseSystem.out.print(this.data + n);public boolean search(String data) /内部搜索节点的方法if(this.data.equals(data) return true;if(this.next != null)return this.next.search(data);elsereturn false;public void delete(Node previous,String data) /内部删除节点的方法if(this.data.equals(data)previous.next = this.next ;elseif(this.next != null)this.next.delete(this,data);private Node root ; /定义头节点public void addNode(String data) /根据内容添加节点Node newNode = new Node(data) ; /要插入的节点if(this.root = null) /没有头节点,则要插入的节点为头节点this.root = newNode;else /如果有头节点,则调用节点类的方法自动增加this.root.add(newNode);public boolean searchNode(String data) /在链表中寻找指定内容的节点return root.search(data); /调用内部搜索节点的方法public void deleteNode(String data) /在链表中删除指定内容的节点if(root.data.equals(data) /如果是头节点if(root.next != null)root = root.next;elseroot =null;elseroot.next.delete(this.root,data);6、 查找最大值1 public class MaxMinTool 2 public static int getMax(int arr) 3 int max = Integer.MIN_VALUE; 4 5 for(int i = 0; i max) 7 max = arri; 8 9 10 return max; 7、十进制整数转换为r进制数1 byte dec2RNumber(int n,byte r) 2 if(n 0 | r 0) 3 throw new IllegalArgumentException( the parameter is valid!); 4 5 Stack s = new Stack(); 6 while( n != 0) 7 s.push

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论