面试常用的代码片段

Posted i-tao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面试常用的代码片段相关的知识,希望对你有一定的参考价值。

package com.transaction.demo.jdbc;

import java.sql.*;
import java.util.ArrayList;

// JDBC
class JDBC {
    /*public static void main(String[] args) throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.65.142:3306/test","root","root");

        Statement statement = connection.createStatement();

        String sql = "select * from user";
        ResultSet resultSet = statement.executeQuery(sql);

        while(resultSet.next()){
            System.out.println(resultSet.getObject("id")+" "+resultSet.getObject("name")+" "+resultSet.getObject("password"));
        }

        resultSet.close();
        statement.close();
        connection.close();
    }*/

    public static void main(String[] args) throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.65.142:3306/test","root","root");
        connection.setAutoCommit(false);

        Statement statement =  connection.createStatement();
        int rows = statement.executeUpdate("insert into user(name,password) values(‘terry‘,‘terry‘)");

        connection.commit();

        System.out.println(rows);

    }
}
// 单例模式
class Singleton {
    private static Singleton instance = null;

    private Singleton() {}

    public static Singleton getInstance(){
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }

    private void sayHello(){
        System.out.println("hello");
    }

    public static void main(String[] args) {
        //单例模式
        Singleton single = getInstance();
        single.sayHello();

        Singleton single1 = new Singleton();
        single1.sayHello();
    }
}
// 多线程 新建->就绪->运行->阻塞->死亡
class ThreadAndRunnable extends Thread implements Runnable{
    @Override
    public void run() {
        System.out.println("线程4");
    }

    public static void main(String[] args) {
        //lambda
        Thread t1 = new Thread(() -> System.out.println(Thread.currentThread().getName()+" 线程1"));
        t1.start();

        //非lambda 匿名内部类
        Thread t2 = new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+" 线程2");
            }
        };
        t2.start();

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+" 线3");
            }
        });
        t3.start();
    }

    //常规
    /*public static void main(String[] args) {
        Thread t1 = new ThreadAndRunnable();
        t1.start();

        Runnable run = new ThreadAndRunnable();
        Thread t2 = new Thread(run);
        t2.start();
    }*/
}

//
class Stack{
    ArrayList<Object> list = new ArrayList<>();

    //入栈
    public void push(Object o){
        list.add(o);
    }
    //出栈
    public Object pop(){
        Object o = list.get(list.size() - 1);
        list.remove(o);
        return o;
    }
    //栈是否为空
    public boolean isEmpty(){
        return list.isEmpty();
    }
    //栈大小
    public int size(){
        return list.size();
    }
    //打印栈元素
    @Override
    public String toString(){
        return String.valueOf(list);
    }

    public static void main(String[] args) {
        //创建一个栈
        Stack stack = new Stack();
        //入栈
        for(int i=1;i<=10;i++){
            stack.push(i);
        }
        //出栈
        while(!stack.isEmpty()){
            System.out.println("栈:" + stack.toString() + "	栈大小为:" + stack.size() + "	出栈元素为:" + stack.pop());
        }
    }
}
//队列
class Queue{
    ArrayList<Object> list = new ArrayList<>();

    //入队
    public void in(Object o) {
        list.add(o);
    }

    //出队
    public Object out() {
        Object o = list.get(0);
        list.remove(o);
        return o;
    }

    //队是否为空
    public boolean isEmpty() {
        return list.isEmpty();
    }

    //队大小
    public int size() {
        return list.size();
    }

    //打印队元素
    @Override
    public String toString() {
        return String.valueOf(list);
    }

    public static void main(String[] args) {
        //创建一个队列
        Queue queue = new Queue();
        //入队列
        for(int i=1;i<=10;i++){
            queue.in(i);
        }
        //出队列
        while(!queue.isEmpty()){
            System.out.println("队:" + queue.toString() + "	队大小为:" + queue.size() + "	出队元素为:" + queue.out());
        }
    }
}
// B树
class BTree{
    public int data;
    public BTree left;
    public BTree rigth;

    public boolean hasLeft(){
        return left != null;
    }

    public boolean hasRigth(){
        return rigth != null;
    }

    public BTree(){}

    public static void main(String[] args) {
        BTree root = new BTree();
        root.data = 0;

        BTree node1 = new BTree();
        node1.data = 1;

        BTree node2 = new BTree();
        node2.data = 2;

        BTree node3 = new BTree();
        node3.data = 3;

        BTree node4 = new BTree();
        node4.data = 4;

        BTree node5 = new BTree();
        node5.data = 5;

        BTree node6 = new BTree();
        node6.data = 6;

        root.left = node1;
        root.rigth = node2;

        node1.left = node3;
        node1.rigth = node4;

        node2.left = node5;
        node2.rigth = node6;

        System.out.println("先序遍历二叉树:");
        queryFirst(root);
        System.out.println();

        System.out.println("中序遍历二叉树:");
        queryMiddle(root);
        System.out.println();

        System.out.println("后序遍历二叉树:");
        queryLast(root);
        System.out.println();
    }
    //先序遍历二叉树
    public static void queryFirst(BTree tree){
        if(tree == null){
            return;
        }
        System.out.print(tree.data+"	");
        if(tree.hasLeft()){
            queryFirst(tree.left);
        }
        if(tree.hasRigth()){
            queryFirst(tree.rigth);
        }
    }
    //中序遍历二叉树
    public static void queryMiddle(BTree tree){
        if(tree == null){
            return;
        }
        if(tree.hasLeft()){
            queryMiddle(tree.left);
        }
        System.out.print(tree.data+"	");
        if(tree.hasRigth()){
            queryMiddle(tree.rigth);
        }
    }
    //后序便利二叉树
    public static void queryLast(BTree tree){
        if(tree == null){
            return;
        }
        if(tree.hasLeft()){
            queryLast(tree.left);
        }
        if(tree.hasRigth()){
            queryLast(tree.rigth);
        }
        System.out.print(tree.data+"	");
    }
}
// 冒泡
class BubbleSort{
    public static void sort(int a[]){
        int temp = 0;
        for(int i = 0;i < a.length;i++){
            for(int j = i;j < a.length;j++){
                if(a[i] > a[j]){
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] a = {1,3,5,2,4,8,6,7,10,9};
        sort(a);

        for(int i = 0;i < a.length;i++){
            System.out.println(a[i]);
        }
    }
}

 

以上是关于面试常用的代码片段的主要内容,如果未能解决你的问题,请参考以下文章

常用python日期日志获取内容循环的代码片段

C#常用代码片段备忘

swift常用代码片段

IOS开发-OC学习-常用功能代码片段整理

# Java 常用代码片段

# Java 常用代码片段