《Java核心技术卷1 基础知识》 第3章 学习笔记

代码点与代码单元

String greeting ="Hello";
int n = greeting.length();//n is 5
		
//获取代码点数量(字符串实际长度)
int cpCount = greeting.codePointCount(0,greeting.length());//cpCount is 5
System.out.println(cpCount);

//返回位置 n 的代码单元
char first = greeting.charAt(0);//位置 0 的代码单元是 H
char last = greeting.charAt(4);//位置 4 的代码单元是 o
System.out.println(first+"..."+last);
		
		
//得到第 i 个代码点(ascii 码值)
int i = 4;
int index = greeting.offsetByCodePoints(0, i);
int cp = greeting.codePointAt(index);
System.out.println(cp);

Scanner 类相关输出

Scanner类位于java.util包中

Scanner in = new Scanner(System.in);
System.out.println("What's your name?");
String name =in.nextLine();//获取输入一行
System.out.println(name);


Scanner in = new Scanner(System.in);
int age = in.nextInt();//获取一个整数   空格为分隔符
System.out.println("age:"+age);


Scanner in = new Scanner(System.in);
String firstName = in.next();//获取一行 空格为分隔符
System.out.println(firstName);

Console 类的简单使用

Console cons = System.console();
if (cons != null) {
    // -------------------------
    PrintWriter printWriter = cons.writer();
    printWriter.write("input:");
    cons.flush();
    // -------------------------
    String str1 = cons.readLine();
    // -------------------------
    cons.format("%s", str1);
}

执行效果:

D:\code\Hello World\src>javac FirstSimple.java
D:\code\Hello World\src>java FirstSimple
input:hello java
hello java
D:\code\Hello World\src>

文件读写

文件读写		
try {
    PrintWriter out = new PrintWriter("MyFile.txt");
} catch (FileNotFoundException e) {
    e.printStackTrace();
} 
try {
    Scanner in = new Scanner(Paths.get("MyFile.txt"));
    System.out.println(in.nextLine());
} catch (IOException e) {
    e.printStackTrace();
}

命令行参数

main方法带有的String args[]参数,将接受命令行参数作为一个字符串数组

if (args[0].equals("-h")) 
	System.out.println("Hello");
else if (args[0].equals("-g"))
	System.out.println("Goodbye");
for(int i=1;i<args.length;i++)
	System.out.println(" "+args[i]);
System.out.println("!");

执行效果:

D:\code\Hello World\bin>java FirstSimple -g cruel world
Goodbye
 cruel
 world
!

数组的使用

数组的基本操作

//数组的初始化
int[] smallPrimes ={2,3,4,5,6,11,13};
smallPrimes = new int[]{17,19,23,29,31};

//数组的拷贝的两种方式
int[] luckyNumbers = smallPrimes;
System.out.println(Arrays.toString(luckyNumbers));

int[] copiedLuckyNumbers = Arrays.copyOf(luckyNumbers, luckyNumbers.length);
System.out.println(Arrays.toString(copiedLuckyNumbers));

//遍历数组的三种方式		
for(int i=0;i<smallPrimes.length;i++)
	System.out.println(smallPrimes[i]);

for(int element : smallPrimes)
	System.out.println(element);

System.out.println(Arrays.toString(smallPrimes));
import java.util.Arrays;
import java.util.Scanner;
public class SecondDemo {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		System.out.println("在多少个正整数中抽取?");
		int n = in.nextInt();
		
		System.out.println("抽取几个数字?");
		int k = in.nextInt();

		int[] numbers = new int[n];
		for(int i=0;i<numbers.length;i++)
			numbers[i]=i+1;
		
		int[] result = new int[k];
		for(int i=0;i<result.length;i++){
			//制作一个 0 到 n-1 之间的随机数
			int r = (int)(Math.random()*n);
			
			result[i] = numbers[r];//将 numbers 中抽到的值赋值给 result 数组
			numbers[r] = numbers[n-1];//用最后一个值填充 number[r]的值,n 再减 1
			n--;
		}	
		Arrays.sort(result);//采用优化的快速排序算法对数组进行排序
		for(int r:result)
			System.out.println(r);
	}
}

二维数组

import java.util.Arrays;
import java.util.Scanner;
public class SecondDemo {
	public static void main(String[] args) {
		final double STARTRATE = 10;
		final int NRATES = 6;
		final int NYEARS = 10;

		double[] interestRate = new double[NRATES];
		
		for(int j2=0;j2<interestRate.length;j2++)
			interestRate[j2]=(STARTRATE +j2)/100.0;

		double[][] balances = new double[NYEARS][NRATES];
			
		for(int j1=0;j1<balances[0][j1];j1++)
			balances[0][j1]=10000;
		
		for(int i=1;i< balances.length;i++){
			for(int j=0;j<balances[i].length;j++){
				
				double oldBalance = balances[i-1][j];
				
				double interest = oldBalance * interestRate[j];
						
				balances[i][j] = oldBalance + interest;
			}
			
		}

		for(int j=0;j<interestRate.length;j++)
			System.out.printf("%9.0f%%",100*interestRate[j]);
		
		System.out.println();
		
		for(double[] row:balances){
			for(double b:row)
				System.out.printf("%10.2f",b);
			System.out.println();
		}
	}
}

执行效果:

10%       11%       12%       13%       14%       15%
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00
0.00      0.00      0.00      0.00      0.00      0.00

不规则数组

public class ThirdDemo {

	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)
			{
				for(int odd:row)
					System.out.printf("%4d",odd);
				System.out.println();
			}
		}
	}
}

基础代码调试

  • 1.使用System.err.println输出
System.out.println("This is a debug info");
System.out.println(new JFrame(){
    public String toString() {
        return "This is a new JFrame";
    }
});
System.err.println("This is an error info");
  • 2.debug 调试

数组越界异常:ArrayIndexOutOfBoundsException

int[] ints = new int[20];
try {
    for(int m = 0;m<21;m++){
        ints[m]=m+1;
        System.out.println(m+1);
    }
} catch (Exception e) {
    e.printStackTrace();
    System.err.println("数组越界异常");
}

发表评论

发表评论

*

沙发空缺中,还不快抢~