我可以在Java中使用swap函数交换两个int []吗? [英] Can I swap two int[] using swap function in java?

查看:127
本文介绍了我可以在Java中使用swap函数交换两个int []吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个2d数组int []
现在,我想在2D数组内交换两个int []



我有这个代码:

  swap(values [row] [col],values [randomRow] [randomCol]); 

其中,值是int []的2D数组。
,所以values [int] [int]是int [];



我收到这样的错误消息:

 错误:类型ShufflePic 


我该如何解决?



非常感谢!

解决方案

Java是传递值。您不能像这样交换值。



相反,请使用这种方法:

  void swap(int [] [] array,int row1,int col1,int row2,int col2){
int temp = array [row1] [col1];
array [row1] [col1] = array [row2] [col2];
array [row2] [col2] = temp;
}

现在您可以调用 swap(...) 交换值的方法

  swap(values,row,col,randomRow,randomCol); 


I created a 2D array of int[] Now, I want to swap two int[] inside of 2D array

I have this for my code:

swap(values[row][col], values[randomRow][randomCol]); 

where values is a 2D array of int[]. so values[int][int] is a int[];

I get an error message like this:

Error: The method swap(int[], int[]) is undefined for the type ShufflePic

How should I fix this?

Thanks a lot!

解决方案

Java is pass-by-value. You cannot swap values like this.

Instead use this approach :

void swap(int[][] array, int row1, int col1, int row2, int col2) {
    int temp = array[row1][col1];
    array[row1][col1] = array[row2][col2];
    array[row2][col2] = temp;
}

Now you can call swap(...) method to swap the values

swap(values, row, col, randomRow, randomCol);

这篇关于我可以在Java中使用swap函数交换两个int []吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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