Java 如何从数组末尾移除元素并将其插入到前面
我正在使用ImplementArrayList。我想写一个方法,它接受最后一个元素并在前面插入它。到目前为止,我得到了它采取的最后一个元素,并插入它在前面,并转移一切。但是,我似乎无法删除前面插入的最后一个元素。例如:1,2,5到5,1,2,但我得到了5,1,2,5。我的replaceArray方法中缺少了一些东西,但我真的不知道是什么。谢谢你的帮助 类的构造函数:Java 如何从数组末尾移除元素并将其插入到前面,java,arrays,arraylist,data-structures,Java,Arrays,Arraylist,Data Structures,我正在使用ImplementArrayList。我想写一个方法,它接受最后一个元素并在前面插入它。到目前为止,我得到了它采取的最后一个元素,并插入它在前面,并转移一切。但是,我似乎无法删除前面插入的最后一个元素。例如:1,2,5到5,1,2,但我得到了5,1,2,5。我的replaceArray方法中缺少了一些东西,但我真的不知道是什么。谢谢你的帮助 类的构造函数: public KWArrayList() { capacity = INITIAL_CAPACITY; theDa
public KWArrayList() {
capacity = INITIAL_CAPACITY;
theData = (E[]) new Object[capacity];
}
public void replaceArray(int index, E anElement) {
for (int i = size; i > index; i--){
theData[i] = theData[i - 1];
}
theData[index] = anEntry;
size--;
for (int i = 0; i < theData.length; i++){
System.out.println(theData[i]);
}
}
使用队列
如果你有你自己的,有一个2整数的位置开始和结束的列表,而replaceArray只是复制元素的位置结束;然后结束=结束+1%容量;开始=开始+1%容量;无需移除-无需
使用队列
如果你有你自己的,有一个2整数的位置开始和结束的列表,而replaceArray只是复制元素的位置结束;然后结束=结束+1%容量;开始=开始+1%容量;无需移除-无需
我已经编写了这个实现。我假设数据的类型为int,但您可以将其更改为任何类型。我还删除了函数中的参数,因为我不使用它们 首先,我们将数组复制到一个临时变量中 其次,我们将数据中的第一个值保存到另一个临时变量中 然后,我们开始从数据的开头移动到第二个索引到最后一个索引 最后,我们将从数据中保存的第一个索引复制到first中,并将其分配给数组的最后一个索引 印刷品
我已经编写了这个实现。我假设数据的类型为int,但您可以将其更改为任何类型。我还删除了函数中的参数,因为我不使用它们 首先,我们将数组复制到一个临时变量中 其次,我们将数据中的第一个值保存到另一个临时变量中 然后,我们开始从数据的开头移动到第二个索引到最后一个索引 最后,我们将从数据中保存的第一个索引复制到first中,并将其分配给数组的最后一个索引 印刷品
我会使用这个简单的旋转数组的方法,我认为这个方法应该称为rotate而不是replaceAll,因为它实际上是将数组旋转一个位置 以下是旋转的方法: 完整的可测试示例 它将打印出:
1, 2, 3, 4, 5,
5, 1, 2, 3, 4,
我会使用这个简单的旋转数组的方法,我认为这个方法应该称为rotate而不是replaceAll,因为它实际上是将数组旋转一个位置 以下是旋转的方法: 完整的可测试示例 它将打印出:
1, 2, 3, 4, 5,
5, 1, 2, 3, 4,
是否需要使用arraylist?如果该方法总是将最后一个元素放在第一个位置,并将所有元素移到右侧,为什么需要任何参数?您可能会调用它rotate并让它将元素复制到新数组中,它会将旧数组中的最后一个元素复制到新数组的第一个位置,按顺序排列其余元素。是否需要使用arraylist?如果该方法总是将最后一个元素放在第一个位置,并将所有元素向右移动,为什么需要任何参数?您可能可以调用它rotate并让它将元素复制到新数组中,它会将旧数组中的最后一个元素复制到新数组的第一个位置,其余元素按顺序排列;将导致编译时错误。@ahoxha错过了该错误。谢谢你还是弄错了。问题不在于将第一个元素放在最后,而在于相反的方向——将最后一个元素放在第一位,并将其余元素向右移动。此外,temp[数据长度]=第一;将导致BoundsException的ArrayOutOfBoundsException。数据是一个数组,此语句int-temp=the数据;将导致编译时错误。@ahoxha错过了该错误。谢谢你还是弄错了。问题不在于将第一个元素放在最后,而在于相反的方向——将最后一个元素放在第一位,并将其余元素向右移动。此外,temp[数据长度]=第一;将导致BoundsException的排列。
public class MyArrayList extends ArrayList<Object>{
public void rotateRight(){
this.add(0, this.remove(this.size()-1));
}
public void rotateLeft(){
this.add(this.remove(0));
}
}
@SuppressWarnings("unchecked")
public void rotate() {
Object[] temp = new Object[theData.length];
//copy each element, except the first, from theData into temp by shifting one position off to the right
for (int i = temp.length - 1; i > 0; i--) {
temp[i] = theData[i - 1];
}
//move the last element into the first position
temp[0] = theData[theData.length - 1];
//update theData
theData = (T[]) temp;
}
public class MyArrayList<T> {
int INITIAL_CAPACITY = 10;
int capacity;
T[] theData;
@SuppressWarnings("unchecked")
public MyArrayList() {
capacity = INITIAL_CAPACITY;
theData = (T[]) new Object[capacity];
}
@SuppressWarnings("unchecked")
public MyArrayList(int capacity) {
this.capacity = capacity;
theData = (T[]) new Object[this.capacity];
}
@SuppressWarnings("unchecked")
public void rotate() {
Object[] temp = new Object[theData.length];
//copy each element, except the first, from theData into temp by shifting one position off to the right
// to the right
for (int i = temp.length - 1; i > 0; i--) {
temp[i] = theData[i - 1];
}
// move the last element into the first position
temp[0] = theData[theData.length - 1];
// update theData
theData = (T[]) temp;
}
/**
* For testing purposes only. It doesn't handle out of bounds values of
* index.
*/
private void insert(T t, int index) {
theData[index] = t;
}
public void print() {
for (T t : theData) {
System.out.print(t + ", ");
}
System.out.println();
}
@SafeVarargs
public static <E> MyArrayList<E> of(E... elements) {
MyArrayList<E> m = new MyArrayList<>(elements.length);
for (int i = 0; i < elements.length; i++) {
m.insert(elements[i], i);
}
return m;
}
}
public class TestMyArrayList {
public static void main(String[] args) {
MyArrayList<Integer> m = MyArrayList.of(1, 2, 3, 4, 5);
m.print();
m.rotate();
m.print();
}
}
1, 2, 3, 4, 5,
5, 1, 2, 3, 4,