1 13 package com.tonbeller.wcf.changeorder; 14 15 import java.util.List ; 16 17 20 public class ChangeOrderUtils { 21 22 26 static boolean checkBounds(int size, int oldIndex, int newIndex) { 27 if (oldIndex < 0 || oldIndex >= size) 28 return false; 29 if (newIndex < 0 || newIndex >= size) 30 return false; 31 return true; 32 } 33 34 public static boolean move(List list, int oldIndex, int newIndex) { 35 if (!checkBounds(list.size(), oldIndex, newIndex)) 36 return false; 37 Object item = list.remove(oldIndex); 38 list.add(newIndex, item); 39 return true; 40 } 41 42 43 public static boolean move(Object [] array, int oldIndex, int newIndex) { 44 if (!checkBounds(array.length, oldIndex, newIndex)) 45 return false; 46 if (oldIndex == newIndex) 47 return true; 48 Object item = array[oldIndex]; 49 if (oldIndex < newIndex) { 50 for (int i = oldIndex; i < newIndex; i++) 51 array[i] = array[i+1]; 52 } 53 else { 54 for (int i = oldIndex; i > newIndex; i--) 55 array[i] = array[i-1]; 56 } 57 array[newIndex] = item; 58 59 return true; 60 } 61 62 public static int indexOf(Object [] array, Object item) { 63 for (int i = 0; i < array.length; i++) 64 if (item.equals(array[i])) 65 return i; 66 return -1; 67 } 68 69 } 70 | Popular Tags |