展开 层次号 标签

      JShell代码
        1  示例
            1.1  对象
                1.1.1  字串
String exampleString = "hello";
exampleString += " world";
"包含e:"+ exampleString.contains("e") + "    长度:" + exampleString.length();
                1.1.2  时间
import java.util.Date;
Date exampleDate = new Date();
"当前时间:" + exampleDate.toString() + "     值:" + exampleDate.getTime();
                1.1.3  数组
import java.util.Arrays;
double[] exampleArray =  new double[5];
for (int i=0; i
                1.1.4  列表
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
List exampleList = new ArrayList<>();
exampleList.addAll(Arrays.asList("a", "b", "c", "d", "e"));
exampleList.toString();
            1.2  表达式
                1.2.1  数值计算
                    1.2.1.1  欧拉数
Math.E
                    1.2.1.2  圆周率
Math.PI
                    1.2.1.3  绝对值
Math.abs(-5.611)
                    1.2.1.4  平方根
Math.sqrt(9)
                    1.2.1.5  三次方根
Math.cbrt(27)
                    1.2.1.6  
Math.pow(2,5)
                    1.2.1.7  欧拉冥
Math.exp(2)
                    1.2.1.8  自然对数(底为e)
Math.log(6)
                    1.2.1.9  常用对数(底为10)
Math.log10(6)
                    1.2.1.10  余弦
Math.cos(0)
                    1.2.1.11  正弦
Math.sin(9)
                    1.2.1.12  正切
Math.tan(5)
                    1.2.1.13  反正切
Math.atan(-7.3)
                    1.2.1.14  反余弦
Math.acos(0.5)
                    1.2.1.15  反正弦
Math.asin(0.3)
                    1.2.1.16  上舍入
Math.ceil(4.13)
                    1.2.1.17  下舍入
Math.floor(4.67)
                    1.2.1.18  四舍五入
Math.round(4.83)
                    1.2.1.19  随机数
Math.random()
                    1.2.1.20  大值
Math.max(1,-3)
                    1.2.1.21  小值
Math.min(1,-3)
                1.2.2  字符串处理
                    1.2.2.1  忽略大小写的等于
"Hello".equalsIgnoreCase("hello")
                    1.2.2.2  等于
"Hello".equals("hello")
                    1.2.2.3  替换首个
"Hello World! World is yourself.".replace("World", "Feeling");
                    1.2.2.4  替换所有
"Hello World! World is yourself.".replaceAll("World", "Feeling");
                    1.2.2.5  开始于
"Hello".startsWith("h")
                    1.2.2.6  结尾于
"Hello".endsWith("o")
                    1.2.2.7  子串
"Hello".substring(2, 5)
                    1.2.2.8  字符
"hello".charAt(2)
                    1.2.2.9  连接
"hello".concat(" world")
                    1.2.2.10  分割
"1,2,3,4".split(",")
                    1.2.2.11  位置
"hello".indexOf("e")
                    1.2.2.12  最后位置
"hello".lastIndexOf("l")
                    1.2.2.13  长度
"hello".length()
                    1.2.2.14  小写
"Hello".toLowerCase()
                    1.2.2.15  大写
"Hello".toUpperCase()
                    1.2.2.16  包含
"Hello".contains("el")
                    1.2.2.17  删除首尾空白字符
" Hello  ".trim()
                    1.2.2.18  为空白字符串
"  ".isBlank()
                    1.2.2.19  为空
"".isEmpty()
                1.2.3  布尔运算
                    1.2.3.1  列表包含
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
List exampleListC = new ArrayList<>();
exampleListC.addAll(Arrays.asList("a", "b", "c", "d", "e"));
String exampleStringC = "c";
exampleListC.contains(exampleStringC);
                    1.2.3.2  字符串匹配
// MyBox类路径应当已添加到JShell环境中
import mara.mybox.tools.StringTools;
String exampleStringM = "abc1233hello";
String exampleRegexM = "\\S*3{2,}\\S*";  
boolean exampleCaseInsensitiveM = true;
StringTools.match(exampleStringM,exampleRegexM,exampleCaseInsensitiveM);
                    1.2.3.3  字符串包含
// MyBox类路径应当已添加到JShell环境中
import mara.mybox.tools.StringTools;
String exampleStringI = "abc1233hello";
String exampleRegexI = "3{2}";
boolean exampleCaseInsensitiveI = true;
StringTools.include(exampleStringI,exampleRegexI,exampleCaseInsensitiveI);
                    1.2.3.4  与/或/否
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
List exampleListB = new ArrayList<>();
exampleListB.addAll(Arrays.asList("a", "b", "c", "d", "e"));
String exampleStringB = "c";
exampleListB.contains(exampleStringB) && 
(exampleListB.size() >= 8 || exampleStringB.length() != 1 || !exampleStringB.startsWith("h"));
            1.3  方法
                1.3.1  圆的面积
double circleAreaByDiameter(double diameter) {        
    double radius = diameter / 2;        
    return   Math.PI *  radius * radius ;        
}
circleAreaByDiameter(120) + circleAreaByDiameter(30)
                1.3.2  四舍五入
import java.math.BigDecimal;        
import java.math.RoundingMode;        
double scale(double v, int scale) {        
    BigDecimal b = new BigDecimal(v);        
    return b.setScale(scale, RoundingMode.HALF_UP).doubleValue();        
}    
scale(Math.PI, 3)
                1.3.3  格式化数值
import java.math.BigDecimal;        
import java.math.RoundingMode;        
double scale(double v, int scale) {        
    BigDecimal b = new BigDecimal(v);        
    return b.setScale(scale, RoundingMode.HALF_UP).doubleValue();        
}        
                                     
import java.text.DecimalFormat;        
String formatDouble(double data, int scale) {        
    try {        
        String format = "#,###";        
        if (scale > 0) {        
            format += "." + "#".repeat(scale);        
        }        
        DecimalFormat df = new DecimalFormat(format);        
        return df.format(scale(data, scale));        
    } catch (Exception e) {        
        return e.toString();        
    }        
}        
                                     
double circleAreaByRadius(double radius) {        
    return   Math.PI *  radius * radius ;        
}        
                                     
formatDouble(circleAreaByRadius(273.4), 4)
                1.3.4  格式化时间
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;

String DatetimeFormat = "yyyy-MM-dd HH:mm:ss.SSS";

String datetimeToString(Date theDate, String format, TimeZone theZone) {
    if (theDate == null || theZone == null) {
            return null;
    }
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    formatter.setTimeZone(theZone);
    String dateString = formatter.format(theDate);
    return dateString;
}

datetimeToString(new Date(), DatetimeFormat, TimeZone.getDefault());
                1.3.5  匹配字符串
import java.util.regex.Matcher;
import java.util.regex.Pattern;

boolean match(String string, String find, boolean isRegex,
            boolean dotAll, boolean multiline, boolean caseInsensitive) {
        if (string == null || find == null || find.isEmpty()) {
            return false;
        }
        try {
            int mode = (isRegex ? 0x00 : Pattern.LITERAL)
                    | (caseInsensitive ? Pattern.CASE_INSENSITIVE : 0x00)
                    | (dotAll ? Pattern.DOTALL : 0x00)
                    | (multiline ? Pattern.MULTILINE : 0x00);
            Pattern pattern = Pattern.compile(find, mode);
            Matcher matcher = pattern.matcher(string);
            return matcher.matches();
        } catch (Exception e) {
            return false;
        }
}

match("Hello1233World", "\\S*3{2,}\\S*", true, true, true, true);
                1.3.6  包含字符串
import java.util.regex.Matcher;
import java.util.regex.Pattern;

boolean include(String string, String find, boolean caseInsensitive) {
        if (string == null || find == null || find.isEmpty()) {
            return false;
        }
        try {
            int mode = (caseInsensitive ? Pattern.CASE_INSENSITIVE : 0x00) | Pattern.MULTILINE;
            Pattern pattern = Pattern.compile(find, mode);
            Matcher matcher = pattern.matcher(string);
            return matcher.find();
        } catch (Exception e) {
            return false;
        }
    }

include("Hello1233World", "3{2}", true);

* 网页可编辑时,链接和控件可能不响应。