java:给你一个数组和两个索引,交换下标为这两个索引的数字

给你一个数组和两个索引,交换下标为这两个索引的数字

 

import java.util.Arrays;

public class Solution {
      public static void main(String args[]) {
            int[] Array = { 3, 2, 1, 4, 5 };
		SwapArray testArray = new SwapArray();
		testArray.swapIntegers(Array, 0, 2);
	}

}

class SwapArray {
	/**
	 * 给你一个数组和两个索引,交换下标为这两个索引的数字
	 */
	public void swapIntegers(int[] array, int index1, int index2) {
		int tmp = array[index2];
		array[index2] = array[index1];
		array[index1] = tmp;
		System.out.println(Arrays.toString(array));
	}
}

  

posted @ 2019-03-15 13:43  南非波波  阅读(1650)  评论(0编辑  收藏  举报