擬似コードで書くと、たとえば
int length = A.txtに含まれるデータの個数;
int[][] data = new int[3][length];
/*
data[0]がA.txtに対応
data[1]がB.txtに対応
data[2]がC.txtに対応
*/
int[] sum = new int[length];
public void coutUp(Object key) {
int val = 1;
try {
val = ((Integer)this.get(key)).intValue() + 1;
}
catch(Exception e) {
val = 1;
}
this.put(key, new Integer(val));
}
public String[] getKeyArray() {
return (String[])this.keySet().toArray(new String[0]);
}
public int getInt(Object key) {
try {
return ((Integer)this.get(key)).intValue();
}
catch(Exception e) {
return 0;
}
}
public String getString(Object key) {
return Integer.toString(getInt(key));
}
public static void main( String[] args ) {
try {
String dirpath = "files/";
File dir = new File(dirpath);
String[] files = dir.list();
ArrayList nodeList = new ArrayList();
for (int i=0; i<files.length; i++) {
BufferedReader in = new BufferedReader(new FileReader(dirpath+files[i]));
ArrayList valList = new ArrayList();
String str;
while( (str = in.readLine()) != null ) {
valList.add(str);
}
nodeList.add(valList);
}
int maxLength = 0;
for (int i=0; i<nodeList.size(); i++) {
int valSize = ((ArrayList)nodeList.get(i)).size();
if ( valSize > maxLength ) maxLength = valSize;
}
Counter counter = new Counter();
for (int row=0; row<maxLength; row++) {
StringBuffer sb = new StringBuffer();
for (int clm=0; clm<nodeList.size(); clm++) {
ArrayList valList = (ArrayList)nodeList.get(clm);
String val;
try {
val = (String)valList.get(row);
}
catch(Exception iob) {
val = "";
}
sb.append(val);
}
counter.coutUp(sb.toString());
}
>26: ray さん
↓こんな感じで走らせても差は出ませんでした。
コンパイル時の変数の割当で変わるんでしょうか?評価したコードがあれば見せてください。
public class ArraySample {
static final int TRY1=100;
static final int TRY2=50;
static final int LARGE = 20000;
static final int SMALL = 2;
public static void sample1(){
int[][] data1 = new int[SMALL][LARGE];
for (int i = 0; i < LARGE; i++){
for (int j = 0; j < SMALL; j++){
data1[j][i] = i+j;
}
}
}
public static void sample2(){
int[][] data2 = new int[LARGE][SMALL];
for (int i = 0; i < LARGE; i++){
for (int j = 0; j < SMALL; j++){
data2[i][j] = i+j;
}
}
}
public static void main(String[] args){
long t1,t2,t3;
long sum1 = 0,sum2 = 0;
for(int n = 0; n < TRY2; n++){
t1 = System.currentTimeMillis();
for (int m = 0; m < TRY1; m++){
sample1();
}
t2 = System.currentTimeMillis();
for (int m = 0; m < TRY1; m++){
sample1();
}
t3 = System.currentTimeMillis();
sum1 += t2 - t1;
sum2 += t3 - t2;
}
System.out.println(sum1+"/"+sum2);
}
}
//equalsの引数の型をObject型に
public boolean equals(Object o) {
if (o instanceof ABC){
ABC abc = (ABC)o;
return a == abc.a && b == abc.b && c == abc.c;
}
return false;
}
//hashCodeはこんな感じで書いてます
//return 0だと、HashMapがしんどいです。
public int hashCode() {
return ((1 + a) * 31 + b) * 31 + c;
}
public Model next() {
int[] values = new int[valueCount.length];
int n = number;
for (int i = values.length - 1; i >= 0; i--) {
values[i] = n % valueCount[i];
n /= valueCount[i];
}
number++;
return new Model(values);
}