如何在Java中使用迭代器将元素添加到数组的中间?
我在玩数组和迭代器。我不想使用集合。只是简单的迭代器和我自己定义的方法。 我已经在数组中设置了一组学生,数组的大小等于学生的数量,它们的时间顺序是由学生索引ID号-123456、162475等构成的。。所以我想创建一个比之前的一个一个元素大的新数组,并添加新的student,但要保存时间顺序。我有一个方法可以创建更大的数组并覆盖对旧数组的引用,但我不知道如何使用迭代器在特定位置添加元素。使用for和它的array[i+1]=array[i]会很容易,但我不知道如何使用迭代器 这是我代码的一部分:如何在Java中使用迭代器将元素添加到数组的中间?,java,arrays,iterator,add,Java,Arrays,Iterator,Add,我在玩数组和迭代器。我不想使用集合。只是简单的迭代器和我自己定义的方法。 我已经在数组中设置了一组学生,数组的大小等于学生的数量,它们的时间顺序是由学生索引ID号-123456、162475等构成的。。所以我想创建一个比之前的一个一个元素大的新数组,并添加新的student,但要保存时间顺序。我有一个方法可以创建更大的数组并覆盖对旧数组的引用,但我不知道如何使用迭代器在特定位置添加元素。使用for和它的array[i+1]=array[i]会很容易,但我不知道如何使用迭代器 这是我代码的一部分:
public class GrupaStud {
public static void main(String[] args) {
Student [] s = new Student[5];
s[0]=new Student("Katarzyna", "Gryzipiórko", 123456, 5);
s[1]=new Student("Bartosz", "Polański", 162475, 4);
s[2]=new Student("Heniek", "Zając", 175642, 3);
s[3]=new Student("Konstanty", "Mołotow", 432156, 2);
s[4]=new Student("Bogdan", "Cichowlaz", 666555, 2.5);
ArrayIterator itab = new ArrayIterator(s);
s = biggerArray(s);
itab = new ArrayIterator(s);
Student nowy =new Student("Małgorzata", "Kopytko", 165642, 4);
for (itab.first(); !itab.isDone(); itab.next()) {
Student st = (Student) itab.current();
//in my mind that if need to check if index number of current element is bigger than
//int index above (165642) , but exactly here I don't know, how to add Student nowy
//to the array with moving rest of elements
if (st.nrIndex >nowy.nrIndex)
}
}
public static Student[] biggerArray(Student[] s)
{
Student[] newArray = new Student[6];
for (int i=0; i<s.length; i++)
newArray[i] = s[i];
return newArray;
}
}
你应该考虑使用一个更好的处理插入的ARARYLIST。否则,您将需要将数组中的所有元素移到另一个位置。ArrayList也会自动为您展开,所以当大小不确定时,它们是很好的
ArrayList<Student> students = new ArrayList<Student>();
students.add(student1);
students.add(student2);
students.add(student3);
int insertionIndex = 1;
students.add(insertionIndex, student4);
在使用迭代器遍历时,我会将现有值添加到新数组中,而不是初始化biggerArray中的所有值。没有一种简单的方法可以像这样插入到数组的中间,这就是为什么您通常会使用ArrayList来进行这类操作 比如:
itab = new ArrayIterator(s);
Student[] newArray = new Student[6];
int newIndex = 0;
Student nowy =new Student("Małgorzata", "Kopytko", 165642, 4);
for (itab.first(); !itab.isDone(); itab.next()) {
Student st = (Student) itab.current();
if (st.nrIndex > nowy.nrIndex) { //Not sure about the meaning of this condition, make sure you only add the new student once!
newArray[newIndex] = nowy;
newIndex++;
}
newArray[newIndex] = st;
newIndex++;
}
看看ArrayList如何实现这一点也很有趣。它使用,您可以使用类似的方法来做您正在做的事情,并且:
s = biggerArray(s);
itab = new ArrayIterator(s);
int index = 0;
Student nowy =new Student("Małgorzata", "Kopytko", 165642, 4);
for (itab.first(); !itab.isDone(); itab.next()) {
Student st = (Student) itab.current();
if (st.nrIndex >nowy.nrIndex) {
System.arraycopy(s, index, s, index + 1, 5 - index);
s[index] = nowy;
break;
}
index++;
}
这不符合规定。它接受原始数组和插入到数组中的数组,数组的长度必须正好大一个,至少大一个。由于它被泛化为与所有非基元类型一起使用,因此它无法创建数组本身 职能:
public static final <O> O[] getNewArrayWithInserted(int insertIdx, O toInsert, O[] orig_arr, O[] arr_toInsInto) {
int idx = -1;
try {
for(O o : orig_arr) {
idx++; //First iteration: was -1, now 0
if(idx < insertIdx) {
arr_toInsInto[idx] = o;
continue;
}
if(idx == insertIdx) {
arr_toInsInto[idx++] = toInsert;
}
arr_toInsInto[idx] = o;
}
} catch(ArrayIndexOutOfBoundsException abx) {
throw new ArrayIndexOutOfBoundsException("idx=" + idx + ", insertIdx=" + insertIdx + ", orig_arr.length=" + orig_arr.length + ", arr_toInsInto.length=" + arr_toInsInto.length + ", original error:" + abx);
}
return arr_toInsInto;
}
是的,我知道所有这些,我知道在这种情况下如何使用ArrayList,但我需要在简单数组中实现add方法,移动其余元素并使用迭代器。我不知道怎么做:forint I=0;i@RIPI对不起,我刚读到你们不想使用集合。嗯……你们第二个建议中的大小是什么意思?是的,忘了那个是硬编码的。应该是5,size index正在尝试获取需要复制的项目数,因为它们位于我们插入的项目之后。是的:您的代码还有一个错误,但我按照文档修复了它。它应该是System.arraycopy…,但我的代码正在运行,并且添加的人是正确的:谢谢:现在我需要仔细考虑,它到底是如何工作的,因为它很智能:
import java.util.Arrays;
/**
<P>{@code java ManualArrayInsertWItrIntoNewArray}</P>
**/
public class ManualArrayInsertWItrIntoNewArray {
public static final void main(String[] ignored) {
//You don't have to prefix it with ManualArrayInsertWItr in this static main,
//but it's normally called this way.
Integer[] intArr = new Integer[] {1, 2, 3, 5, 6, 7, 8, 9};
Integer[] intArr2 = ManualArrayInsertWItrntoNewArray.<Integer>getNewArrayWithInserted(3, 4,
intArr, new Integer[intArr.length + 1]);
System.out.println("Original: " + Arrays.toString(intArr));
System.out.println("New with insert: " + Arrays.toString(intArr2));
}
public static final <O> O[] getNewArrayWithInserted(int insertIdx, O toInsert, O[] orig_arr, O[] arr_toInsInto) {
int idx = -1;
try {
for(O o : orig_arr) {
idx++; //First iteration: was -1, now 0
if(idx < insertIdx) {
arr_toInsInto[idx] = o;
continue;
}
if(idx == insertIdx) {
arr_toInsInto[idx++] = toInsert;
}
arr_toInsInto[idx] = o;
}
} catch(ArrayIndexOutOfBoundsException abx) {
throw new ArrayIndexOutOfBoundsException("idx=" + idx + ", insertIdx=" + insertIdx + ", orig_arr.length=" + orig_arr.length + ", arr_toInsInto.length=" + arr_toInsInto.length + ", original error:" + abx);
}
return arr_toInsInto;
}
}
[C:\java_code\]java ManualArrayInsertWItrIntoNewArray
Original: [1, 2, 3, 5, 6, 7, 8, 9]
New with insert: [1, 2, 3, 4, 5, 6, 7, 8, 9]