java intercambiar la posicion de valores de un array code example

Example 1: java intercambiar la posicion de valores de un array

int lastElement = values[values.length-1];
values[values.length-1] = values[0];
values[0] = lastElement;

Example 2: java intercambiar la posicion de valores de un array

public class ArrayMethods {

private int[] values;
public ArrayMethods(int[] initialValues) {
values = initialValues;
}
public void swapFirstAndLast() {
int lastvalplace = values.length;
int firstval = values[0];
values[0] = values[lastvalplace];
values[lastvalplace] = firstval;
}

public static void main(String[] args) {
ArrayMethods initialValues = new ArrayMethods(int[] 50, 32, 4, 9, 2);
swapFirstAndLast = new swapFirstAndLast(values);
}

}

Tags:

Misc Example