Scanner文件输入流
Scanner in = new Scanner(Paths.get("myfile.txt"),"utf-8");
String s = null;
while(in.hasNext()) {
s = in.nextLine(); //读取每一行字符
}
System.out.println(s); //HelloWorld!
InputStream input = new FileInputStream("myfile.txt");
Scanner in = new Scanner(input,"utf-8");
String s = null;
while(in.hasNext()) {
s = in.nextLine(); //读取每一行字符
}
System.out.println(s); //HelloWorld!
for循环
for循环保存100以下的奇数和,偶数和
int oddSum = 0; // 奇数
int evenSum = 0; // 偶数
for (int i = 0; i < 100; i++) {
if (i % 2 != 0) {
oddSum += i;
} else {
evenSum += i;
}
}
System.out.println("奇数:" + oddSum);
System.out.println("偶数:" + evenSum);
结果:
奇数:2500
偶数:2450
for循环打印乘法表
for (int i = 1; i <= 9; i++) {
System.out.println();
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + " ");
}
}
结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
for循环打印矩形
for (int i = 0; i < 5; i++) {
System.out.println();
for (int j = 0; j < 10; j++) {
if (i == 0 || i == 5 - 1 || j == 0 || j == 10 - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
}
结果:
**********
* *
* *
* *
**********
for循环打印空心菱形
for (int n = 1; n <= 5; n++) { // 控制输出行
for (int j = 5; j >= n; j--) { // 控制输出空格
System.out.print(" ");
}
for (int k = 1; k <= 2 * n - 1; k++) { // 控制输出*
if (k == 1 || k == 2 * n - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int n = 1; n <= 5; n++) { // 控制输出行
System.out.print(" ");
for (int j = 1; j <= n; j++) { // 控制输出空格
System.out.print(" ");
}
for (int k = 1; k <= 2 * (5 - n) - 1; k++) { // 控制输出*
if (k == 1 || k == 2 * (5 - n) - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
结果:
*
* *
* *
* *
* *
* *
* *
* *
*
for循环打印圆形
int r = 5;
for (int y = 0; y <= 2 * r; y += 2) {
int x = (int) (r - Math.sqrt(2 * r * y - y * y)); // 左边空格5 1 0 1 5
int len = 2 * (r - x); // 中间的空格 0 8 10 8 0
for (int i = 1; i <= x; i++) {
System.out.print(' '); // 左边的空格
}
System.out.print('*'); // 左边的*
for (int j = 1; j <= len; j++) {
System.out.print(' '); // 中间的空格
}
System.out.println('*'); // 右边的*
}
结果:
**
* *
* *
* *
* *
**
大数值
BigInteger和BigDecimal可以处理包含任意长度数字序列的数值。
BigInteger类实现了任意精度的整数运算,BigDecimal实现了任意精度的浮点数运算。
BigInteger:
public BigInteger abs()
返回大整数的绝对值
public BigInteger add(BigInteger val)
返回两个大整数的和
public BigInteger subtract(BigInteger val)
返回两个大整数相减的结果
public BigInteger multiply(BigInteger val)
返回两个大整数的积
public BigInteger divide(BigInteger val)
返回两个大整数的商
大数值计算机率
Scanner in = new Scanner(System.in);
System.out.println("抽多少个?");
int k = in.nextInt();
System.out.println("一共多少个?");
int n = in.nextInt();
/*
* 如果说 一共 5个
* 抽两次
* 1*(5-1+1)/1 5/1 = 5
* 5*(5-2+1)/2 20/2 = 10
* 相当于 5×4÷1÷2 = 10
* 机率率是1/10
*
* 如果说 一共 10个
* 抽三次
* 1*(10-1+1)/1 10/1 = 10
* 10*(10-2+1)/2 90/2 = 45
* 45*(10-3+1)/3 360/3 = 120
* 相当于 10×9×8÷1÷2÷3 = 120
* 机率1/120
*
* int odds = 1;
* for(int i = 1; i<=k;i++) {
* odds = odds * (n - i + 1)/i;
* }
* System.out.println("抽中的几率是:1/"+odds);
*/
BigInteger odds = BigInteger.valueOf(1);
for(int i = 1; i<=k;i++) {
odds = odds.multiply(BigInteger.valueOf(n-i+1))
.divide(BigInteger.valueOf(i));
}
System.out.println("抽中的几率是:1/"+odds);
比如说一共5个数,要从中抽取两个,中奖的概率是多少?
概率是:
4 + 3 + 2 + 1 = 10
5 × 4 ÷ 1 ÷ 2 = 10
如果要抽取三个概率是多少?
概率是:
5 × 4 × 3 ÷ 1 ÷ 2 ÷ 3 = 20
数组
一维数组
int[] arr = new int[3];
二维数组
int[ ][ ] arr = new int[3][ ];
arr[0] = new int[3];
arr[1] = new int[5];
arr[2] = new int[4];
Arrays数组工具类
public static int binarySearch(int[] a, int key)
二分查找
public static int[] copyOf(int[] original, int newLength)
把original数组拷贝到新数组并返回新数组。newLength是新数组的长度,这个方法通常来扩展数组长度。
public static String toString(Object[] a)
返回指定数组内容的字符串表示形式。
public static String deepToString(Object[] a)
返回指定数组“深层内容”的字符串表示形式。
public static boolean equals(Object[] a, Object[] a2)
如果两个指定的 Objects 数组彼此相等,则返回 true。
public static boolean deepEquals(Object[] a, Object[] a2)
如果两个指定数组彼此是深层相等 的,则返回 true。
public static int hashCode(Object[] a)
基于指定数组的内容返回哈希码。
public static int deepHashCode(Object[] a)
基于指定数组的“深层内容”返回哈希码。
String[][][] arrays = new String[3][3][3];
for(int i = 0;i < arrays.length;i++) {
for(int j = 0; j < arrays[i].length;j++) {
for(int n = 0; n < arrays[i][j].length; n++) {
arrays[i][j][n] = (i+1)*(j+1)*(n+1)+"";
}
}
}
String[][][] arrays2 = arrays;
String[][][] arrays3 = new String[3][3][3];
for(int i = 0;i < arrays3.length;i++) {
for(int j = 0; j < arrays3[i].length;j++) {
for(int n = 0; n < arrays3[i][j].length; n++) {
arrays3[i][j][n] = (i+1)*(j+1)*(n+1)+"";
}
}
}
System.out.println(Arrays.deepToString(arrays));
System.out.println(Arrays.deepHashCode(arrays));
System.out.println(Arrays.equals(arrays, arrays2));
System.out.println(Arrays.equals(arrays, arrays3));
System.out.println(Arrays.deepEquals(arrays, arrays2));
System.out.println(Arrays.deepEquals(arrays, arrays3));
结果:
deepToString: [[[1, 2, 3], [2, 4, 6], [3, 6, 9]], [[2, 4, 6], [4, 8, 12], [6, 12, 18]], [[3, 6, 9], [6, 12, 18], [9, 18, 27]]]
deepHashCode: 179164111
equals: true
equals: false
deepEquals: true
deepEquals: true
数组拷贝
//数组的拷贝
int[] numbers = {77,99,101,124,66,33};
numbers = Arrays.copyOf(numbers, 7);
for(int i =0;i<numbers.length;i++) {
System.out.println(numbers[i]); //77 99 101 124 66 33 0
}
Comparator数组排序
//public static <T> void sort(T[] a,Comparator<? super T> c)
//根据指定比较器产生的顺序对指定对象数组进行排序。
public class Human {
String name;
int age;
public Human(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Student extends Human {
public Student(String name, int age) {
super(name,age);
this.name = name;
this.age = age;
}
}
public class HumanComparator implements Comparator<Human>{
@Override
public int compare(Human o1, Human o2) {
return -(o1.age - o2.age);
}
}
public class Test {
public static void main(String[] args) throws IOException {
Student s1 = new Student("周杰伦",23);
Student s2 = new Student("张学友",35);
Student s3 = new Student("李沁",17);
Human[] humans = {s1,s2,s3};
Arrays.sort(humans, new HumanComparator());
for (Human stu : humans) {
System.out.print(stu.getName()+"---");//张学友---周杰伦---李沁---
}
}
}
数组排序方法
public class Test {
public static void main(String[] args) {
int[] num1 = {1,44,23,1,16};
int[] num2 = {3,66,99,2,1};
int[] num3 = {68,60,70,90,80};
int[] num4 = {124,256,36,64,666};
bubbleSort(num1);
selectSort(num2);
insertSort(num3);
Arrays.sort(num4);//Arrays.sort排序
printArrays("Arrays排序", num4);
}
//冒泡
public static void bubbleSort(int[] num) {
for(int i = 0; i < num.length-1; i++) {
for(int j = i + 1; j < num.length;j++) {
if(num[i] > num[j]) {
int temp = 0;
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
printArrays("冒泡排序", num);
}
//选择排序
public static void selectSort(int[] num) {
for(int i = 0; i < num.length-1; i++) {
int min = i;
for(int j = i+1;j<num.length;j++) {
if(num[j] < num[min]) {
min = j;
}
}
if(min!=i){
int temp = 0;
temp = num[min];
num[min] = num[i];
num[i] = temp;
}
}
printArrays("选择排序", num);
}
//插入排序
public static void insertSort(int[] num) {
for(int i = 1; i < num.length; i++) {
for(int j = i; j > 0; j--) {
if(num[j]<num[j-1]) {
int temp = 0;
temp = num[j];
num[j] = num[j-1];
num[j-1] = temp;
}
}
}
printArrays("插入排序", num);
}
public static void printArrays(String sort, int[] num) {
System.out.print(sort+": ");
for (int i : num) {
System.out.print(i+" ");
}
System.out.println();
}
}
二维数组打印一个三角形
//数组打印一个三角形
int[][] num = new int[9][];
for(int x = 0; x <9;x++) {
num[x] = new int[x+1];
}
/*
for(int n = 0; n < num.length; n++) {
for(int k = 0; k < num[n].length; k++) {
if(n<=1) {
num[n][k] = 1;
} else {
if(k==0||k==num[n].length-1) {
num[n][k] = 1;
}else{
num[n][k] = num[n-1][k-1]+num[n-1][k];
}
}
}
}*/
for(int n = 0; n < num.length; n++) {
for(int k = 0; k < num[n].length; k++) {
int odds = 1;
for(int i = 1; i <= k; i++) {
odds = odds*(n-i+1)/i;
}
num[n][k] = odds;
}
}
for (int[] is : num) {
System.out.println();
for (int i : is) {
System.out.print(i + " ");
}
}
结果:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1