【发布时间】:2014-03-03 18:02:53
【问题描述】:
我在玩数组和迭代器。我不想使用集合。只是我自己定义的简单迭代器和方法。 我在数组中有一组学生(数组的大小等于学生的数量),它们按时间顺序排列(按学生索引(ID)编号 - 构造函数中的 123456、162475 等)。所以我想创建一个比前一个(一个元素)大的新数组并添加新学生,但要保存时间顺序。我有创建更大数组并覆盖对旧数组的引用的方法,但我不知道如何使用迭代器在特定位置添加元素。使用 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;
}
}
【问题讨论】: