Java数组全攻略(二)

例题分析

1.传入函数内一个值,找到其对应的数组角标

public class Rewriter {
    public static int fun(int num) {
        int[] a =new int[] {1,2,3,4,5};
        for (int i = 0; i < a.length; i++) {
            if (a[i] == num) {
                return i;
            }
        }
        return -1;      //当传入的数值不在数组时,返回 -1
    }
    public static void main(String[] args) {    
        int num =fun(4);
        System.out.println(num);
    }
}
输出 :3

2.利用同一个数组让其内元素反转

public class Rewriter {
    public static void fun() {
        int[] a =new int[] { 1, 2, 3, 4, 5};
        for (int i = 0; i < a.length/2; i++) {
            int temp = a[a.length-1-i];           // 利用中间值重新分配两个数
            a[a.length -1-i] = a[i];
            a[i] = temp;
        }
        System.out.println(Arrays.toString(a));  // 利用Arrays中的toString方法,遍历a数组,让其以字符串形式输出
    }

    public static void main(String[] args) {            
        fun();
    }
}
输出:  [5, 4, 3, 2, 1]

3.冒泡循环

相邻两个数进行对比, 交换两个数的位置

public class Rewriter {
    public static void main(String[] args) {            
        int[] a = new int[] {5,4,3,2,1};
        for (int i = 0; i < a.length - 1; i++) {       
        //相邻两个数进行比较 一共会比较 a.length -1 次
            for (int j = 0; j < a.length - 1 - i; j++) {    
            //内循环比每趟的次数 每趟的次数都会比上一趟 -1    
                if (a[j] > a[j+1]) {
                    int temp = a[j+1];
                    a[j+1] = a[j];
                    a[j] = temp;
                }
            }
        }System.out.println(Arrays.toString(a));
    }
}
输出: [1, 2, 3, 4, 5]

4.选择排序

选择一个数和接下来的每一个数比较 交换位置 以此类推

public class Rewriter {
    public static void main(String[] args) {            
        int[] a = new int[] {5,4,3,2,1};
        for (int i = 0; i < a.length - 1; i++) {                
        //第一次 首选数和后面所有数依次比较 循环4次 第二次 3次
            for (int j = i+1; j < a.length ; j++) {  
            //第一趟比较了4次 第二趟比较了3次             
                if (a[i] > a[j]) {
                    int temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }
        }System.out.println(Arrays.toString(a));
    }
}

5.查找游戏

随机生成一个数[1,100], 用户输入一个数,如果比这个数大 输出大了,让用户重新输出,直到输入正确为止

public class Rewriter {
    public static void main(String[] args) {    
        System.out.println("请输入一个1~100之内的数");
        Scanner scanner = new Scanner(System.in);
        int num = (int) (Math.random()*100+1);      
        while (true) {
            int n = scanner.nextInt();
            if (n > num) {
                System.out.println("你输入的数大了");
            }else if (n < num) {
                System.out.println("你输入的数小了");
            }else {
                System.out.println("正确");
                break;
            }
        }   
    }
}

6.折半查找

在一个有序数组中,输入一个元素值或者角标 可以查找到对应的角标或值

public class Rewriter {
    public static void fun(int[] a,int b) { 
        //传入数组 和 传入需要查询角标的元素值
        int max = a.length - 1;     
        //最大角标
        int min = 0;                
        //最小角标
        int mid = (max + min) / 2;  
        //中间角标
        while (b != a[mid]) {
            if (b > a[mid]) {   
                //当需要查找的角标比中间值大
                min = mid + 1;
            }else if (b < a[mid]) { 
                //当需要查找的角标比中间值小
                max = mid -1;
            }
            mid = (max + min) / 2;  
            //重新定义中间值
            if (max < min) {        
                //当最大角标 小于或等于最小角标说明这个值不存在
                mid = -1;
            }
        }
        System.out.println(mid);
    }

    public static void main(String[] args) {    
        int[] a = new int[] {1,5,10,15,20,25,30,35};
        fun(a, 30);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值