java.util.Arrays.sort()数组形参传递为啥能修改原始数组的内容?

查看:103
本文介绍了java.util.Arrays.sort()数组形参传递为啥能修改原始数组的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

如题java.util.Arrays.sort(int[] a)数组形参传递为啥能修改原始数组的内容?

[public static void sort(int[] a)](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[]))

Sorts the specified array into ascending numerical order.

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.

Parameters:a - the array to be sorted

解决方案

1、对于int[] a,变量a是一个对象引用,可以理解为保存数组对象在堆中的地址(数组中的具体元素都保存在堆中);
2、sort()中传入变量a的引用值,即1中说的数组在堆内的地址值,利用a的值,当然可以访问到在堆内的数组,进而程序可以修改数组内元素的值和顺序了。

这篇关于java.util.Arrays.sort()数组形参传递为啥能修改原始数组的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆