Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在我的数组中交换index1和index2的两个数字_Java_Arrays_Swap - Fatal编程技术网

Java 如何在我的数组中交换index1和index2的两个数字

Java 如何在我的数组中交换index1和index2的两个数字,java,arrays,swap,Java,Arrays,Swap,这是我的代码,对于如何将index1处的数组值与index2处的数组值交换,我感到困惑 public static void swapByIndex(int myArray[], int index1, int index2) { int position1 = myArray[index1]; int position2 = myArray[index2]; for(int i = 0; i < myArray.length; i++) {

这是我的代码,对于如何将index1处的数组值与index2处的数组值交换,我感到困惑

    public static void swapByIndex(int myArray[], int index1, int index2) {
    int position1 = myArray[index1];
    int position2 = myArray[index2];
    
    for(int i = 0; i < myArray.length; i++) {
        if(i == position1) {
            myArray[i] = position2;
        }
        if(i == position2) {
            myArray[i] = position1;
        }
        System.out.print(myArray[i] + " ");
    }
}
publicstaticvoidswapbyindex(int-myArray[],int-index1,int-index2){
int position1=myArray[index1];
int position2=myArray[index2];
for(int i=0;i
实现这一点非常简单和常见的方法是:

public static void swapByIndex(int myArray[], int index1, int index2) {
   int temp = myArray[index1];
   myArray[index1] = myArray[index2];
   myArray[index2] = temp;
}

另外,此解决方案的时间复杂度为O(1),但您的解决方案的时间复杂度为O(n)。

执行交换的经典方法只是在一个位置被更改时使用一个临时int变量来获取该位置的值,因此当您要将其分配给另一个位置时,您仍然可以使用它

    public static void swapByIndex(int myArray[], int index1, int index2) {
        int temp = myArray[index1];
        myArray[index1] = myArray[index2];
        myArray[index2] = temp;

}

交换元素时,不需要迭代整个数组。只需获取第三个变量并在其中存储第一个索引的值,就可以交换它们。然后,将第二个索引值存储在第一个索引中。然后,将第三个变量的值存储在第二个索引中

public static void swapByIndex(int myArray[], int index1, int index2) {
    int temp = myArray[index1];
    myArray[index1] = myArray[index2];
    myArray[index2] = temp;
}

下面是交换工作原理的快速说明

publicstaticvoidswapbyindex(int-myArray[],int-index1,int-index2){
int position1=myArray[index1];
int position2=myArray[index2];
//此时,您已经知道索引和值。
//您可以直接指定值。
myArray[index1]=位置2;
myArray[index2]=位置1;
}
现在,一旦您有了这个,您可能会注意到,我们只需要将其中一个值存储在一个临时变量中。我们可以将其进一步简化为更常见的形式:

publicstaticvoidswapbyindex(int-myArray[],int-index1,int-index2){
int position1=myArray[index1];
//覆盖存储在索引1中的值
myArray[index1]=myArray[index2];
//使用存储的值填写第二个索引
myArray[index2]=位置1;
}
这就是我们如何获得交换函数的最常见版本的方法

公共静态无效交换(int[]数组,int a,int b){
int tmp=数组[a];
数组[a]=数组[b];
数组[b]=tmp;
}
如果您刚刚开始学习java,我不建议您使用它,但您也可以使用适用于任何类型数组的通用版本

公共静态无效交换(T[]数组,int a,int b){
T tmp=阵列[a];
数组[a]=数组[b];
数组[b]=tmp;
}

感谢您的回复,我应该声明我不确定是否允许使用.toString方法,那么我如何将其打印为数组而不是字符串?@apex这完全是另一个问题。请参阅示例(以及该链接其他答案中的一些其他想法)。感谢您的回复,我应该声明我不确定是否允许使用.toString方法,那么如何将其打印为数组而不是字符串?打印数组:
System.out.println(Arrays.toString(myArray))感谢您的回复,我应该声明我不确定是否允许使用.toString方法,那么如何将其打印为数组而不是字符串?感谢您的回复,我应该说,我不确定是否允许使用.toString方法,那么我如何将其打印为数组而不是字符串?通常最好的方法是使用
数组.toString(数组)
,但如果不能使用,则尝试创建for循环并打印每个元素<代码>用于(int i=0;i