javase tutorials

This commit is contained in:
sxyx2008
2014-07-29 09:52:58 +08:00
parent 0a8d56b642
commit 80a677d653
5 changed files with 118 additions and 1 deletions
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.aimeizi</groupId>
<artifactId>maven-framework-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>javase-tutorials</artifactId>
<name>javase-tutorials</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>
@@ -0,0 +1,40 @@
package org.javase.tutorials;
/**
* 二分法查找
* @author welcome
*
*/
public class BinarySearch {
/**
* 二分法查找算法
* @param array 一维数组(充当数据结果集)
* @param key 目标元素(要查找的值)
* @return 没找到返回-1
*/
public static int binary(int[] array,int key){
int low = 0;
int high = array.length-1;
while(low <= high){
int middle = (low+high)/2;
if(key == array[middle]){//找到,返回下标索引
return middle;
}else if(key > array[middle]){
low = middle + 1;
}else if(key < array[middle]){
high = middle-1;
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(binary(new int[]{3, 5, 8, 13, 15, 16, 20, 27, 31, 56},20));
//Arrays.binarySearch(new int[]{3, 5, 8, 13, 15, 16, 20, 27, 31, 56},20);
}
}
@@ -0,0 +1,58 @@
package org.javase.tutorials;
/**
* 交换两个数的值,不借助于第三个数
* @author welcome
*
*/
public class SwapVar {
private static void swap1(int a,int b){
System.out.println("交换前a,b的值:"+a+","+b);
a=a+b-(b=a);
System.out.println("交换后a,b的值:"+a+","+b);
}
private static void swap2(int a,int b){
System.out.println("交换前a,b的值:"+a+","+b);
b=a+(a=b)*0;
System.out.println("交换后a,b的值:"+a+","+b);
}
private static void swap3(int a,int b){
System.out.println("交换前a,b的值:"+a+","+b);
a=a*b;
b=a/b;
a=a/b;
System.out.println("交换后a,b的值:"+a+","+b);
}
private static void swap4(int a,int b){
System.out.println("交换前a,b的值:"+a+","+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("交换后a,b的值:"+a+","+b);
}
private static void swap5(int a,int b){
System.out.println("交换前a,b的值:"+a+","+b);
a=a^b;
b=a^b;
a=a^b;
System.out.println("交换后a,b的值:"+a+","+b);
}
public static void main(String[] args) {
swap1(10, 20);
System.out.println("------------------------------");
swap2(30, 40);
System.out.println("------------------------------");
swap3(50, 60);
System.out.println("------------------------------");
swap4(70, 80);
System.out.println("------------------------------");
swap5(90, 100);
}
}
+1
View File
@@ -96,6 +96,7 @@
<module>spring-jdbc-tutorials</module>
<module>spring-data-example</module>
<module>spring-ibatis-example</module>
<module>javase-tutorials</module>
</modules>
<properties>
@@ -43,7 +43,6 @@ public class ProductDaoImpl implements ProductDao {
Object object = getSqlMapClient().insert("org.spring.ibatis.example.model.Product.save", product);
return object==null?false:true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;