0

My tide prediction application uses 8 double arrays for tide height calculations. Literally every tide station in the United States requires these to have 37 elements, EXCEPT Anchorage, Alaska which requires 124 elements.

Here is a declaration example

final int NUM_C = 37;   //all stations except anchorage use 37
//final int NUM_C = 124;  //anchorage uses 124
double a[] = new double[NUM_C + 1];

Can I efficiently specify the array size at the start up of the app? I can determine which is needed. I don't want to burden the application with inefficiency for 99% + of the users to handle this one case. The difference is only about 3K bytes.

user1644002
  • 3,211
  • 3
  • 19
  • 21
  • Yes. You just have to provide the user of your application to select the state and then you pick the constant which is required to initialize the double array. I think I have not understood your question. Because the problem I did understand, is quite simple to solve. – Reaz Murshed Apr 22 '18 at 14:48
  • Reaz I don't want to make the array access slower by using a variable to size it rather than a constant. I cannot tell if it will. – user1644002 Apr 22 '18 at 14:53
  • I didn't explain my question well.My app loads state info as instructed by user. But the 8 arrays remain as declared at the top of the main class. Rarely will the 124 entries be required. If I used a true variable in the new statements, what happens if I change the array size in the new statements from 37 to 124 after first run. Or 125 back to 37. thanks. – user1644002 Apr 23 '18 at 13:24

1 Answers1

0

Why don't you instantiate the variable in the constructor? It gives you more freedom to do programatic manipulation.

public class Station {

    double a[];

    public Station(String location) {
        if(location.equals("Anchorage")) {
            a = new double[124];
        } else {
            a = new double[37];
        }
    }
}

As I understand the instantiation of the object fields in the constructor is the normal case, while the instantiation with the declaration is just an additional feature of Java.

As for the speed it does not make a difference, if you specify the size by a literal value, a constant or a variable. A more interesting question is, if you should use ArrayList instead of an array. See here.

public class Station {

    ArrayList<Double> a;

    public Station(String location) {
        if(location.equals("Anchorage")) {
            a = new ArrayList<>(124);
        } else {
            a = new ArrayList<>(37);
        }
    }
}

My choice would be ArrayList as it is more flexible. Eight times 124 is not a very large number anyway. No reason to worry about performance for this.

Blcknx
  • 1,921
  • 1
  • 12
  • 38
  • Blcknx, Well if I use a variable in the new statement instead of the constant NUM_C, in my main body, and if I change it based on new station selections, how is the memory use managed. If each access has to be tested by the limit, it is a disaster. It seems to be an ArrayList would be inherently slower. I need access these arrays a very large number of times. I am thinking should just accept the extra 2784 bytes in the arrays of 124 and keep it simple. – user1644002 Apr 28 '18 at 15:57
  • In the new statement you set the size of memory the array is using. If this size comes from a constant or whatever has none effect on the size or the speed. Once the array is created, the value is not used any more. – Blcknx Apr 28 '18 at 18:28
  • It depends, if you reuse one array for all stations or if you create a new array per station. In the first case you should set the size to the largest value. In the second case, you should set the size to an appropriate value. – Blcknx Apr 28 '18 at 18:32
  • I have some background of Bioinformatics. I tell you 2784 bytes is nothing. Today the memory is counted in GB. 2784 bytes is less than 0.000003 GB. You would get into trouble, if you create millions of stations at the same time. – Blcknx Apr 28 '18 at 18:40
  • If you access the array a very many time I also would prefer `array` over `ArrayList`. From my experience it may be more important to avoid chains of method calls per access. Loops are usually faster than method recursion. While I like to separate code into small methods, I avoid this for methods, that are repeated thousands of times, because each method call has a price. In doubt I would run a testing setup to measure if there is a real difference. The compiler does a lot of optimization. – Blcknx Apr 28 '18 at 19:38
  • Blcknx, Given what you told me, just setting the larger array size is the best approach. The arrays are declared before the location is known. The arrays stay till the app is killed. Only one station is calculated at a time, but after results are posted a station change is allowed. I have a very tight loop that flies through the arrays to calculate a height at a given moment, but many moments have to be calculated for the 24 hour report. I don't want to introduce new complicated code to save 2784 bytes, so that is my choice. I appreciate your consultation. Larry – user1644002 Apr 29 '18 at 21:10
  • Think your choice is fine. – Blcknx Apr 30 '18 at 08:38