Java基础(第三章)

2016-08-03  本文已影响0人  兰陵忧患生

K# Java核心技术卷一


得到最接近的整数 
double x = 9.997;
int nx = (int) Math.round(x);//round返回的结果是long类型
int cpCount = greeting.codePointCount(0, greeting.length());//得到实际长度,即代码点得数量.
char first = greeting.chatAt(0);//调用s.char.charAt(n)将返回位置n到代码单元.
//得到第i个代码点.
int index = gretting.offsetByCodePoint(0, i);
int cp = gertting.codePintAt(index);//
//遍历一个字符串,依次查看每一个代码点
int cp = sentence.codePointAt(i);
if(Chartacter.isSupplementaryCodePoint(cp)) i += 2;
else i++;
//回退操作
i--;
if(Chartacter.isSupplementaryCodePoint(cp)) i--;
int cp = sentence.codePointAt(i);
API java.lang.Stirng 1.0
int codePointAt(int index);//返回给定位置开始或结束到代码点
int offsetByCodePoints(int startIndex, int cpCount);//返回从startIndex代码点开始,位移cpCount后的代码点索引
int compareTo(String other);//按照字典顺序,如果字符串位于other之前,返回一个负数;如果字符串位于other之后返回一个正数;如果两个字符串相等,返回0
int indexOf(String str);
int indexOf(Sting str, int fromIndex);
int indexOf(int cp);
int indexOf(int cp, int fromIndex);//返回与字符串str或代码点cp匹配的第一个子串得开始位置.这个位置从索引0或fromIndex开始计算.如果在原始串中不存在str,返回-1.
int codePointCount(int startIndex, int endIndex);//返回startIndex和endIndex-1 之间的代码点数量.没有配成对到代用字符将计入代码点.
Stirng trim();//返回一个新字符串.这个字符串将删除年原始字符串头部和尾部到空格.
StringBuilder builder = new StringBuilder();//构造空的字符串构建器
bulider.append(ch);添加内容
String completedString = builder.toStirng();//构建字符串,得到一个String对象,其中包含了构建器中的字符序列。
API java.lang.StringBuilder 5.0
StringBuilder();//构造一个空的字符串构建器
int length();//返回构建器或缓冲器中的代码单元
StringBuilder append(String str);//追加一个字符串并返回this
StringBuilder append(char c);//追加一个代码单元并返回this
StringBuilder appendCodePoint(int cp);//追加一个代码点,并将其转换为一个或两个代码单元并返回this
String toStrinig();//返回一个与构造器或缓冲区内容相同的字符串。
API java.util.Scanner 5.0
Scanner in = new Scanner(System.in);//用给定的输入流创建一个scanner对象。
System.out.print("What is your name?");
String name = in.nextLine();//输入一行,包括空格。
String firstName = in.next();//读取一个单词,以空格符作为分隔符。
System.out.print("How old are you?");
int age = in.nextInt();//读取一个整数。nextDouble()读取下一个浮点数。
API java.io.Console 6
Console cons = System.console();
String username = cons.readLine("User name: ");
char[] passwd = cons.readPassword("Password: ");//Console每次只能读取一行,没有读取一个单词或者一个数值的方法。
API java.lang.System 1.0
staic Console console() 6 //交互操作
System.printf("%tc", new Date());//Mon Feb 09 18:05:19 PST 2004
System.printf("%1$s %2StB %2$te, %2$tY", "Due date", new Date());//Due date: February 9, 2004
System.out.printf("%s %tB %<te, %<tY", "Due date: ", new Dte());//<标志,它指示前面格式说明中的参数将再次被使用。
Scanner in = new Scanner(Paths.get("myfile.txt"));
PrintWriter out = new PrintWriter("myfile.txt");
Scanner in = new Scanner("myfile.txt");//字符串
API java.io.PrintWrier 1.1
PrintWriter(String fileName);//构造一个将数据写入文件的PrintWriter。文件名由参数决定。
API java.nio.file.Pahts 7
static Path get(String pathname);//根据给定的路径名构造一个Path。
if(x <= 0) if(x == 0) sign = 0; else sign = -1;
for(double x = 0; x != 10; x += 0.1)...//可能永远不会结束,由于舍入的误差,最终可能得不到精确值。循环中,因为0.1无法精确地用二进制表示,x将从9.99...98跳到10.09...98
int lotterOdds = 1;
for(int i = 1; i <= k; i++) {
    lotterOdds = lotterOdds * (n-i+1) / i;
}
  1. 类型为char, byte, short或int(或其包装类Character, Byte, Short和Integer)的常量表达式.
  2. 枚举变量
  3. 从Java SE 7 开始,case标签还可以是字符串字面量.
Size sz = ...;
switch(sz) {
    case SMALL: //no need to use Size.SMALL
        ...
        breakl
    ...
}
API java.math.BigInteger 1.1
BigInteger add(BigInteger other);//和
BigInteger subtract(BigInteger other);//差
BigInteger multiply(BigInteger other);//积
BigInteger divide(BigInteger other);//商
BigInteger mod(BigInteger other);//余数
//返回一个大整数和另一个大整数other的和,差,积,商以及余数。
static BigInteger valueOf(long x);//返回值等于x的大整数。
BigDecimal add(BigDecimal other);//和
BIgDecimal subtract(BigDecimal other);//差
BigDecimal multiply(BigDecimal other);//积
BigDecimal divide(BigDecimal other RoundingMode mode);//商
//返回这个大实数与另一个大实数other.HALF_UP是在学校学习的四舍五入方式。
static BigDecimal valueOf(long x);
static BigDecimal valueOf(long x, int scale);
//返回值位x或x/10^scale的大实数

$$x/10^{scale}$$

int[] a;//声明整型数组a
int[] a = new int[100];//初始化一个数组,数组的长度不要求是常量,new int[n]创建一个长度位n的数组。
int[] smallPrimes = {2, 3, 5, 7, 11, 13};//创建数组并初始化的简化形式,不需要new
new int[] {17, 19, 23, 29, 31, 37};//初始化一个匿名数组
samllPrimes = new int[] {17, 19, 23, 29, 31, 37};
int[] copiedLuckyNumbers = Array.copyOf(luckNumbers, luckNumbers.length);//将一个数组的所有值拷贝到一个新的数组中去。
luckNumbers = Arrays.copyOf(luckNumbers, 2 * luckyNUmbers.length);//这种方法通常用来增加数组的大小。如果数组是数值型,那么多余的元素将被赋值为0;布尔型,将赋值为false;如果长度小于原始数组的长度,则只拷贝最前面的数据元素。
public class Message {
    public static void main(String[] args) {
        if...
    }
}
java Message -g cruel world;//args数组将包含下列内容:args[0]:"-g";args[1]:"cruel";args[2]:"world"
import java.util.*;
/**
  *3-7
  */
public class LotteryDrawing {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("How many numbers do you need to draw?");
        int k = in.nextInt();
        System.out.print("What is the highest number you can deaw?");
        int n = in.nextInt();
        int[] numbers = new int[n];
        for(int i = 0; i < numbers.length; i++) {
            int r = (int) (Math.random() * n);
            result[i] = number[r];
            numbers[r] = numbers[n - 1];
            n--;
            Arrays.sort(result);
            System.out.println("Bet the following combination.It'll make you rish!");
            for(int r : result)
                System.out.println(r);
        }
    }
}
API java.util.Arrays 1.2
//参数a 类型为int,long,char,byte,boolean,float或double的数组.
//start 包含这个值.
//end 不包含这个值.
//v 同a的数据元素类型相同的值.
static String toString(type[] a);//返回包含a中数据元素的字符串,这些数据元素被放在括号中,并用逗号分隔.
static type copyOf(type[] a, int length);
static type copyOf(type[] a, int start, int end);
static void sort(type[] a);
static int binarySearch(type[] a, type v);
static int binarySearch(type[] a, int start, int end, type v);//采用二分搜索算法查找v.如果查找成功,则返回相应到下标值;否则,返回一个负数值,-r-1是为保持a有序v应插入到位置.
static void fill(type[] a, type v);//将数组的所有数据元素值设置为v.
static boolean equals(type[] a, type[] b);//如果两个数组大小相同,并且下标相同的元素都对应相等,返回true.
public class LotteryArray {
    public static void main(String[] args) {
        final int NMAX = 10;
        int[][] odds = new int[NMAX + 1][];
        for(int n = 0; n <= NMAX; n++)
            odds[n] = new int[n + 1];
        for(int n = 0; n < odds.length; n++) {
            for(int k = 0;k < odds[n].length; k++) {
                int lotteryOdds = 1;
                for(int i = 1; i <= k; i++)
                    lotteryOdds = lotteryOdds * (n - i + 1) / i;
                odds[n][k] = lotteryOdds;
            }
            for(int[] row : odds)
                System.out.printf("%4d", odd);
            System.out.println();
        }
    }
}

表3-3 特殊字符的转义序列符
图3-1 数值类型之间的合法转换
表3-4 运算符优先级
表3-5 用于printf的转换符
表3-6 用于printf的标志
表3-7 日期和时间的转换符
图3-6 格式说明符语法

上一篇 下一篇

猜你喜欢

热点阅读