package base;
import java.util.Arrays;
public class Day39 {
public static void main(String[] args) {
int[] i1={9,8,7,6,5,4,3,2,1};
Arrays.sort(i1,3,5);//指定位置的排序
System.out.println(Arrays.toString(i1));
}
}
package base;
import java.util.Arrays;
public class Day39 {
public static void main(String[] args) {
int[] i1={9,8,7,6,5,4,3,2,1};
int[] i2={9,8,7,6,5,4,3,2,1};
int[] i3=Arrays.copyOf(i1,i1.length);
int[] i4=Arrays.copyOfRange(i1,2,i1.length);
int[] i5=new int[10];
int[] i6;
System.out.println(Arrays.equals(i1,i2));//deepEquals:比较二维数组内容是否相等
System.arraycopy(i1,0,i5,2,3);
i6=Arrays.copyOf(i5,i5.length);
Arrays.fill(i6,4,i6.length,2);//将一个数组全部置为 val ,或在下标范围内将数组置为 val
System.out.println("i3:"+Arrays.toString(i3));
System.out.println("i4:"+Arrays.toString(i4));
System.out.println("i5:"+Arrays.toString(i5));
System.out.println("i6:"+Arrays.toString(i6));
Arrays.sort(i3);
System.out.println(Arrays.binarySearch(i3,3));//binarySearch 二分法查找,数组必须有序,且存在此数组中,否则返回负数下标
Arrays.setAll(i3,x->i3[x]*2);
System.out.println(Arrays.toString(i3));
Arrays.parallelPrefix(i3,(x,y)->x*y);//二元迭代,对原数组内容进行二元操作
System.out.println(Arrays.toString(i3));
Arrays.parallelPrefix(i3,1,4,(x,y)->x*y);//二元迭代,对原数组指定位置的内容进行二元操作
System.out.println(Arrays.toString(i3));
}
}
/*
true
i3:[9, 8, 7, 6, 5, 4, 3, 2, 1]
i4:[7, 6, 5, 4, 3, 2, 1]
i5:[0, 0, 9, 8, 7, 0, 0, 0, 0, 0]
i6:[0, 0, 9, 8, 2, 2, 2, 2, 2, 2]
2
[2, 4, 6, 8, 10, 12, 14, 16, 18]
[2, 8, 48, 384, 3840, 46080, 645120, 10321920, 185794560]
[2, 8, 384, 147456, 3840, 46080, 645120, 10321920, 185794560]
进程已结束,退出代码 0
*/
// 冒泡排序法
int[] i1={9,7,8,6,3,4,5,2,1,0};
int temp;
// 外层循环判断我们要走多少次
for (int i=0;i<i1.length-1;i++){
//内层循环,比较两个数
for(int j=0;j<i1.length-1-i;j++){
if(i1[j]>i1[j+1]){
temp=i1[j];
i1[j]=i1[j+1];
i1[j+1]=temp;
}
}
}
System.out.println(Arrays.toString(i1));
{
//稀疏数组
int[][] i1=new int[11][11];
i1[1][2]=1;
i1[0][0]=2;
int nums=0;
int count=0;
// 输出
for (int[] i:i1){
for (int i2:i){
if (i2!=0)
nums++;
System.out.print(i2+"\t");
}
System.out.println();
}
// 转换为稀疏数组
System.out.println();
int[][] i2=new int[nums+1][3];
i2[0][0]=i1.length;
i2[0][1]=i1[0].length;
for (int i=0;i<i1.length;i++){
for (int j=0;j<i1[0].length;j++){
if(i1[i][j]!=0) {
count++;
i2[count][0] = i;
i2[count][1] = j;
i2[count][2] = i1[i][j];
}
}
}
for (int[] i:i2){
for (int i3:i) {
System.out.print(i3 + "\t");
}
System.out.println();
}
}
/*
2 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
11 11 0
0 0 2
1 2 1
*/
package oop;
public class Day39 {
static String name;
public Day39(String name){
this.name=name;//this.name是代表对象本身的name
}
}
package oop;
public class Test39 {
public Test39() {
}
public static void main(String[] args) {
System.out.println(new Day39("hhhhh").name);
}
}
//快捷键ALT+INS
注意
//object->person->Teacher+student
person per=new student();
System.out.println(per instanceof student);//true
System.out.println(per instanceof person);
System.out.println(per instanceof object);
System.out.println(per instanceof teacher);//flase
System.out.println(per instanceof String);//编译出错
package oop;
import static java.lang.Math.random;//静态导入包
public class Test1{
{
System.out.println("匿名代码块");
}
static{
System.out.println("静态代码块");
}//只会被调用一次
public Test1() {
System.out.println("构造函数");
}
public static void main(String[] args) {
Test1 test=new Test1();
Test1 test1=new Test1();
System.out.println(random());
}
}
/*静态代码块
匿名代码块
构造函数
匿名代码块
构造函数
0.33911942073635515
*/
int a=1;
int b=0;
try{//try监控区域
sout(a/b)
}catch(ArithmeticException e){//catch捕获异常(想要捕获的异常类型!)最高的异常类型是Throwable
sout("程序出现异常")
}finally{//处理善后工作
sout("finally")
}
//快捷键ctrl+alt+t
package oop;
public class Extention extends Exception {
private int i1;
public void myException(int a){
this.i1=a;
}
@Override
public String toString() {
return "Extention{" +
"i1=" + i1 +
'}';
}
}
package oop;
public class Test2 {
static void test(int a) throws Extention {
if(a>10){
throw new Extention();
}
}
public static void main(String[] args) {
try {
test(11);
} catch (Extention extention) {
extention.myException(11);
System.out.println("myException=>"+extention.toString());
}
}
}
//myException=>Extention{i1=11}