import java.util.ArrayList;
import java.util.Vector;
import java.util.Arrays;
public class ImoocStudent {
public static void main(String args[]){
int array[] = {2,5,4,-2,-3,-29,20};
Arrays.sort(array);
printArr("数组排序的结果为:",array);
int index = Arrays.binarySearch(array,1);
System.out.println("元素1在第" + index + "个位置(负数表示不存在)");
int newIndex = -index -1;
array = insertElement(array,1,newIndex);
printArr("数组增加元素1",array);
}
private static void printArr(String message,int array[]){
System.out.println(message + ":[length:" + array.length + "]");
for(int i = 0;i < array.length;i++){
if(i != 0)
System.out.print(", ");
System.out.print(array[i]);
}
System.out.println();
}
private static int[] insertElement(int original[],int element,int index){
int length = original.length;
int destination[] = new int[length + 1];
System.arraycopy(original,0,destination,0,index);
destination[index] = element;
System.arraycopy(original,index,destination,index + 1,length - index);
return destination;
}
}
标签:index,java,int,System,length,添加,数组,array
From: https://www.cnblogs.com/pansidong/p/17430611.html