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.
Saturday, 24 November 2012
Java Code Review Tool
One of the most painful but important steps of a java project, is the code review. checking and reviewing thousands of lines of some body else's code for small things like braces, name style, documentation and overall design can really knock you out and make you feel like there is a heavy rock right on the top of your heart.
fortunately, there are tools to reduce your pain and automate this precudrue as mush as possible. There are 2 tools that I am familiar with, each of them has their own features, and by using them together, really can make your life much easier.
first one I recommand is CheckStyle. But its checking rule is too strick, so you better set up your own one. It checks for the followings in your code:
fortunately, there are tools to reduce your pain and automate this precudrue as mush as possible. There are 2 tools that I am familiar with, each of them has their own features, and by using them together, really can make your life much easier.
first one I recommand is CheckStyle. But its checking rule is too strick, so you better set up your own one. It checks for the followings in your code:
- Java comments and docs
- naming conventions
- header
- imports
- size violations
- white space
- blocks
- modifiers
- and etc.
- empty catch block
- unused if statement and local variables
- duplicate codes
- classes that could be singletons
- inproperiate variable and method names
- and etc.
Saturday, 17 November 2012
Checked VS Unchecked exception
Checked exceptions
- any subclass extend Exception, including Exception itself, excluding RuntimeException
- must be catch or declare in a throws clause.
- represent invalid conditions outside the direct control of the program, Such as wrong format of user input, (which we were dealing a lot during the implementation of A2 in CSC207), database problems, etc
Unchecked exceptions
- extend RuntimeException only, but notice, RuntimeException itself is a subclass of Exception
- the programmer may not know that this kind of exceptions could be thrown, thus, they are not forced to be declared.
- represent bugs in the program, which cannot be reasonably recovered from at run time. Such as NullPointerException, IndexOutOfBoundsException etc.
- any subclass extend Exception, including Exception itself, excluding RuntimeException
- must be catch or declare in a throws clause.
- represent invalid conditions outside the direct control of the program, Such as wrong format of user input, (which we were dealing a lot during the implementation of A2 in CSC207), database problems, etc
Unchecked exceptions
- extend RuntimeException only, but notice, RuntimeException itself is a subclass of Exception
- the programmer may not know that this kind of exceptions could be thrown, thus, they are not forced to be declared.
- represent bugs in the program, which cannot be reasonably recovered from at run time. Such as NullPointerException, IndexOutOfBoundsException etc.
Saturday, 10 November 2012
When to declare "throws Exception"
In Java, when defining a method, we also need to declare in the method head what exception may be throw. That's what I was believing, but, in my instructor's slides, some methods she did this way, some methods she just throw exception right the way. such as following:
public void methodA() throws AnException {
//do something throw new AnException();
}
//do something throw new AnException();
}
public void methodA() {
//do the same thing throw new AnException();
}
//do the same thing throw new AnException();
}
And I am kind of confusing in which situation we need to declare,
is there a specific requirement or restriction? or we can do it either way depends
on our mood?
after some google, I find that there are two different major type of exceptions,
one is checked, which does not extend RuntimeException, and those exceptions must
be declared. In the other hand, unchecked exception is the exception that extends
RuntimeException, and then either way is allowed(declare/not declare). But now I have other doubts like, in which circumstances our exception should extends RuntimeException?
what is the difference between those two? and what is the advantage on each?
Sunday, 4 November 2012
leave it behind....review first
Since we are having a Midterm tomorrow, I decided to leave that implementation after I finished our second midterm....XD
Table implementation
I created a Table class based on 2d ArrayList. The constructor of this class is just simplely take in a 2d String ArrayList. Now the requirements of the assignmnet changed a little bit, so that this Table class would be able to handle both String and Int type of ArrayList. I could build up two different constructors, so that one take in String ArrayList, and another one take in Int ArrayList. But the problem is, the instance of this class was initialized as a String ArrayList, thus a Int ArrayList cannot be assigned to it. I tried to initial another instance with Int ArrayList, but NetBean warned me that there is a name clash between those two constructors, they have the same erasure. Anybody knows what does "erasure" means in Java? And I think my method of implementing this task is wrong, is there another way? Take a rest, come back later...
Monday, 22 October 2012
One More Thing About Midterm1!!
After I get back my midterm 1 paper, I found that I stated the WRONG P(n) for each problem, and TA deducte one mark for each, that is, 3 marks, 20% in total...SAD FACE, weighed so much. However, then I looked through slides and class notes very carefully, to check out what I have done wrong. Eventually I realized that when we state the P(n) for a problem, we should not include "for every n belongs to natural numbers"(or something like that), because that is what we going to prove, and if we already stated that out, nothing left out for us. In other hand, isn't it strange to say in the induction step: assume for every n belong to natural number, P(n) is true? that is, if we already stated then why we assume again?
A good thing is from this midterm, I learned that I should be more attentive when study those class slides.
A good thing is from this midterm, I learned that I should be more attentive when study those class slides.
Monday, 15 October 2012
About First Term Test (sad FACE)
Well, it seemed like first term of this course was pretty stright foward, not so hard, but also, not so easy. Unfortunately, I just noticed one day ahead of the term test that we are going to have the term next day! What a big mistake! And which caused me did not have enough time to recover all induction concepts. Perhaps this would not happend again, no, must not!
Also, I realized few small mistakes I have done in question No.2 during the midterm.
Should be more specific in the induction step.
Following is the complete one, I hope. :)
P(n): at most, total leaves of a n height tree is 2^(n-1).
induction step:
assume n belongs to all natural number, 1 <= i < n, P(i) is True.
case i = 1, then this tree has only one node, this node is also its leave => 2^(1-1) = 1, 1<=1, thus, P(i) holds
case i > 1, then at most, this tree has two children, TL and TR, and TL, TR also inherited T's properties, but their's heights are less than T's by 1, thus 1<= TR's height, TL's height < n, P(TLn), P(TRn) holds. Thus,
Tleaves = TLleavs + TRleaves
<= 2^(TLn-1) + 2^(TRn-1) # by IH
<= 2^(n-1-1) + 2^(n-1-1)
<= 2^(n-2) + 2^(n-2)
<= 2* 2^(n-2)
<= 2^(n-2+1)
<= 2^(n-1)
then, P(i) holds.
Since in both cases P(i) holds, then for all natural numbers of n, n>= 1, P(n) is true.
"mistakes are opportunities to learn, adjust, grow, and improve." I believe I would not make such mistakes again.
Also, I realized few small mistakes I have done in question No.2 during the midterm.
Should be more specific in the induction step.
Following is the complete one, I hope. :)
P(n): at most, total leaves of a n height tree is 2^(n-1).
induction step:
assume n belongs to all natural number, 1 <= i < n, P(i) is True.
case i = 1, then this tree has only one node, this node is also its leave => 2^(1-1) = 1, 1<=1, thus, P(i) holds
case i > 1, then at most, this tree has two children, TL and TR, and TL, TR also inherited T's properties, but their's heights are less than T's by 1, thus 1<= TR's height, TL's height < n, P(TLn), P(TRn) holds. Thus,
Tleaves = TLleavs + TRleaves
<= 2^(TLn-1) + 2^(TRn-1) # by IH
<= 2^(n-1-1) + 2^(n-1-1)
<= 2^(n-2) + 2^(n-2)
<= 2* 2^(n-2)
<= 2^(n-2+1)
<= 2^(n-1)
then, P(i) holds.
Since in both cases P(i) holds, then for all natural numbers of n, n>= 1, P(n) is true.
"mistakes are opportunities to learn, adjust, grow, and improve." I believe I would not make such mistakes again.
Subscribe to:
Posts (Atom)