Kaynağa Gözat

*currentTime 精确到纳秒
*ms 精确到毫秒
*Test 时间戳测试类

UI 2 yıl önce
ebeveyn
işleme
7a4c1b46b7

+ 0 - 204
src/com/example/ReversePolishMultiCalc.java

@@ -1,204 +0,0 @@
-package com.example;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Stack;
-import java.util.regex.Pattern;
-
-public class ReversePolishMultiCalc {
-
-	 /**
-     * 匹配 + - * / ( ) 运算符
-     */
-    static final String SYMBOL = "\\+|-|\\*|/|\\(|\\)";
-
-    static final String LEFT = "(";
-    static final String RIGHT = ")";
-    static final String ADD = "+";
-    static final String MINUS= "-";
-    static final String TIMES = "*";
-    static final String DIVISION = "/";
-
-    /**
-     * 加減 + -
-     */
-    static final int LEVEL_01 = 1;
-    /**
-     * 乘除 * /
-     */
-    static final int LEVEL_02 = 2;
-
-    /**
-     * 括号
-     */
-    static final int LEVEL_HIGH = Integer.MAX_VALUE;
-
-
-    static Stack<String> stack = new Stack<>();
-    static List<String> data = Collections.synchronizedList(new ArrayList<String>());
-
-    /**
-     * 去除所有空白符
-     * @param s
-     * @return
-     */
-    public static String replaceAllBlank(String s ){
-        // \\s+ 匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]
-        return s.replaceAll("\\s+","");
-    }
-
-    /**
-     * 判断是不是数字 int double long float
-     * @param s
-     * @return
-     */
-    public static boolean isNumber(String s){
-        Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
-        return pattern.matcher(s).matches();
-    }
-
-    /**
-     * 判断是不是运算符
-     * @param s
-     * @return
-     */
-    public static boolean isSymbol(String s){
-        return s.matches(SYMBOL);
-    }
-
-    /**
-     * 匹配运算等级
-     * @param s
-     * @return
-     */
-    public static int calcLevel(String s){
-        if("+".equals(s) || "-".equals(s)){
-            return LEVEL_01;
-        } else if("*".equals(s) || "/".equals(s)){
-            return LEVEL_02;
-        }
-        return LEVEL_HIGH;
-    }
-
-    /**
-     * 匹配
-     * @param s
-     * @throws Exception
-     */
-    public static List<String> doMatch (String s) throws Exception{
-        if(s == null || "".equals(s.trim())) throw new RuntimeException("data is empty");
-        if(!isNumber(s.charAt(0)+"")) throw new RuntimeException("data illeagle,start not with a number");
-
-        s = replaceAllBlank(s);
-
-        String each;
-        int start = 0;
-
-        for (int i = 0; i < s.length(); i++) {
-            if(isSymbol(s.charAt(i)+"")){
-                each = s.charAt(i)+"";
-                //栈为空,(操作符,或者 操作符优先级大于栈顶优先级 && 操作符优先级不是( )的优先级 及是 ) 不能直接入栈
-                if(stack.isEmpty() || LEFT.equals(each)
-                        || ((calcLevel(each) > calcLevel(stack.peek())) && calcLevel(each) < LEVEL_HIGH)){
-                    stack.push(each);
-                }else if( !stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek())){
-                    //栈非空,操作符优先级小于等于栈顶优先级时出栈入列,直到栈为空,或者遇到了(,最后操作符入栈
-                    while (!stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek()) ){
-                        if(calcLevel(stack.peek()) == LEVEL_HIGH){
-                            break;
-                        }
-                        data.add(stack.pop());
-                    }
-                    stack.push(each);
-                }else if(RIGHT.equals(each)){
-                    // ) 操作符,依次出栈入列直到空栈或者遇到了第一个)操作符,此时)出栈
-                    while (!stack.isEmpty() && LEVEL_HIGH >= calcLevel(stack.peek())){
-                        if(LEVEL_HIGH == calcLevel(stack.peek())){
-                            stack.pop();
-                            break;
-                        }
-                        data.add(stack.pop());
-                    }
-                }
-                start = i ;    //前一个运算符的位置
-            }else if( i == s.length()-1 || isSymbol(s.charAt(i+1)+"") ){
-                each = start == 0 ? s.substring(start,i+1) : s.substring(start+1,i+1);
-                if(isNumber(each)) {
-                    data.add(each);
-                    continue;
-                }
-                throw new RuntimeException("data not match number");
-            }
-        }
-        //如果栈里还有元素,此时元素需要依次出栈入列,可以想象栈里剩下栈顶为/,栈底为+,应该依次出栈入列,可以直接翻转整个stack 添加到队列
-        Collections.reverse(stack);
-        data.addAll(new ArrayList<>(stack));
-
-        System.out.println(data);
-        return data;
-    }
-
-    /**
-     * 算出结果
-     * @param list
-     * @return
-     */
-    public static Double doCalc(List<String> list){
-        Double d = 0d;
-        if(list == null || list.isEmpty()){
-            return null;
-        }
-        if (list.size() == 1){
-            System.out.println(list);
-            d = Double.valueOf(list.get(0));
-            return d;
-        }
-        ArrayList<String> list1 = new ArrayList<>();
-        for (int i = 0; i < list.size(); i++) {
-            list1.add(list.get(i));
-            if(isSymbol(list.get(i))){
-                Double d1 = doTheMath(list.get(i - 2), list.get(i - 1), list.get(i));
-                list1.remove(i);
-                list1.remove(i-1);
-                list1.set(i-2,d1+"");
-                list1.addAll(list.subList(i+1,list.size()));
-                break;
-            }
-        }
-        doCalc(list1);
-        return d;
-    }
-
-    /**
-     * 运算
-     * @param s1
-     * @param s2
-     * @param symbol
-     * @return
-     */
-    public static Double doTheMath(String s1,String s2,String symbol){
-        Double result ;
-        switch (symbol){
-            case ADD : result = Double.valueOf(s1) + Double.valueOf(s2); break;
-            case MINUS : result = Double.valueOf(s1) - Double.valueOf(s2); break;
-            case TIMES : result = Double.valueOf(s1) * Double.valueOf(s2); break;
-            case DIVISION : result = Double.valueOf(s1) / Double.valueOf(s2); break;
-            default : result = null;
-        }
-        return result;
-
-    }
-
-    public static void main(String[] args) {
-        String math = "9+(3-1)*3+10/2";
-        //String math = "12.8 + (2 - 3.55)*4+10/5.0";
-        try {
-            doCalc(doMatch(math));
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-}
-

+ 0 - 225
src/com/example/StackByArray.java

@@ -1,225 +0,0 @@
-package com.example;
-
-import java.util.EmptyStackException;
-
-public class StackByArray {
-    public static void main(String[] args) {
-        String exps = "12+8*9-9/3";
-        // 索引,用来读取字符串中的元素
-        StackByArray stackByArray = new StackByArray();
-        stackByArray.results(exps);
-    }
-
-    public String results(String exps) {
-        int index = 0;
-        // 保存读取到的数字和符号
-        int number1 = 0;
-        int number2 = 0;
-        int thiChar = ' ';
-        // 拼接字符串
-        StringBuilder sb = new StringBuilder();
-        // 数字栈
-        ArrayStack numberStack = new ArrayStack(10);
-        // 符号栈
-        ArrayStack operationStack = new ArrayStack(10);
-        // 保存计算结果
-        int result;
-
-        for (index = 0; index < exps.length(); index++) {
-            thiChar = exps.charAt(index);
-            // 判断字符是不是运算符
-            if (operationStack.isOperation(thiChar)) {
-                // Todo 比较运算符之间的优先级
-                if (operationStack.comparePriority(thiChar)) {
-                    // 入栈的运算符大于栈顶的,直接入栈
-                    operationStack.push(thiChar);
-                } else {
-                    // 入栈的运算符小于栈顶的,将数字栈顶的两个元素进行入栈运算符计算
-                    // 栈顶运算符
-                    int popChar = operationStack.pop();
-                    // 取出数字栈顶的两个元素
-                    number2 = numberStack.pop();
-                    number1 = numberStack.pop();
-                    // 计算结果
-                    result = operationStack.calculation(number1, number2, popChar);
-                    // 将入栈的运算符入符号栈
-                    operationStack.push(thiChar);
-                    // 将计算的结果放入数字栈顶
-                    numberStack.push(result);
-                }
-                // 数字直接添加到数字栈中
-            } else {
-                while (thiChar >= '0' && thiChar <= '9') {
-                    // 数字有可能会是多为,12
-                    sb.append(thiChar - '0');
-                    System.out.println("拼接字符串" + sb);
-                    index++;
-                    // 到了最后一位结束
-                    if (index >= exps.length()) {
-                        break;
-                    }
-                    thiChar = exps.charAt(index);
-                }
-                int num = Integer.parseInt(sb.toString());
-                numberStack.push(num);
-                // 初始化sb
-                sb = new StringBuilder();
-                index--;
-            }
-        }
-        // 运算
-        while (!operationStack.isEmpty()) {
-            int popChar = operationStack.pop();
-            number2 = numberStack.pop();
-            number1 = numberStack.pop();
-            result = operationStack.calculation(number1, number2, popChar);
-            numberStack.push(result);
-        }
-        System.out.println(numberStack.pop());
-        return exps;
-
-    }
-}
-
-class ArrayStack {
-    // 栈大小
-    private final int maxSize;
-    // 栈数组
-    int[] stack;
-    // 栈顶指针,默认为-1,添加元素top++
-    private int top;
-
-    /**
-     * 初始化栈
-     */
-    public ArrayStack(int maxSize) {
-        this.maxSize = maxSize;
-        stack = new int[this.maxSize];
-        top = -1;
-    }
-
-    /**
-     * 判断是否为空,top=-1,说明栈中没有元素
-     *
-     * @return top == -1
-     */
-    public boolean isEmpty() {
-        return top == -1;
-    }
-
-    /**
-     * 判断栈是否满了,
-     *
-     * @return
-     */
-    public boolean isFull() {
-        return top == maxSize - 1;
-    }
-
-    /**
-     * 添加元素
-     *
-     * @param i
-     */
-    public void push(int i) {
-        if (isFull()) {
-            System.out.println("栈满了,请不要再进来了");
-            return;
-        }
-        top++;
-        stack[top] = i;
-    }
-
-    /**
-     * 从栈顶取出一个元素
-     *
-     * @return
-     */
-    public int pop() {
-        if (isEmpty()) {
-            System.out.println("栈是空的,请不要在进来取了");
-            throw new EmptyStackException();
-        }
-        int retNum = stack[top];
-        top--;
-        return retNum;
-    }
-
-    /**
-     * 遍历栈
-     */
-    public void traverse() {
-        for (int thiChar : stack) {
-            System.out.println(thiChar);
-        }
-    }
-
-    /**
-     * 判断符号优先级
-     *
-     * @param operation
-     * @return
-     */
-    public int getPriority(int operation) {
-        if (operation == '*' || operation == '/') {
-            return 2;
-        } else if (operation == '+' || operation == '-') {
-            return 1;
-        } else if (operation >= '0' && operation <= '9') {
-            return 0;
-        } else {
-            return -1;
-        }
-    }
-
-    /**
-     * 比较符号栈和入栈符号元素的优先级
-     *
-     * @param operation
-     * @return
-     */
-    public boolean comparePriority(int operation) {
-        if (isEmpty()) {
-            return true;
-        } else {
-            int priority1 = getPriority(operation);
-            int priority2 = getPriority(stack[top]);
-            return priority1 > priority2;
-        }
-    }
-
-    /**
-     * 判断输入的是否是一个运算符号
-     *
-     * @param operation
-     * @return
-     */
-    public boolean isOperation(int operation) {
-        return operation == '*' || operation == '/' || operation == '-' || operation == '+';
-    }
-
-    /**
-     * 计算
-     *
-     * @param number1
-     * @param number2
-     * @param operation
-     * @return
-     */
-    public int calculation(int number1, int number2, int operation) {
-        switch (operation) {
-            case '+':
-                return number1 + number2;
-            case '-':
-                return number1 - number2;
-            case '*':
-                return number1 * number2;
-            case '/':
-                return number1 / number2;
-            default:
-                System.out.println(operation);
-                throw new RuntimeException("符号读取错误!");
-        }
-    }
-
-}

+ 31 - 0
src/com/example/currentTime.java

@@ -0,0 +1,31 @@
+package com.example;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Random;
+
+public class currentTime{
+
+    /*
+     * 
+     * 测试时间戳类
+     */
+    public static void main(String[] args) {
+        ArrayList<Integer> arrayList = new ArrayList<>();
+        long start = System.currentTimeMillis(); 
+        System.out.println("开始时间:"+start);
+        try {
+            Thread.sleep(5000); 
+            Random random = new Random();
+            for (int i = 0; i <= 20; i++) {
+                // 循环,生成随机数
+                arrayList.add(random.nextInt(20) + 1);
+            }
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end = System.currentTimeMillis(); 
+        System.out.println(end - start+"毫秒"); 
+    }
+
+}

+ 89 - 0
src/com/example/ms.java

@@ -0,0 +1,89 @@
+package com.example;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Random;
+
+public class ms {
+    public static void main(String[] args) {
+        ArrayList<Integer> arrayList = new ArrayList<>();
+        System.out.println("===================自动生成数组元素=====================");
+        long start = System.currentTimeMillis(); 
+        System.out.println("开始时间(毫秒):"+start);
+        try {
+             Thread.sleep(0); //线程休眠
+            Random random = new Random();
+            for (int i = 0; i <= 5; i++) {
+                arrayList.add(random.nextInt(5) + 1);
+            }
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end = System.currentTimeMillis(); 
+        System.out.println("结束时间(毫秒):"+end);
+        System.out.println("用时:" +(end - start)+ "毫秒"); 
+        System.out.println();
+        System.out.println("===================Iterator遍历数组=====================");
+        long start1 = System.currentTimeMillis(); 
+        System.out.println("开始时间(毫秒):"+start1);
+        try {
+            Thread.sleep(0); //线程休眠
+            java.util.Iterator<Integer> it = arrayList.iterator();
+            while (it.hasNext()) {
+                System.out.println(it.next());
+            } 
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end1 = System.currentTimeMillis(); 
+        System.out.println("结束时间(毫秒):"+end1);
+        System.out.println("用时:" +(end - start)+ "毫秒"); 
+        System.out.println();
+        System.out.println("===================获取指定元素=====================");
+        long start2 = System.currentTimeMillis(); 
+        System.out.println("开始时间(毫秒):"+start2);
+        try {
+            Thread.sleep(0); //线程休眠
+            int element = arrayList.get(3);
+            System.out.println("获取的元素:"+element);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end2 = System.currentTimeMillis(); 
+        System.out.println("结束时间(毫秒):"+end2);
+        System.out.println("用时:" +(end - start)+ "毫秒"); 
+        System.out.println();
+        System.out.println("===================删除指定元素=====================");
+        long start3 = System.currentTimeMillis(); 
+        System.out.println("开始时间(毫秒):"+start3);
+        try {
+            Thread.sleep(0); //线程休眠
+            int element1 = arrayList.get(3);
+            arrayList.remove(element1);
+            System.out.println("删除的元素:"+element1);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end3 = System.currentTimeMillis(); 
+        System.out.println("结束时间(毫秒):"+end3);
+        System.out.println("用时:" +(end - start)+ "毫秒"); 
+        System.out.println();
+        System.out.println("===================删除所有元素=====================");
+        long start4 = System.currentTimeMillis(); 
+        System.out.println("开始时间(毫秒):"+start4);
+        try {
+            Thread.sleep(0); //线程休眠
+            arrayList.removeAll(arrayList);
+            System.out.println("删除所有元素");
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end4 = System.currentTimeMillis(); 
+        System.out.println("结束时间(毫秒):"+end4);
+        System.out.println("用时:" +(end - start)+ "毫秒"); 
+        System.out.println();
+    }
+
+}

+ 94 - 0
src/com/example/nanosecond.java

@@ -0,0 +1,94 @@
+package com.example;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Random;
+
+public class nanosecond {
+    public static void main(String[] args) {
+        ArrayList<Integer> arrayList = new ArrayList<>();
+        System.out.println("===================自动生成数组元素=====================");
+        long start = System.nanoTime(); 
+        System.out.println("开始时间(纳秒):"+start);
+        try {
+             Thread.sleep(0); //线程休眠
+            Random random = new Random();
+            for (int i = 0; i <= 5; i++) {
+                arrayList.add(random.nextInt(5) + 1);
+            }
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end = System.nanoTime(); 
+        System.out.println("结束时间(纳秒):"+end);
+        System.out.println("用时:" +(end - start)+ "纳秒"); 
+        System.out.println("===================结束=====================");
+        System.out.println();
+        System.out.println("===================Iterator遍历数组=====================");
+        long start1 = System.currentTimeMillis(); 
+        System.out.println("开始时间(纳秒):"+start1);
+        try {
+            Thread.sleep(0); //线程休眠
+            java.util.Iterator<Integer> it = arrayList.iterator();
+            while (it.hasNext()) {
+                System.out.println(it.next());
+            } 
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end1 = System.currentTimeMillis(); 
+        System.out.println("结束时间(纳秒):"+end1);
+        System.out.println("用时:" +(end - start)+ "纳秒"); 
+        System.out.println("===================结束=====================");
+        System.out.println();
+        System.out.println("===================获取指定元素=====================");
+        long start2 = System.currentTimeMillis(); 
+        System.out.println("开始时间(纳秒):"+start2);
+        try {
+            Thread.sleep(0); //线程休眠
+            int element = arrayList.get(3);
+            System.out.println("获取的元素:"+element);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end2 = System.currentTimeMillis(); 
+        System.out.println("结束时间(纳秒):"+end2);
+        System.out.println("用时:" +(end - start)+ "纳秒"); 
+        System.out.println("===================结束=====================");
+        System.out.println();
+        System.out.println("===================删除指定元素=====================");
+        long start3 = System.currentTimeMillis(); 
+        System.out.println("开始时间(纳秒):"+start3);
+        try {
+            Thread.sleep(0); //线程休眠
+            int element1 = arrayList.get(3);
+            arrayList.remove(element1);
+            System.out.println("删除的元素:"+element1);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end3 = System.currentTimeMillis(); 
+        System.out.println("结束时间(纳秒):"+end3);
+        System.out.println("用时:" +(end - start)+ "纳秒"); 
+        System.out.println("===================结束=====================");
+        System.out.println();
+        System.out.println("===================删除所有元素=====================");
+        long start4 = System.currentTimeMillis(); 
+        System.out.println("开始时间(纳秒):"+start4);
+        try {
+            Thread.sleep(0); //线程休眠
+            arrayList.removeAll(arrayList);
+            System.out.println("删除所有元素");
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        long end4 = System.currentTimeMillis(); 
+        System.out.println("结束时间(纳秒):"+end4);
+        System.out.println("用时:" +(end - start)+ "纳秒"); 
+        System.out.println("===================结束=====================");
+        System.out.println();
+    }
+
+}