This is a nice site for code stealing
Read more at www.krugle.com/
Our technical scratch pad
This is a nice site for code stealing
Read more at www.krugle.com/
public void Show(List l) {
for (int i = 0; i < l.size(); i++) {
System.out.println (l.get(i));
}
}
This code seems right; however it could be very inefficient.
The in-efficiency can be attributed to the sequential manner - the
above progresses,resulting in an algorithm of order O(n^2)
whereas an algorithm O(n) is easily obtained.
O(n) can be got with the use of iterators (similar to c++)
public static void ShowFast(List l) {
for (Iterator it = l.iterator(); it.hasNext();) {
System.out.println (it.next());
}
}
When you ask for an iterator of the collection using the method
iterator(), a reference to the beginning of the list is retrieved.
Then, the hasNext() method returns whether there are still more
elements to come, and it.next() does two things: it advances the
reference to the next element in the collection and retrieves the
current element. This gives the LinkedList the opportunity to iterate
through the list in O(n). For the moment, all that can be done in
earlier versions of Java. Even if the above code works, it's not nice
and quite error prone. For example, if in the hurry you call again to
it.next() in the for block in order to retrieve again the element,
you'll get half the list in one place and half in another. Of course
that this can be solved storing the value of it.next() in a local
variable, but Java 1.5 brings a nicer and safer way to do the same: the
for-each loop.
public static void ShowFastAndNice(List l) {
for (Object o : l) {
System.out.println (o);
}
}
You can read the for sentence as "for each Object o in l"
With New features--- have to come their share of new problems.
Not everything is rosy here .....
The following bombs
Object o;
for (o : l) {
System.out.println (o);
}
Sun documents this as "these kinds of practices are not very clear and robust,
so the compiler is making you to write better code, even if it might require
an additional flag."(not verbatim but on similar lines).
A stock ticker is a piece of code which keeps you updated with the latest(as and when) stock quotations of various companies at the stock exchange. Technically to build this one needs the following tools --
1. XML
Need to know just this concepts elements.
2. RSS (Really Simple Syndicate) -- it is really simple.
As I understand this, there is a link like "http://www.xanadb.com/ticker/YHOO" which provides RSS feed for stocks(YHOO- for yahoo, MSFT-microsoft).
Now observe the xml format carefully. All rss feeds essentially have these 3 elements
title,descrption,link.
An RSS reader is one which makes sense of this rss feed.
So to build the stock ticker here is the design
a) Get the contents of "http://www.xanadb.com/ticker/YHOO" in a variable.
b) Just find the “title” tag. Trash the rest.
c) Find the number and thats it.
3. cURL
Now to achieve step (a) mentioned above we have to use a tool which gets us the contents of the page in a variable or a file.
cURL is shipped freely with most of the Linux installations.
Catch hold of one and execute this
curl "http://www.xanadb.com/ticker/YHOO"
Now go through the manual in case you are using proxies, SSL protected site, etc etc etc.
Now the power is all yours. Just think all the stuff possible with this tool and have i told you this. Its a MIT product.
4. sed,grep and other scripting techniques
Here after its child's play and the scripting skills.
curl "http://www.xanadb.com/ticker/YHOO" | grep title | sed s/title//g
I havent tried the above on a machine , but hope you got an idea as to how the attempt is being made to get the stock quote.
No matter how hard web designers may try to force you to surf their sites interactively, there will always be free tools to help automate the process, thus enriching our overall Net experience.
A deliberate attempt has been made only to answer the "How" part of the tools and not the "What" part of them, as they are available in the links suggested.
Next in Line
PODCAST -- what the hell is this