Java中数组自定义函数

本文详细介绍了在Java中自定义的数组操作函数,包括createLinearArray()用于创建整型一维数组,swap()函数实现数组元素交换,show()函数进行数组迭代显示,show1()和show2()遍历数组,以及insertSort()、bubbleSort()和selectSort01()、selectSort02()函数分别实现插入排序、冒泡排序和选择排序。此外,还讲解了binaySearch()函数用于有序一维数组的二分查找方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.用createLinearArray()函数创造整型一维数组

 

//创造整型一维数组
    public static int[] createLinearArray(){
        Scanner sc = new Scanner(System.in);
        System.out.println("创造一维整型数组容量为:");
        int a = sc.nextInt();
        int[] arr = new int[a];
        for(int i = 0;i < arr.length;i++){
            System.out.println("请输入第"+(i+1)+"个整数装入数组");
            arr[i] = sc.nextInt();
        }
        sc.close();
        return arr;      
    }

2.用swap()函数实现数组内值的交换

//数组内值的交换
    public static void swap( int[] arr,int a,int b){
        arr[a] = arr[a]^arr[b];
        arr[b] = arr[a]^arr[b];
        arr[a] = arr[a]^arr[b];
    }

3.用show()函数实现一维数组迭代

public static void show(int[] arr){
        for(int a:arr){
            System.out.print(a+" ");
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值