Java examples for java.util:List Element
exchange element in a List by index
//package com.java2s; import java.util.List; public class Main { public static void main(String[] argv) { List list = java.util.Arrays.asList("asdf", "java2s.com"); int indexA = 2; int indexB = 4; exchange(list, indexA, indexB);// ww w . j a v a 2 s .co m } public static <T> void exchange(List<T> list, int indexA, int indexB) { indexA -= 1; indexB -= 1; if (indexA >= list.size() || indexB >= list.size() || indexA < 0 || indexB < 0 || indexA == indexB) { return; } else { T _a = list.get(indexA); T _b = list.get(indexB); list.remove(indexA); list.add(indexA, _b); list.remove(indexB); list.add(indexB, _a); return; } } }