1.冒泡排序
package jichu;
public class Maopao {
public static void main(String[] args) {
int a[] = { 1, 3, 6, 7, 4, 8 };
int temp = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
2.简单的单例模式
package jichu;
//饿汉式
public class Singleton1{
private Singleton1(){}
private static final Singleton1 single=new Singleton1();
public static Singleton1 getInstance(){
return single;
}
}
//懒汉式
public class Singleton2{
private Singleton2(){}
private static Singleton2 single=null;
public synchronized static Singleton2 getInstance(){
if(single==null){
single=new Singleton2();
}
return single;
}
}’
3.打印菱形
package jichu;
import java.util.Scanner;
public class Dayinglinxin {
//打印菱形
public static void main(String[] args) {
System.out.println("请输入行数:");
Scanner scanner = new Scanner(System.in);
int mun = scanner.nextInt();
if (mun % 2 == 0) {
System.out.println("输入错误/n");
} else {
for (int i = 1; i <= (mun / 2 + 1); i++) {
for (int j = 1; j <= (mun / 2 + 1) - i; j++) {
System.out.print(" ");
}
for (int j = 1; j < 2 * i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = (mun + 1) / 2; i <= mun; i++) {
for (int j = 1; j < i - (mun / 2 - 1); j++) {
System.out.print(" ");
}
for (int j = 1; j <= (mun - i) * 2 - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
4.手写jdbc
package jichu;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Jdbc {
private static final String DRIVIVE="";
private static final String URL="";
private static final String user="";
private static final String pwd="";
public static Connection getConnection(){
Connection connection =null;
try {
Class.forName(DRIVIVE);
connection=DriverManager.getConnection(URL, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(Connection connection,Statement statement){
if(null!=statement){
try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(null!=connection){
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
5.获取当前时间
package jichu;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Huoqudangqianshijian{
public static void main (String[] args){
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(date));
}
}