java 实现三角形平行四边形等

记得刚开始学习编程的时候,有段时间一直在练习三角形平行四边形这些;
但是这些东西过段时间就会逻辑不清晰,今天回顾了一下,顺便做个记录.
System.out.println("直角在左下的等腰直角三角形");
        for (int i = 1; i <= 9; i++) {
            String row = "";
            for (int j = 1; j <= i; j++) {
               row += " * "; 
            }
             System.out.println(row); 
            } 
System.out.println("--------------------------------------------------------------"); 
            System.out.println("直角在左上的等腰直角三角形"); 
            for (int i = 9; i >= 1; i--) {
            String row = "";
            for (int j = 1; j <= i; j++) {
                row += " * "; 
            } 
            System.out.println(row); 
            } 
System.out.println("--------------------------------------------------------------"); 
            System.out.println("直角在右上的等腰直角三角形"); 
            for (int i = 9; i >= 1; i--) {
            String row = "";
            for (int ij = 9; ij > i; ij--) {
                row += "   ";
            }
            for (int j = 1; j <= i; j++) {
                row += " * ";
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("直角在右下的等腰直角三角形");
        for (int i = 1; i <= 9; i++) { 
           String row = ""; 
          for (int ij = 9; ij > i; ij--) {
                row += "   ";
            }
            for (int j = 1; j <= i; j++) {
                row += " * ";
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("底边在下的等腰三角形");
        for (int i = 1; i <= 7; i++) { 
            String row = ""; 
            for (int j = 7; j > i; j--) {
                row += "   ";
            }
            for (int ij = 1; ij <= (i * 2 - 1); ij++) {
                row += " * ";
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("等边三角形");
        for (int i = 1; i <= 6; i++) { 
           String row = ""; 
            for (int j = 6; j > i; j--) {
                row += " ";
            }
            for (int ij = 1; ij <= i; ij++) {
                if (ij % 2 != 0) {
                    row += "*";
                } else {
                    row += " * ";
                }
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("向右偏的平行四边形");
        for (int i = 1; i <= 6; i++) { 
            String row = ""; 
            for (int j = 6; j > i; j--) {
                row += "   ";
            }
            for (int ij = 1; ij <= 6; ij++) {
                row += " * ";
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");

        System.out.println("空心平行四边形");
        //上半部分逻辑
        for (int i = 1; i <= 6; i++) { 
         String row = ""; 
        //左线前的空格逻辑 
         for (int j = 6; j > i; j--) {
                row += "   ";
            }
            //左线逻辑
            for (int ij = 1; ij < 2; ij++) {
                row += " * ";
            }
            //中心逻辑
            for (int ij2 = 1; ij2 < (i * 2 - 2); ij2++) {
                row += "   ";
            }
            //右线逻辑
            for (int ij = 1; ij < 2; ij++) {
                if (i != 1) {
                    row += " * ";
                }
            }
            System.out.println(row);
        }
        //下半部分逻辑
        for (int i = 1; i <= 5; i++) {
            String row = "";
            //左线前空格逻辑
            for (int j = 1; j <= i; j++) {
                row += "   ";
            }
            //左线逻辑
            for (int j = 1; j < 2; j++) {
                row += " * ";
            }
            //中间空白逻辑
            for (int ij = 1; ij < (5 - i) * 2; ij++) {
                row += "   ";
            }
            //右线逻辑
            for (int j = 1; j < 2; j++) {
                if (i != 5) {
                    row += " * ";
                }
            }
            System.out.println(row);
        }
        System.out.println("--------------------------------------------------------------");
测试结果如下:
直角在左下的等腰直角三角形
 * 
 * * 
 * * * 
 * * * * 
 * * * * * 
 * * * * * * 
 * * * * * * * 
 * * * * * * * * 
 * * * * * * * * * 
--------------------------------------------------------------
直角在左上的等腰直角三角形
 * * * * * * * * * 
 * * * * * * * * 
 * * * * * * * 
 * * * * * * 
 * * * * * 
 * * * * 
 * * * 
 * * 
 * 
--------------------------------------------------------------
直角在右上的等腰直角三角形
 * * * * * * * * * 
 * * * * * * * * 
 * * * * * * * 
 * * * * * * 
 * * * * * 
 * * * * 
 * * * 
 * * 
 * 
--------------------------------------------------------------
直角在右下的等腰直角三角形
 * 
 * * 
 * * * 
 * * * * 
 * * * * * 
 * * * * * * 
 * * * * * * * 
 * * * * * * * * 
 * * * * * * * * * 
--------------------------------------------------------------
底边在下的等腰三角形
 * 
 * * * 
 * * * * * 
 * * * * * * * 
 * * * * * * * * * 
 * * * * * * * * * * * 
 * * * * * * * * * * * * * 
--------------------------------------------------------------
等边三角形
     *
    * * 
   * * *
  * * * * 
 * * * * *
* * * * * *  
--------------------------------------------------------------
向右偏的平行四边形
           * * * * * * 
         * * * * * * 
       * * * * * * 
     * * * * * * 
   * * * * * * 
 * * * * * * 
--------------------------------------------------------------
空心平行四边形
           * 
         *   * 
       *       * 
     *           * 
   *               * 
 *                   * 
   *               * 
     *           * 
       *       * 
         *   * 
           * 
--------------------------------------------------------------

Spring提供的JdbcTemplate简单使用

所需maven配置

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-tx</artifactId>
 <version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>6.0.6</version>
</dependency>
// JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
         DriverManagerDataSource dataSource = new DriverManagerDataSource();
         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
         dataSource.setUrl("jdbc:mysql://localhost:3306/spring");
         dataSource.setUsername("root");
         dataSource.setPassword("123456");
 
         // 创建JDBC模板
         JdbcTemplate jdbcTemplate = new JdbcTemplate();
         // 这里也可以使用构造方法
         jdbcTemplate.setDataSource(dataSource);
 
         // sql语句
         String sql = "select count(*)  from user";
         Long num = (long) jdbcTemplate.queryForObject(sql, Long.class);
 
         System.out.println(num);

Java对JDBC结果集ResultSet的处理

代码中的Json及JsonList为本人重新创建的类,参考代码时可转为自己常用的相应类
/**
     * 将结果集转化为json对象
     *
     * @param resultSet 操作数据库后的结果集
     * @return json类型的结果集
     * @throws SQLException sql异常
     */
    protected static Json getResultSetByJson(ResultSet resultSet) throws SQLException {
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        Json json = new Json();
        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i++) {
                String key = resultSetMetaData.getColumnLabel(i);
                String value = resultSet.getString(key);
                json.put(key, value);
            }
        }
        return json;
    }

    /**
     * 将结果集转化为JosnList对象
     *
     * @param resultSet 结果集
     * @return jsonlist类型的结果集
     * @throws SQLException sql异常
     */
    protected static JsonList getResultSetByJsonList(ResultSet resultSet) throws SQLException {
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        JsonList jsonList = new JsonList();
        while (resultSet.next()) {
            Json json = new Json();
            for (int i = 1; i <= columnCount; i++) {
                String key = resultSetMetaData.getColumnLabel(i);
                String value = resultSet.getString(key);
                json.put(key, value);
            }
            jsonList.add(json);
        }
        return jsonList;
    }

    /**
     * 将结果集映射为实体类
     *
     * @param resultSet 结果集
     * @param t         实体
     * @param <T>       泛型
     * @return 实体类型的结果集
     * @throws SQLException              sql异常
     * @throws InvocationTargetException 反射异常
     * @throws IllegalAccessException    反射时访问私有成员权限不足导致的异常
     * @throws NoSuchMethodException     得不到方法异常
     */
    protected static <T> T getResultSetByT(ResultSet resultSet, T t) throws SQLException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        //获取method方法
        Method[] methods = t.getClass().getMethods();

        //获取ResultSet列名
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        String[] columnNames = new String[columnCount];
        for (int i = 0; i < columnCount; i++) {
            columnNames[i] = resultSetMetaData.getColumnLabel(i + 1);
        }
        //遍历ResultSet
        while (resultSet.next()) {
            for (int i = 0; i < columnNames.length; i++) {
                //取得set方法
                String setMethodName = "set" + columnNames[i];
                //遍历method方法
                for (int j = 0; j < methods.length; j++) {
                    if (methods[j].getName().equalsIgnoreCase(setMethodName)) {
                        setMethodName = methods[j].getName();
                        Object value = resultSet.getObject(columnNames[i]);

                        //通过set方法赋值到实体
                        try {
                            if (value != null) {
                                //实体字段类型和RsultSet一样时
                                Method method = t.getClass().getMethod(setMethodName, value.getClass());
                                method.invoke(t, value);
                            }
                        } catch (Exception e) {
                            //实体字段类型和ResultSet不一样时,转为String
                            Method method = t.getClass().getMethod(setMethodName, String.class);
                            method.invoke(t, value.toString());
                        }
                    }
                }
            }
        }
        return t;
    }

Java实现区块链小实例

通过本文,你将可以做到:

创建自己的区块链
理解 hash 函数是如何保持区块链的完整性的
如何创造并添加新的块
多个节点如何竞争生成块
通过浏览器来查看整个链
所有其他关于区块链的基础知识
但是,对于比如工作量证明算法(PoW)以及权益证明算法(PoS)
这类的共识算法文章中将不会涉及。同时为了让你更清楚得查看
区块链以及块的添加,我们将网络交互的过程简化了,关于 P2P 
网络比如“对等网络”等内容将在将来的文章中讲解。

让我们开始吧!

设置

我们假设你已经具备一点 Java 语言的开发经验,以及maven项目构建经验。
在安装和配置 Java 开发环境后之后,我们新建maven项目,在pom中增加一些依赖:
<!--超小型web框架-->
  <dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-core</artifactId>
    <version>${spark.version}</version>
  </dependency>
Spark-web Framework是一个基于jetty的超小型框架,我们用它来写http访问的请求处理。
 <dependency>
   <groupId>commons-codec</groupId>
   <artifactId>commons-codec</artifactId>
   <version>${commons.codec.version}</version>
 </dependency>
这个通用包拥有几乎所有加解密算法及常规操作
 <dependency>
 <groupId>com.google.code.gson</groupId>
 <artifactId>gson</artifactId>
 <version>${gson.version}</version>
 </dependency>
Google的json包,当然你可以使用你喜欢的其他json包。

最后,增加log相关的包
<!--log start-->
<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>${log4j.version}</version>
 </dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>${slf4j.version}</version>
 </dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>${slf4j.version}</version>
 </dependency>
<!-- log end -->
 相关版本属性设置
<properties>
<commons.codec.version>1.9</commons.codec.version>
<spark.version>2.6.0</spark.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<gson.version>2.8.2</gson.version>
</properties>

数据模型 read more

try-finally中return的执行先后顺序

关于try-finally中return谁先执行,刚开始只记得是一个面试题;
今天遇到了较类似的情况,于是写了个小的事例进行测试
public String getString() {
    try {
      System.out.println("try");
      return "try中return";
    } finally {
      System.out.println("finally");
      return "finally中return";
    }
}
以上是try和finally都有return的情况;
执行结果为:

try
finally
finally中return

如果注释掉finally中的return,执行结果为:

try
finally
try中return

总结:
无论try中是否有return语句,finally内的语句都会执行

Idea自定义类注解

1.点击左上角file找到Settings...并点击
2.搜索file and Code Templates
3.选中file选项卡,找到Class并点击,可看到当前生成类的model
4.在public class ..这行上面添加自己想要的注解model
下面是我的当前model
/**
 * 描述:
 *    ${DESCRIPTION}
 * @author ${USER}
 * @create ${YEAR}-${MONTH}-${DAY} ${TIME}
 */
由于本人方法注解一直使用系统默认样式,所以并没有对方法注解进行自定义处理

Java网络编程

package com.net;

import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JavaNet {

	/**
	 * 根据域名获取IP
	 * 
	 * @param realmName 域名
	 * @return IP
	 */
	public static String GetIP(String realmName) {
		InetAddress address;
		try {
			address = InetAddress.getByName(realmName);
		} catch (UnknownHostException e) {
			return null;
		}
		return address.getHostAddress();
	}

	/**
	 * 获取端口是否被占用
	 * 
	 * @param host IP
	 * @param port 端口号
	 * @return 端口的使用状态
	 */
	public static boolean PortIsUsed(String host, int port) {
		try {
			Socket socket = new Socket(host, port);
			return true;
		} catch (UnknownHostException e) {
		} catch (IOException e) {
		}
		return false;
	}

	/**
	 * 获取本机IP
	 * 
	 * @return 本机IP地址
	 * @throws UnknownHostException 异常
	 */
	public static String LocalHostAddress() throws UnknownHostException {
		InetAddress address = InetAddress.getLocalHost();
		return address.getHostAddress();
	}

	/**
	 * 获取本机的主机名
	 * 
	 * @return 主机名
	 * @throws UnknownHostException 异常
	 */
	public static String LocalHostName() throws UnknownHostException {
		InetAddress address = InetAddress.getLocalHost();
		return address.getHostName();
	}

	/**
	 * 获取远程文件大小
	 * 
	 * @param fileLocal 文件路径
	 * @return 文件大小,单位为bytes
	 * @throws IOException 异常
	 */
	public static int getHostFileSize(String fileLocal) throws IOException {
		int size;
		URL url = new URL(fileLocal);
		URLConnection conn = url.openConnection();
		size = conn.getContentLength();
		conn.getInputStream().close();
		return size;
	}

	/**
	 * 获取文件的最后修改日期
	 * 
	 * @param fileLocal 文件路径
	 * @return 最后修改日期
	 * @throws IOException 异常
	 */
	public static Date getFileLastEditDate(String fileLocal) throws IOException {
		URL url = new URL(fileLocal);
		URLConnection conn = url.openConnection();
		conn.setUseCaches(false);
		long timestamp = conn.getLastModified();
		conn.getInputStream().close();
		Date date = new Date(timestamp);
		return date;
	}

	/**
	 * 网页抓取
	 * 
	 * @param local 抓取的路径
	 * @param htmlName html名 可为空
	 * @return 抓取的结果
	 * @throws IOException 异常
	 */
	public static String getHtmlText(String local, String htmlName) throws IOException {
		if (!local.contentEquals("http://"))
			return "local error,like http://www.xxx.com";
		if (htmlName == null)
			htmlName = "data.html";
		URL url = new URL(local);
		BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
		BufferedWriter writer = new BufferedWriter(new FileWriter(htmlName));
		String line;
		StringBuffer stringBuffer = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			stringBuffer.append("\n" + line);
			writer.write(line);
			writer.newLine();
		}
		String result = stringBuffer.toString().replaceFirst("\n", "");
		reader.close();
		writer.close();
		return result;
	}

	/**
	 * 获取相应头信息
	 * 
	 * @param local 请求路径 携带http://
	 * @return 响应头信息
	 * @throws IOException 异常
	 */
	public static Map getHeaders(String local) throws IOException {
		URL url = new URL(local);
		URLConnection conn = url.openConnection();
		Map headers = conn.getHeaderFields();
		return headers;
	}

	/**
	 * 解析URL
	 * 
	 * @param local 请求路径
	 * @return url信息
	 * @throws MalformedURLException 异常
	 */
	public static Map getURLInfo(String local) throws MalformedURLException {
		URL url = new URL(local);
		Map map = new HashMap();
		map.put("url", url.toString());
		// 协议
		map.put("protocol", url.getProtocol());
		// 文件名
		map.put("file", url.getFile());
		// 主机
		map.put("host", url.getHost());
		// 路径
		map.put("path", url.getPath());
		// 端口号
		map.put("port", url.getPort());
		// 默认端口号
		map.put("defaultPort", url.getDefaultPort());
		return map;
	}

}