Why we can’t do List mylist = ArrayList(); [duplicate]

Suppose we could. Then this program would have to be fine: ArrayList<Banana> bananas = new ArrayList<Banana>(); List<Fruit> fruit = bananas; fruit.add(new Apple()); Banana banana = bananas.get(0); That’s clearly not type safe – you’ve ended up with an apple in the collection of bananas. What you can do is: List<? extends Fruit> fruit = new ArrayList<Banana>(); … Read more

What would an AST (abstract syntax tree) for an object-oriented programming language look like?

AST is an abstraction of the CST (concrete syntax tree, or, parse tree). The concrete syntax tree is the tree resulting from the productions (in the grammar) used to parse the file. So your AST is basically derived from your grammar definition, but has for transformed Exp / | \ / | \ * Ident … Read more

Reading in from System.in – Java [duplicate]

You can use System.in to read from the standard input. It works just like entering it from a keyboard. The OS handles going from file to standard input. import java.util.Scanner; class MyProg { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(“Printing the file passed in:”); while(sc.hasNextLine()) System.out.println(sc.nextLine()); } }

Parse Web Site HTML with JAVA [duplicate]

There is a much easier way to do this. I suggest using JSoup. With JSoup you can do things like Document doc = Jsoup.connect(“http://en.wikipedia.org/”).get(); Elements newsHeadlines = doc.select(“#mp-itn b a”); Or if you want the body: Elements body = doc.select(“body”); Or if you want all links: Elements links = doc.select(“body a”); You no longer need … Read more