方法参数传递的案例
package testone;
public class HelloWorld {
public static void main(String[] args) {
//打印数组的内容
int[] arr = {1, 3, 4, 2};
printArray(arr);
System.out.println("-------------------");
int[] arr2 = null;
printArray(arr2);
System.out.println("-------------------");
int[] arr3 = {};
System.out.println(arr3.length); //0
printArray(arr3);
}
public static void printArray(int[] arr) {
System.out.print("[");
if (arr != null && arr.length > 0) {
for (int i = 0; i < arr.length; i++) {
// if (i== arr.length-1){
// System.out.println(arr[i]);
// }else {
// System.out.println(arr[i]+",");
// }
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
}
}
System.out.println("]");
}
}
package testone;
public class MethodTest4 {
public static void main(String[] args) {
//查询数组的索引并返回,数据不存在返回-1
int[] arr = {3, 5, 8, 9, 1};
int index = searchIndex(arr, 7);
System.out.println(index);
}
//定义一个方法,参数接受数组和要查询的数据,返回int类型
public static int searchIndex(int[] arr, int data) {
//找出这个数的索引
for (int i = 0; i < arr.length; i++) {
if (arr[i] == data) {
return i;
}
}
//查无此元素
return -1;
}
}
package testone;
public class CompareArr {
public static void main(String[] args) {
//比较任意两个数组的值是否相等,相等返回true
int[] arr11 = {1, 55, 3};
int[] arr22 = {1, 5, 3};
System.out.println(compare(arr11, arr22));
}
//定义一个数组,参数传两个数组,返回类型是布尔类型
public static boolean compare(int[] arr1, int[] arr2) {
//判断两个数组的内容是否一样
if (arr1.length == arr2.length) {
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
//遍历结束发现没有不相等的,返回true
return true;
} else {
return false;
}
}
}
return跳出当前方法,立即结束所在方法的执行
package testone;
public class ReturnDemo {
public static void main(String[] args) {
//return关键字的作用
chu(10, 0);
}
public static void chu(int a, int b) {
if (b == 0) {
System.out.println("分母不能是0");
return; //跳出当前方法,立即结束所在方法的执行
}
int c = a / b;
System.out.println("结果是" + c);
}
}
package testone;
import java.util.Scanner;
public class TestDemo1 {
public static void main(String[] args) {
//录入信息调用方法
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票原价:");
double price = sc.nextDouble();
System.out.println("请输入月份:");
int month = sc.nextInt();
System.out.println("请输入舱位类型(头等舱,经济仓):");
String type = sc.next();
double rs = cacl(price, month, type);
System.out.println("您当前购买机票的价格" + rs);
}
public static double cacl(double money, int month, String type) {
//判断月份是淡季还是旺季
//区间判断使用if
if (month > 5 && month <= 10) {
//遇到判断值匹配使用switch
switch (type) {
case "经济舱":
money *= 0.85;
break;
case "头等舱":
money *= 0.9;
break;
default:
System.out.println("你输入的舱位不正确");
money = -1; //当前无法计算价格
}
} else if (month == 11 || month == 12 || (month >= 1 && month <= 4)) {
switch (type) {
case "经济舱":
money *= 0.65;
break;
case "头等舱":
money *= 0.7;
break;
default:
System.out.println("你输入的舱位不正确");
money = -1; //当前无法计算价格
}
} else {
System.out.println("月份有问题");
money = -1;
}
return money;
}
}
package testone;
public class TestDemo2 {
public static void main(String[] args) {
//求101到200之间的素数
for (int i = 101; i < 200; i++) {
//信号位标记
boolean flag = true;
//判断遍历的这个数是否是素数
for (int j = 2; j < i / 2; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
//是素数则输出
if (flag) {
System.out.print(i + "\t");
}
}
}
}
Array常用方法
package d7_Array;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
//学会使用Array类的常用API
int[] arr = {1, 4, 6, 7, 2};
System.out.println(arr); //打印的是地址
//1返回数组内容
System.out.println(Arrays.toString(arr)); //[1, 4, 6, 7, 2]
//2排序的API,默认进行升序排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
//3二分搜索技术(前提拍好顺序,否则出bug)
int index = Arrays.binarySearch(arr, 2);
System.out.println(index);
}
}
自定义数组的排序规则,comparator比较器对象
package d7_Array;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayDemo2 {
public static void main(String[] args) {
//目标;自定义数组的排序规则,comparator比较器对象
int[] ages = {23, 4, 5, 2, 33, 66};
//Arrays的sort是对于有值特性的数组默认升序排序
Arrays.sort(ages);
System.out.println(Arrays.toString(ages));
//降序排序(自定义比较器对象,只能支持引用类型的排序入Integer)
Integer[] age1 = {1, 22, 6, 8, 4, 3};
/**
* 参数一;被排序的数组必须是引用类型的元素
* 参数二;匿名内部类对象,代表了一个比较器对象
*/
// Arrays.sort(age1, new Comparator<Integer>() {
// @Override
// public int compare(Integer o1, Integer o2) {
// //指定比较规则
//// if (o1>o2){
//// return 1;
//// }else if (o1<o2){
//// return -1;
//// }else{
//// return 0;
//// }
//// return o1-o2; //升序
// return o2 - o1; //降序
//
// }
// });
Arrays.sort(age1,(Integer o1, Integer o2) -> {return o2-o1;});
System.out.println(Arrays.toString(age1));
System.out.println("----------------------");
Student[] students = new Student[3];
students[0] = new Student("xiaowang", 23, 1.86);
students[1] = new Student("xiao2", 43, 1.66);
students[2] = new Student("xiao3", 26, 1.70);
System.out.println(Arrays.toString(students)); //需要重写Student类中的方法,toString,打印结果:[Student{name='xiaowang', age=23, height=1.86}, Student{name='xiao2', age=43, height=1.66}, Student{name='xiao3', age=26, height=1.7}]
// Arrays.sort(students); //直接运行崩溃,三个参数,不知道怎么排
Arrays.sort(students, new Comparator<Student>() { //@FunctionalInterface有这个标志可以使用lambda表达式
@Override
public int compare(Student o1, Student o2) {
//自己指定比较规则
// return o1.getAge()-o2.getAge(); //按照年龄升序排序
return Double.compare(o1.getHeight(), o2.getHeight()); //比较浮点型按照身高升序排列
}
});
System.out.println(Arrays.toString(students));
}
}