笔记整理

1.冒泡排序:

 1 int[] arr = new int[]{12,43,212,11,23,55,1};
 2 for(int i=0; i<arr.length; i++) {
 3             
 4     for(int j=0; j<arr.length-i-1; j++) {
 5         if(arr[j]>arr[j+1]) {
 6             int temp = arr[j];
 7             arr[j] = arr[j+1];
 8             arr[j+1] = temp;
 9         }
10     }
11 }
12 System.out.println(Arrays.toString(arr));

2.创建数组的三种方式:

数据类型[ ]  数组名  =  new  数据类型[数组长度] 

数据类型[ ]  数组名  =  {数组0,数组1,数组2,...} 

数据类型[ ]  数组名  =  new  数据类型[ ] {数组0,数组1,数组2,...}

3.不能通过new方法怎么创建对象?

把构造方法私有化

4.单例模式在java语言中的两种构建方式:

  懒汉模式:(线程不安全,加上synchronized后线程安全)

 1 public class Singleton {
 2     private static Singleton instance;
 3     private Singleton (){}
 4  
 5     public static synchronized  Singleton getInstance() {
 6       if (instance == null) {
 7           instance = new Singleton();
 8       }
 9       return instance;
10     }
11 }

  饿汉模式:(线程安全)

1 public class Singleton {
2     private static Singleton instance = new Singleton();
3     private Singleton (){}
4     public static Singleton getInstance() {
5       return instance;
6     }
7 }

 5.JDBC的封装:

  (1)加载驱动类

class.forName("Driver类的路径");

  (2)创建连接

Connection connection = DriverManager.getConnection("url","username","password");

  (3)创建sql编辑器

PreparedStatement ps = connection.preparedStatement("sql语句");

  (4)执行并返回结果

ResultSet rs = ps.executeQuery( );

  (5)释放资源

rs.close( );
ps.close( );
connection.close( );

代码实现:

public static void main(String[] args) {
        try {
            //1. 加载驱动类
            Class.forName("Driver类的路径");
            //2. 创建连接
            Connection connection = DriverManager.getConnection
                    ("jdbc:mysql:///abc", "root", "root");
            //3. 创建sql编辑器
            PreparedStatement ps = connection.prepareStatement("sql语句");
            //4. 执行并返回结果
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                System.out.println(name+" "+sex+" "+age);
            }
            //5. 释放资源
            rs.close();
            ps.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
    }

 6.Demo4J解析xml文件:

  (1)读取当前的xml文件

SaxReader reader = New SaxReader();
Doucument doc = reader . read(文件的路径);

  (2)如果想获取元素第一步应该先获取根节点

Element el = doc .getRootElement();

  (3)获取元素名称的方法(无论根还是子元素)

元素对象.getName();

  (4)读取根元素下的子元素

List elList = 根元素对象.elements();

  (5)读取属性对象

Attribute  a = 元素对象.attribute();

  (6)获取元素属性值

String v = 属性对象.getValue();

  (7)获取当前元素(节点)的文本值

元素对象.getText();

7.工厂模式:

  (1)创建一个接口

public interface Shape {
   void draw();
}

  (2)创建实现接口的实体类

public class Circle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("我是Circle");
   }
}
public class Square implements Shape {
 
   @Override
   public void draw() {
      System.out.println("我是Square");
   }
}

  (3)创建一个工厂,生成实体类的对象

public class ShapeFactory {
    
   //使用 getShape 方法获取形状类型的对象
   public Shape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }        
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      return null;
   }
}

  (4)使用该工厂获取实体类的对象

public class FactoryPatternDemo {
 
   public static void main(String[] args) {
      ShapeFactory shapeFactory = new ShapeFactory();
 
      //获取 Circle 的对象,并调用它的 draw 方法
      Shape shape1 = shapeFactory.getShape("CIRCLE");
 
      //调用 Circle 的 draw 方法
      shape1.draw();
 //获取 Square 的对象,并调用它的 draw 方法
      Shape shape3 = shapeFactory.getShape("SQUARE");
 
      //调用 Square 的 draw 方法
      shape3.draw();
   }
}

  (5)输出结果

我是Circle
我是Square
原文地址:https://www.cnblogs.com/jihongtao/p/10485472.html