当java数组调用一个方法,将数组传进去
public static void main(String[] args) {
int arr[] = { 6,1,2,3,4,5 };
sort(arr, arr.length);
for (int i = 0; i < arr.length; i++)
{
System.out.println(arr[i]);
}
}
下面是冒泡排序
public static void sort(int arr[], int count)
{
for (int i = 0; i < count-1; i++)
{
int tem = 0;
for (int j = 0; j < count-i-1; j++)
{
if (arr[j] < arr[j + 1]) {
tem = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tem;
}
}
}
}
控制台打印
数组作为引用数据类型作为参数时,传递的实际是数组的地址值,而不是数组本身的值。所以当形参数组的值改变时,实参的值也随之改变
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)