public class Test03 { public int[] insert(int arr[], int insertNum){ Arrays.sort(arr);//数组排序 System.out.println(Arrays.toString(arr)); //复制 int[] b = new int[arr.length + 1];//java python数组是固定长 for(int i = 0 ; i < arr.length;i++){ b[i] = arr[i]; } //直接插入 for(int i = 0; i < arr.length; i++){ if(insertNum <= arr[i] ){ b[i+1] = b[i];//右移 b[i] = insertNum;//插入 } } System.out.println(Arrays.toString(b)); return b; } public static void main(String[] args) { Test03 t = new Test03(); int[] a = {2,1,4,5,79,1}; t.insert(a, 9); } }
public static void main(String[] args) { //随机生成10个整数(1-100)保存到数组中 int[] arr = new int[10]; int sum = 0; double avg = 0; int max = 0,index = 0; int flag = 0; for(int i = 0; i < arr.length; i++){ arr[i] = 1 + (int)(Math.random() * 100); //random= (0,1) sum += arr[i]; avg = sum/arr.length;//平均值 if(arr[i] == 8){//判断是否有8 flag = 1; }else{ flag = 0; } } System.out.println("原始数组为:" + Arrays.toString(arr)); int[] b = new int[arr.length]; for(int i = 0; i < arr.length; i++){ b[i] = arr[arr.length - 1 - i]; } for(int i = 0; i< arr.length; i++){//最大值与最大值下标 for(int j = i;j < arr.length; j++){ if(arr[i] >= arr[j]){ max = arr[i]; index = i; }else{ max = arr[j]; index = j; } } } System.out.println("倒序输出为:" + Arrays.toString(b)); System.out.println("平均值为:" + avg); System.out.println("最大值为:" + max); System.out.println("最大值下标为:" + index); if(flag == 1){ System.out.println("数组中有8"); }else{ System.out.println("数组中没有8"); } }
public int[] maopaoSort(int[] arr){ for(int i = 0; i < arr.length;i++ ){ for(int j = 0 ; j < arr.length - i -1;j++ ){ if(arr[j] >= arr[j+1]){//交换 int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } System.out.println(Arrays.toString(arr)); return arr; } public static void main(String[] args) { int[] a = new int[]{1,45,3,34,6,24}; Test03 t = new Test03(); t.maopaoSort(a); }
public class AA { public boolean isOdd(int a ){ //奇数为true,偶数为false if(a % 2 != 0){ System.out.println(true); return true; }else{ System.out.println(false); return false; } } public static void main(String[] args) { new AA().isOdd(6); } }
public static void main(String[] args) { char[][] c = new char[][]{{'1','a'},{'*','%'},{'#','b','?'}}; System.out.println(Arrays.deepToString(c)); System.out.println("请输入所要打印的行数和列数"); Scanner sc = new Scanner(System.in); int col = sc.nextInt(); int row = sc.nextInt(); System.out.println("相对应的字符为:" + c[col -1][row -1]); sc.close(); }
public class Methods { public int m(int x){ return x * x; } public int m(int x,int y){ return x * y; } public String m(String s){ System.out.println(s); return s; } public static void main(String[] args) { System.out.println(new Methods().m(3)); System.out.println(new Methods().m(3, 4)); new Methods().m("hello world"); } }
public void max(int i, int j){ System.out.println((i >= j)? i : j); } public void max(double i, double j){ System.out.println((i >= j)? i : j); } public void max(double i, double j, double k){ System.out.println((((i >= j)? i : j) >= k) ? ((i >= j)? i : j) : k); }
知识点:可变参数(参数类型…形参名)(接收0-多)
使用可变参数时,可以当作数组来使用,即用形参名.
public class HspMethod { public String showScore(String name, double...score){ double totalScore = 0; for(int i = 0; i < score.length; i++){ totalScore += score[i]; } return name + "有" + score.length + "门课的成绩总分为=" + totalScore ; } public static void main(String[] args) { HspMethod hm = new HspMethod(); System.out.println(hm.showScore("张三", 80.4)); System.out.println(hm.showScore("李四", 20.1,22.1,23.1)); } }
public class Person { String name; int age; public Person(){ age = 18; } public Person(String pName, int pAge){ name = pName; age = pAge; } public static void main(String[] args) { Person p1 = new Person(); System.out.println("p1的信息 name = " + p1.name + " age=" + p1.age); Person p2 = new Person("张三",20); System.out.println("p1的信息 name = " + p2.name + " age=" + p2.age); } }
public class Person { String name; int age; public Person(String name, int age){ this.name = name; this.age = age; } public boolean compareTo(Person p){ if(this.name.equals(p.name) && this.age == p.age){ return true; }else{ return false; } } }
public class TestPerson { public static void main(String[] args) { Person p1 = new Person("李四", 20); Person p2 = new Person("李四", 20); System.out.println(p1.compareTo(p2)); } }