Difference between declaring variables before or in loop?

I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (quite pointless) example in Java: a) declaration before loop: double intermediateResult; for(int i=0; i < 1000; i++){ intermediateResult = i; System.out.println(intermediateResult); } b) declaration (repeatedly) inside loop: for(int i=0; … Read more

Declare and initialize a Dictionary in Typescript

Given the following code interface IPerson { firstName: string; lastName: string; } var persons: { [id: string]: IPerson; } = { “p1”: { firstName: “F1”, lastName: “L1” }, “p2”: { firstName: “F2″ } }; Why isn’t the initialization rejected? After all, the second object does not have the “lastName” property. 6 Answers 6

How to initialize an array in Java?

I am initializing an array like this: public class Array { int data[] = new int[10]; /** Creates a new instance of Array */ public Array() { data[10] = {10,20,30,40,50,60,71,80,90,91}; } } NetBeans points to an error at this line: data[10] = {10,20,30,40,50,60,71,80,90,91}; How can I solve the problem? 10 Answers 10 data[10] = {10,20,30,40,50,60,71,80,90,91}; … Read more

What is the easiest way to initialize a std::vector with hardcoded elements?

I can create an array and initialize it like this: int a[] = {10, 20, 30}; How do I create a std::vector and initialize it similarly elegant? The best way I know is: std::vector<int> ints; ints.push_back(10); ints.push_back(20); ints.push_back(30); Is there a better way? 29 s 29 If your compiler supports C++11, you can simply do: … Read more