现在上课初学JAVA..作业遇到了问题..实在实在想不通了~ 就厚着脸皮向大家请教...请帮帮忙.. * Runs an experiment involving inserting into the front of a list.
*
* Given the list to insert into and a collection of Strings to insert,
* this method will loop over the elements of the parameter toInsert,
* and will insert them at the front of list given by the parameter theList.
*
* The performance of this task will be measured by keeping track of how long it takes.
* This will be done by using System.currentTimeMillis() method to determine the time.这是老师的要求
把一个collection 里边的东西加到一个list 的最前端..然后计时这个需要多少时间..
这是我写的code:public class ListExperiments {public long insertAtFront(List<String> theList, Collection<String> toInsert) { long start = System.currentTimeMillis(); String[] temp = (String[]) toInsert.toArray();
for(int i = 0; i<theList.size();i++){
theList.add(i, temp[i]);
}
// int i = 0;
// for(String s:toInsert){
// theList.add(i,s);
// i++;
// } long end = System.currentTimeMillis();
long time = end - start;
return time;
}尝试了两种方法,用junit test貌似都过不了,没说错在哪儿.就是有error..
请大家教教我~! 谢谢!
*
* Given the list to insert into and a collection of Strings to insert,
* this method will loop over the elements of the parameter toInsert,
* and will insert them at the front of list given by the parameter theList.
*
* The performance of this task will be measured by keeping track of how long it takes.
* This will be done by using System.currentTimeMillis() method to determine the time.这是老师的要求
把一个collection 里边的东西加到一个list 的最前端..然后计时这个需要多少时间..
这是我写的code:public class ListExperiments {public long insertAtFront(List<String> theList, Collection<String> toInsert) { long start = System.currentTimeMillis(); String[] temp = (String[]) toInsert.toArray();
for(int i = 0; i<theList.size();i++){
theList.add(i, temp[i]);
}
// int i = 0;
// for(String s:toInsert){
// theList.add(i,s);
// i++;
// } long end = System.currentTimeMillis();
long time = end - start;
return time;
}尝试了两种方法,用junit test貌似都过不了,没说错在哪儿.就是有error..
请大家教教我~! 谢谢!
import java.util.Collection;
import java.util.Iterator;
import java.util.List;public class Test {
public static void main(String[] args) {
List<String> theList = new ArrayList<String>();
theList.add("x");
theList.add("y");
Collection<String> toInsert = new ArrayList<String>();
toInsert.add("One");
toInsert.add("Two");
toInsert.add("Three");
long time = insertAtFront(theList, toInsert);
System.out.println(theList);
System.out.println(time);
} public static long insertAtFront(List<String> theList, Collection<String> toInsert) {
long start = System.currentTimeMillis(); Iterator<String> iter = toInsert.iterator();
while (iter.hasNext()) {
theList.add(0, iter.next());
} long end = System.currentTimeMillis();
long time = end - start;
return time;
}
}[/code]
还得再继续学习学习!