私はspliceと同じ機能を持つメソッドを作成することになりますが、適切なインデックスに適切な値を取得できません。私のコードは次のように動作しますが、私は後で私の主な方法でコマンドを与えるときスプライスのようなJavaのメソッドを書く方法
public void PlaceElementAt(int newValue, int index) throws ArrayIndexOutOfBoundsException {
//check that index is valid
if (index >= 0 && index <= data.length) { //Checks that the index is within position 0 and 7 by default.
System.out.println ("Index is valid"); //returns index is valid if so
}
//increase size if necessary
if (data.length == numElements) { //checking if the number of elements is filling the spaces
doubleCapacity(); // calls upon the double capacity method if it is
}
if (numElements==0) {
data[numElements] = newValue;
System.out.println ("Element: " + data[numElements] + " at index: " + index);
numElements++;
}
//shuffle values down from index
else {
int bottompos = numElements-1;
int loopcount = numElements-index;
int NewBottom = numElements+1;
for (int i=0; i<loopcount; i++){
data[bottompos]=data[NewBottom];
bottompos--;
NewBottom--;
}
//insert newValue at index
data[numElements] = newValue;
System.out.println ("Element: " + data[numElements] +" at index: " + index);
numElements++;
}
}
は私の問題は明らかです。
myData.PlaceElementAt(3,0)
myData.PlaceElementAt(2,5)
myData.PlaceElementAt(7,3)
私は私のブレークポイントをチェックしたら、私は値が表示さ配列に追加されていますが、インデックス0から1から1の基準で追加されています。どんな提案も大きな助けになります。
Javaはスプライス方法を持っていません。 – shmosel
@shmosel私は彼が1つを作ろうとしていると思う。少なくとも、私は彼の問題の記述から集めたものです。 –
@HypnicJerkコメントを理解するには、[改訂履歴](http://stackoverflow.com/posts/42496438/revisions)を参照してください。 – shmosel