【阅读笔记】Java核心技术卷一 #1.Chapter3

3 Java的基本程序设计结构

3.1 一个简单的 Java 应用程序(略)

3.2 注释(略)

3.3 数据类型

8种基本类型
byte,short,int,long
float,double
char(Unicode)
boolean

3.4 变量

声明,初始化,赋值,final

3.5 运算符

java.lang.Math

///API
Math.sqrt(double x);
Math.pow(double x, double a);
Math.floorMod(position+adjustment, 12);
Math.sin
Math.cos
Math.tan
Math.atan
Math.atan2
Math.exp
Math.log
Math.log10
Math.PI
Math.E
long Math.round(double)

了解:StrictMath类

  • 枚举类型
enum Size {SMALL, MEDIUM, LARGE, EXTRA_LARGE}
Size size = Size.SMALL;

3.6 字符串

码点和码元
𝕆
要想得到第 i 个码点
int index = str.offsetByCodePoints(0, i) ;
int cp = str.codePointAt(index);

java.lang.StringBuilder

int length();
StringBuilder append(String str);
StringBuilder append(Char c);
void setCharAt(int i, char c);
StringBuilder insert(int offset, String str);
StringBuilder insert(int offset, Char c);
StringBuilder delete(int startindex, int endlndex);
String toString();

3.7 输入输出

3.7.1 读取输入

  • java.util.Scanner
Scanner in = new Scanner(System.in);
///API
Scanner (InputStream in);
String nextLine();
String next();
int nextInt();
double nextDouble();
boolean hasNext();
boolean hasNextInt();
boolean hasNextDouble();
  • java.io.Console
Console cons = System.console();
String username = cons.readLine("Username: " );
char[] passwd = cons.readPassword ("Password: ");
///API
static char [] readPassword(String prompt, Object... args);
static String readLine(String prompt , Object... args);
//显示字符串 prompt 并且读取用户输入,直到输入行结束。args参数可以用来提供输人格式

3.7.2 格式化输出

  • System.out.printf(); 类似于C语言的printf()
  • 可以使用静态的String.format方法创建一个格式化的字符串,而不打印输出
    String message = String.format("Hello, %s. Next year, you'll be %d", name, age);
    

3.7.3 文件输入与输出

读取:使用文件对象构造Scanner。

Scanner in = new Scanner(Paths.get("myflle.txt"), "UTF-8");

写入:PrintWriter,可以像输出到System.out一样使用printprintln以及printf命令。覆盖写入

PrintWriter out = new PrintWriter("myfile.txt", " UTF-8");
out.print("test str");
// 必须flush或者close才能写入
// out.flush();
out.close();

3.8 控制流程

  • 不能在嵌套的两个块中声明同名的变量
  • case标签只能是:
    • char byte short int 的常量表达式
    • 枚举常量
    • 字符串字面量
  • 带标签的continue和break,可以跳出多重嵌套

3.9 大数值

java.math.BigInteger 大整数

///API
BigInteger add(BigInteger other);
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other);
BigInteger divide(BigInteger other);
BigInteger mod(BigInteger other);
int compareTo(BigInteger other);
static BigInteger valueOf(long x);

java.math.BigDecimal 大实数

///API
BigDecimal add(BigDecimal other);
BigDecimal subtract(BigDecimal other)
BigDecimal multiply(BigDecimal other);
BigDecimal divide(BigDecimal other, RoundingMode mode);
BigDecimal mod(BigDecimal other);
int compareTo(BigDecimal other);
static BigDecimal valueOf(long x);
static BigDecimal valueOf(long x, int scale);

3.10 数组

数组是一种数据结构。
foreach循环:for (variable : collection ) statement

int[] a = new int[10];
// 赋值
// foreach
for (int element : a)
    System.out.println(element);
System.out.println(Arrays.toString(a));

java.util.Arrays

/// API
static String toString (type[] a);
static type copyOf(type[] a, int length);
static type copyOfRange(type[] a, int start , int end);
static void sort(type[] a);
static int binarySearch(type[] a, int start, int end, type v);
static void fill(type[] a, type v);
static boolean equals(type[] a, type[] b);
  • 小例子:从100个数中抽出10个不重复的数

  • 多维数组

int[][] a = new int[10][5];
// 赋值
for(int[] row : a)
    for(int value : row)
        System.out.println(value);
System.out.println(Arrays.deepToString(a));
int[][] odds = new int[N + 1][];
for (int i = 0; i <= N; i++)
    odds[i] = new int[i + 1];
posted @ 2020-03-15 17:33  Recycer  阅读(150)  评论(0编辑  收藏  举报