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];

最新文章

  1. Sql Server系列:规范化及基本设计
  2. Visual Studio Code 使用 ESLint 增强代码风格检查
  3. 浅谈分治算法在OI中的应用
  4. String,你到底创建了几个对象????
  5. error:no such partition grub rescue
  6. 如何解决android studio 运行时中文乱码的问题
  7. 管道命令xargs
  8. sql 截取两个字符串之间的字符
  9. NOI2014题解
  10. vhd镜像格式及vhd-util工具应用
  11. Android 接入 OpenCV库的三种方式
  12. python-opencv在有噪音的情况下提取图像的轮廓
  13. bat自动打包压缩实现
  14. java ReentrantLock 公平锁 非公平锁 测试
  15. Hadoop之MapReduce思维导图
  16. hdu-2043解题报告
  17. G2( bizCharts ) React 绘制混合图例
  18. [视频]K8飞刀 Discuz! X 系列(1.5 2.5 3.x)产品升级漏洞GetShell教程
  19. Python3出现&quot;No module named &#39;MySQLdb&#39;&quot;问题-以及使用PyMySQL连接数据库
  20. 编译Linux内核(Mac OS平台)

热门文章

  1. 免费获取思维导图Mindmaster会员教程
  2. 12、Linux磁盘设备基础知识(1)
  3. GDI+中发生一般性错误的解决办法(转载)
  4. Centos 6.5 GitLab安装配置搭建详细过程
  5. AcWing 204. 表达整数的奇怪方式
  6. 常见链表操作-链表中环的检测(JAVA实现)
  7. 如何Spring Cloud Zuul作为网关的分布式系统中整合Swagger文档在同一个页面上
  8. 关于mysql binlog二进制
  9. Android Binder 进程间通讯机制梳理
  10. SESSION和JWT