public class ArrayTools {
//数组遍历
public static String printArray (int[] arr){
StringBuffer sb = new StringBuffer("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length-1) {
sb.append(arr[i]).append("]");
} else {
sb.append(arr[i]).append(", ");
}
}
return sb.toString();
}
//查找指定元素
public static int getArrayIndex(int[] arr, int num) {
int index = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == num) {
index = i;
break;
}
}
return index;
}
}
|
|