//数组复制算法 public class Test{ public static void main(String[] args){ int[] arrA = {100,800,500,600,700}; int[] arrB = new int[arrA.length]; //用for循环把A的对应下标值复制给B for(int i=0;i<arrA.length;i++){ arrB[i] = arrA[i]; } //输出arrB for(int i=0;i<arrB.length;i++){ System.out.println(arrB[i]); } } }
运行结果:
//数组查找算法 //查找学生是否存在 import java.util.Scanner; //导入扫描仪 public class Test{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("请输入你要查找的学生:"); String name = in.next(); String[] arr = {"王二","张三","李四","王五","六麻子"}; //学生姓名 int yes = arr.length; //记录一个不可能的下标 for(int i=0;i<arr.length;i++){ if(name.equals(arr[i])){ yes = i; break; } } if(yes<arr.length){ System.out.println("学生:"+name+"存在,下标值是"+yes); } else{ System.out.println("学生:"+name+"不存在"); } } }
运行结果:
//数组插入算法 import java.util.Scanner; public class Test{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int[] arr = {2,43,67,99,199,433}; System.out.println("请输入你想要插入的值:"); int num = in.nextInt(); arr[arr.length-1] = num; for(int i=arr.length-1;i>0;i--){ if(arr[i]<arr[i-1]){ int t = arr[i]; arr[i] = arr[i-1]; arr[i-1] = t; } else{ break; } } //输出 for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } } }
运行结果: