Java方法中传递数组与改变数组值的问题

14 篇文章 0 订阅

基本类型总是按值传递。

对于对象来说,是将对象的引用也就是副本传递给了方法,在方法中只有对对象进行修改才能影响该对象的值操作对象的引用时是无法影响对象

引用,即对象的名字,即指向对象的指针。传递进去的是指针的副本,所以操作指针及它指向的对象无法影响原来指针指向的对象,我们要把指针(及其指向的对象)作为函数返回值返回,这样就能得到修改后的对象啦!

因此:
1)函数中直接对数组元素的操作属于对对象的操作,比如swap函数交换数组中某两个index上的数;再比如使用递归方法的排序算法(如归并,快排,堆排),递归函数的返回值都是void;我们直接调用排序方法就能排好序。

public class QuickSort {
    public static int[] sortArray(int[] arr){
        quickSort(arr, 0, arr.length - 1);
        return arr;
    }
    private static void quickSort(int[] nums, int l, int r){
        if (l < r) {
            int pivotIndex = partition(nums, l, r);
            quickSort(nums, l, pivotIndex -1);
            quickSort(nums, pivotIndex + 1, r);
        }
    }
    private static int partition(int[] nums, int l, int r) {
        int pivot = r;
        int i = l;
        for (int j = l; j < r; j++){
           if (nums[j] < nums[pivot]){
               swap(nums, i, j);
               i++;
           }
        }
        swap(nums, i, pivot);
        return i;
    }
    private static void swap(int[] nums, int i, int j) {
        int temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
    }
    public static void main(String[] args) {
        int[] A = {2,5,4,6};
        sortArray(A);
        for (int i = 0; i < A.length; i++) {
            System.out.println(A[i]);
        }
    }
}

2)函数中操作对数组的引用没用,改变的是另一个数组,并不是原数组本身。因此,我们写函数需要操作数组引用时,总把原数组传入,把操作后的数组作为返回值传出。

class BucketSort {

    private static final heapSort heapsort = new heapSort();
    private static int[] bucketSort(int[] arr, int bucketSize) {
        if (arr.length == 0) {
            return arr;
        }

        int minValue = arr[0];
        int maxValue = arr[0];
        for (int value : arr) {
            if (value < minValue) {
                minValue = value;
            } else if (value > maxValue) {
                maxValue = value;
            }
        }

        int bucketCount = (int) Math.floor((maxValue - minValue) / bucketSize) + 1; //桶的数量=floor((max-min)/bucketSize) + 1
        int[][] buckets = new int[bucketCount][0]; //用二元数组表示多个桶,第[0]维度表示桶index,第[1]维度表示桶中元素index

        // 利用映射函数将数据分配到各个桶中
        for (int i = 0; i < arr.length; i++) {
            int index = (int) Math.floor((arr[i] - minValue) / bucketSize); //bucketIndex
            buckets[index] = arrAppend(buckets[index], arr[i]); //通过复制原数组arr[n]到新建数组arr[n+1]实现自动扩容,O(n)
        }

        int arrIndex = 0;
        for (int[] bucket : buckets) {
            if (bucket.length <= 0) {
                continue;
            }
            // 对每个桶进行排序,这里使用了插入排序

            heapsort.heapSort(bucket);
            for (int value : bucket) {
                arr[arrIndex++] = value; //将排好序的所有桶中的元素依次赋值到原数组上
            }
        }

        return arr;
    }

    /**
     * 自动扩容,并保存数据
     *
     * @param arr
     * @param value
     */
    private static int[] arrAppend(int[] arr, int value) {
        arr = Arrays.copyOf(arr, arr.length + 1);
        arr[arr.length - 1] = value;
        return arr;

    }
    public static void main(String[] args) {
        int[] A = {2,5,4,6,1};
        A = bucketSort(A,  2);
        for (int i = 0; i < A.length; i++) {
            System.out.print(A[i] + " ");
        }
    }

}

比如这个例子,bucketSort返回了修改后的数组,我们打印这个数组,即用桶排序排好序的数组。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值