I heard that Iterator is much faster than for loop, but after I did a test, I found that for loop runs faster. Here is the code:
import java.util.ArrayList;
import java.util.Iterator;
/**
*
* @author chenyi
*/
public class Review {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//ArrayList default: object
ArrayList <Integer> o = new ArrayList();
//ArrayList String
// ArrayList<String> s = new ArrayList();
//ArrayList Int
//ArrayList<Integer> in = new ArrayList();
//System.out.println(o.size());
for(int i = 0; i<90000; i++){
o.add(i);
// o.add(Integer.toString(i+1));
// System.out.println(i);
// l.add(i+1,Integer.toString(i));
}
long forStart = System.currentTimeMillis();
for(int j = 0; j<o.size(); j++){
o.get(j);
}
long forEnd = System.currentTimeMillis();
// long forend = System.currentTimeMillis();
Iterator<Integer> it = o.iterator();
long itStart = System.currentTimeMillis();
while (it.hasNext()){
it.next();
}
long itEnd = System.currentTimeMillis();
System.out.println("for loop ending time: "+(forEnd-forStart) + "\n"
+ "interator ending time: " + (itEnd - itStart));
// o.iterator();
}
}
And the output is
run:
for loop ending time: 7
interator ending time: 15
anybody knows why?
}
Wednesday, 5 December 2012
Monday, 3 December 2012
Shall I say byebye world this time?
This morning's TUT is my last period of CSC236. Thanks to everybody that helped me in this course. Especially to TAs and Heap, thank you so much for teaching and assisting us. Final exams are coming, hope I can survival...
Thank you again! I am really appreciated.
"Byebye, World"
Thank you again! I am really appreciated.
"Byebye, World"
Saturday, 1 December 2012
Table implementation problem solved
We solved this problem by create an interface called "TableCell" which represent a cell that contains the cell data in a Table. Then we created "TableCellInt" and "TableCellStr" that implement this interface and constructed their methods to meet our requirement. Also, in this way, it is easy for us to add on more type in the future. For example, if we want a another type like boolean, we can just create another class called "TypeCellBool", and constrcut whatever features and methods we need. I may post the revalent source code later, if I have time.
Subscribe to:
Posts (Atom)