Here you can find the source of swap(T[] array, int indexOne, int indexTwo)
Parameter | Description |
---|---|
T | Class type of the elements in the array. |
array | array with the elements to swap. |
indexOne | index of the first element to swap. |
indexTwo | index of the second element to swap. |
Parameter | Description |
---|---|
ArrayIndexOutOfBoundsException | if the indexes are not valid indexes in the array. |
NullPointerException | if the array is null. |
public static <T> T[] swap(T[] array, int indexOne, int indexTwo)
//package com.java2s; /*//from ww w.j a v a 2 s . co m * Copyright 2016 Author or Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Swaps elements in the array at indexOne and indexTwo. * * @param <T> Class type of the elements in the array. * @param array array with the elements to swap. * @param indexOne index of the first element to swap. * @param indexTwo index of the second element to swap. * @return the array with the specified elements at indexOne and indexTwo swapped. * @throws ArrayIndexOutOfBoundsException if the indexes are not valid indexes in the array. * @throws NullPointerException if the array is null. */ public static <T> T[] swap(T[] array, int indexOne, int indexTwo) { T elementAtIndexOne = array[indexOne]; array[indexOne] = array[indexTwo]; array[indexTwo] = elementAtIndexOne; return array; } }