Java 在数组中插入一个元素,并返回索引。如果数组已满,则重新分配数组
我需要实现一个方法public int insert(int x),该方法在“elements”数组中插入x,使其保持有序,并返回输入的索引; 如果元素已经存在,不要插入x并返回-1;如果数组已满,它会将“elements”数组重新分配到一个大小为两倍的数组中,然后插入项x。 我知道API中可能已经有类似的方法,但是应该实现它 我写了这个,但我不知道如何完成它Java 在数组中插入一个元素,并返回索引。如果数组已满,则重新分配数组,java,arrays,insert,indexing,Java,Arrays,Insert,Indexing,我需要实现一个方法public int insert(int x),该方法在“elements”数组中插入x,使其保持有序,并返回输入的索引; 如果元素已经存在,不要插入x并返回-1;如果数组已满,它会将“elements”数组重新分配到一个大小为两倍的数组中,然后插入项x。 我知道API中可能已经有类似的方法,但是应该实现它 我写了这个,但我不知道如何完成它 public int insert(int x) { if(numElements == elements.length) {
public int insert(int x) {
if(numElements == elements.length) {
//code
}
int pos = binarySearch(x);
if(pos != -1)
return -1;
if(elements[-(pos + 1)] != null) {
for(int i = numElements; i >= -(pos + 1); i--)
elements[i + 1] = elements[i];
}
elements[-(pos + 1)] = x;
numElements++;
return -(pos + 1);
}
但是,我不知道我写的内容是否正确,我错过了完整数组的例子。有人能帮我吗?
谢谢
private int-binarySearch(intx){
int-inf=0;
int sup=数值-1;
if(sup==-1 | |元素[0]>x)
返回-1;
if(元素[sup]>1;//按到期日划分
if(元素[i]>x)
sup=i-1;
else if(元素[i]
给你
我所做的是创建一个两倍大小的新数组,然后使用System.arraycopy
移动现有元素。最后,我将放大数组的引用指定给elements
变量
我使用了System.arraycopy
,因为它比手动创建的循环拷贝快。它使用JVM的本机实现
我还用System.arraycopy
替换了数组位移检查和for循环。
而且-(pos+1)会给您一个负数
public int insert(int x) {
if(numElements == elements.length) {
int[] enlargedElements = new int[elements.length * 2];
System.arraycopy(elements, 0, enlargedElements, 0, elements.length);
elements = enlargedElements;
}
int pos = binarySearch(x);
if(pos == -1)
return -1;
if(pos < numElements) {
System.arraycopy(elements, pos, elements, pos + 1, numElements - pos);
}
elements[pos] = x;
numElements++;
return pos;
}
private int binarySearch(int x) {
int inf = 0;
int sup = numElements - 1;
if(sup == -1 || elements[0] > x)
return 0;
if(elements[sup] < x)
return numElements;
//Invariante di ciclo: se x in a, allora x in a[inf...sup]
while(inf <= sup) {
int i = (inf + sup) >>> 1; //divide per due
if(elements[i] > x)
sup = i - 1;
else if(elements[i] < x)
inf = i + 1;
else
return -1;
}
return inf;
}
public int insert(int x){
if(numElements==elements.length){
int[]放大数据=新的int[elements.length*2];
System.arraycopy(元素,0,放大倍数,0,元素.长度);
元素=放大或删除;
}
int pos=二进制搜索(x);
如果(位置==-1)
返回-1;
如果(位置<数值){
系统阵列副本(元素,位置,元素,位置+1,数值元素-位置);
}
元素[pos]=x;
numElements++;
返回pos;
}
私有int二进制搜索(intx){
int-inf=0;
int sup=数值-1;
if(sup==-1 | |元素[0]>x)
返回0;
if(元素[sup]>1;//按到期日划分
if(元素[i]>x)
sup=i-1;
else if(元素[i]
您需要重新分配元素
并使用System.arraycopy
将元素复制到新数组中。不要忘记数组也会收缩的情况。是否使用“-(pos+1)”而不是“0”?pos将为-1,因此如果您写入元素[0],则会更简单如果我理解正确。谢谢,但不起作用。当我创建长度为4的数组并插入2时,它被插入到位置0(确定),然后我插入位置0(确定)的1,然后当我插入2时,会启动ArrayInxexOutOfBoundsException。为什么?binarySearch
会返回未找到时应该插入的位置,如果找到则返回-1?对吗?@Andrei no,不起作用。当我在空数组中输入数字时,它总是返回-1。这是正确的,因为它运行binarySearch如果数组(空数组)中不存在所需的元素,则返回-1。感谢您的耐心。还有一个问题。我创建了一个长度为4的数组。我插入值2->[2,/,/,/,/]我插入值3->[2,3,/,/]插入值1->[1,2,0,/]程序包含数字1时出现问题…请立即尝试。它没有复制最后一个元素。
public int insert(int x) {
if(numElements == elements.length) {
int[] enlargedElements = new int[elements.length * 2];
System.arraycopy(elements, 0, enlargedElements, 0, elements.length);
elements = enlargedElements;
}
int pos = binarySearch(x);
if(pos == -1)
return -1;
if(pos < numElements) {
System.arraycopy(elements, pos, elements, pos + 1, numElements - pos);
}
elements[pos] = x;
numElements++;
return pos;
}
private int binarySearch(int x) {
int inf = 0;
int sup = numElements - 1;
if(sup == -1 || elements[0] > x)
return 0;
if(elements[sup] < x)
return numElements;
//Invariante di ciclo: se x in a, allora x in a[inf...sup]
while(inf <= sup) {
int i = (inf + sup) >>> 1; //divide per due
if(elements[i] > x)
sup = i - 1;
else if(elements[i] < x)
inf = i + 1;
else
return -1;
}
return inf;
}