Java数组中部删除中部插入
public class Array {
public static int getRealNum(Object[] objs) {
int len = 0;
for (int i = 0; i < objs.length; i++)
{
Object item = objs[i];
if (item == null) {
break;
}
len++;
}
return len;
}
public static void append(Object[] objs,Object obj) {
int realNum = getRealNum(objs);
int length = objs.length;
if (realNum == length) {
return;
}
objs[realNum] = obj;
}
public static Object pop(Object[] objs) {
int realNum = getRealNum(objs);
if (realNum == 0) {
return null;
}
int lastIdx = realNum - 1;
Object obj = objs[lastIdx];
objs[lastIdx] = null;
return obj;
}
public static void insert(Object[] objs, int insertIndex, Object obj) {
int realNum = getRealNum(objs);
if (insertIndex >= realNum) {
return;
}
if (realNum == objs.length) {
return;
}
int lastIdx = realNum - 1;
for (int i = lastIdx; i >= insertIndex; i--)
{
Object o = objs[i];
objs[i + 1] = o;
}
objs[insertIndex] = obj;
}
public static Object pop(Object[] objs,int delIndex) {
int realNum = getRealNum(objs);
if (delIndex >= realNum) {
return null;
}
Object delItem = objs[delIndex];
int lastIndex = realNum - 1;
for (int i = delIndex; i <= lastIndex-1; i++)
{
Object o = objs[i + 1];
objs[i] = o;
}
objs[lastIndex] = null;
return delItem;
}
}
|