Created
September 16, 2014 10:31
-
-
Save kaneshin/cc8ffc730608474c448e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Foo | |
*/ | |
public class Foo { | |
private static final int MIN_HEIGHT = 130; | |
private static final int MAX_HEIGHT = 200; | |
private static final int[] EXPECTED = { | |
130, | |
135, | |
140, | |
145, | |
150, | |
155, | |
160, | |
165, | |
170, | |
175, | |
180, | |
185, | |
190, | |
195, | |
200, | |
}; | |
public static void main(String[] args) { | |
int[] range1 = range1(); | |
for (int i = 0, n = range1.length; i < n; ++i) { | |
if (range1[i] == EXPECTED[i]) { | |
System.out.println("OK: " + range1[i]); | |
} else { | |
System.out.println("NG: Result = " + range1[i] + ", Expected = " + EXPECTED[i]); | |
} | |
} | |
System.out.println("====================="); | |
int[] range2 = range2(); | |
for (int i = 0, n = range2.length; i < n; ++i) { | |
if (range2[i] == EXPECTED[i]) { | |
System.out.println("OK: " + range2[i]); | |
} else { | |
System.out.println("NG: Result = " + range2[i] + ", Expected = " + EXPECTED[i]); | |
} | |
} | |
} | |
public static int[] range1() { | |
int total = ( (MAX_HEIGHT - MIN_HEIGHT) / 5 ) + 1; | |
int[] range = new int[total]; | |
int num = 0; | |
for (int i = MIN_HEIGHT; i <= MAX_HEIGHT; i++) { | |
if (i % 5 == 0) { | |
range[num] = i; | |
num++; | |
} | |
} | |
return range; | |
} | |
private static final int INTERVAL = 5; | |
public static int[] range2() { | |
int total = ((MAX_HEIGHT - MIN_HEIGHT) / INTERVAL) + 1; | |
int[] range = new int[total]; | |
for (int i = 0; i < total; i++) { | |
range[i] = MIN_HEIGHT + i * INTERVAL; | |
} | |
return range; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
「5飛ばしで格納する」という発想を「MIN_HEIGHTに i を5倍したものを格納する」
これなら処理に負荷の掛かる剰余演算使わないのと、条件を省ける。一時変数のnumも要らない。(GCされるからメモリは気にしない)