[PATCH] Reduce usage of careless 'for (int i = 0; i < x.size(); i++)' pattern
Wald Commits
scm-commit at wald.intevation.org
Sat Oct 5 17:52:21 CEST 2013
# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1380988336 -7200
# Node ID fe32a7f9655e0a00483de1158c07a7f466f81402
# Parent 90a3bcd8060aebb5a7b2b570183e9e9ca3cb59c4
Reduce usage of careless 'for (int i = 0; i < x.size(); i++)' pattern.
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation4.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation4.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation4.java Sat Oct 05 17:52:16 2013 +0200
@@ -339,19 +339,21 @@
infoldings.add(infolding);
}
- for (int i = 0; i < infoldings.size(); i++) {
- String name = infoldings.get(i).getName();
+ for (int i = 0, I = infoldings.size(); i < I; i++) {
+ ConstantWQKms infolding = infoldings.get(i);
+ String name = infolding.getName();
// TODO: i18n
if (i == 0) {
- infoldings.get(i).setName("untere Umh\u00fcllende " + name);
+ infolding.setName("untere Umh\u00fcllende " + name);
}
- else if (i == infoldings.size() - 1) {
- infoldings.get(i).setName("obere Umh\u00fcllende " + name);
+ else if (i == I-1) {
+ infolding.setName("obere Umh\u00fcllende " + name);
}
else {
- infoldings.get(i).setName("geschnitten " + name);
+ infolding.setName("geschnitten " + name);
}
}
+
return infoldings.toArray(new ConstantWQKms[infoldings.size()]);
}
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQ.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQ.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WQ.java Sat Oct 05 17:52:16 2013 +0200
@@ -126,8 +126,10 @@
// When we convert and have a datum we have a calculated
// result at a gauge so we must subtract the datum.
double subtractDatum = datum == null ? 0 : datum.doubleValue();
- for (int i=0; i < wq.size(); i++) {
- ret.add((wq.get(i)[0] - subtractDatum)* 100, wq.get(i)[1]);
+ double [] data = new double[8];
+ for (int i = 0, WQ = wq.size(); i < WQ; i++) {
+ wq.get(i, data);
+ ret.add((data[0] - subtractDatum)*100d, data[1]);
}
log.debug("Converted W values to centimeter and substracted: " + subtractDatum);
return ret;
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/map/HWSFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/map/HWSFactory.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/map/HWSFactory.java Sat Oct 05 17:52:16 2013 +0200
@@ -168,8 +168,7 @@
sqlQuery.setString("river", river);
List<Object []> resultsPoints = sqlQuery.list();
- for (int i = 0; i < resultsPoints.size(); i++) {
- Object[] row = resultsPoints.get(i);
+ for (Object [] row: resultsPoints) {
container.addHws(
new HWS(
(String) row[0],
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java Sat Oct 05 17:52:16 2013 +0200
@@ -152,8 +152,7 @@
sqlQuery.setInteger("height_id", height_id);
List<Object []> results = sqlQuery.list();
- for (int i = 0; i < results.size(); i++) {
- Object[] row = results.get(i);
+ for (Object [] row: results) {
log.debug("got station: " + (Double)row[1]);
Double row0 = row[0] != null ? (Double)row[0] : Double.NaN;
Double row1 = row[1] != null ? (Double)row[1] : Double.NaN;
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/MorphologicWidthFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/MorphologicWidthFactory.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/MorphologicWidthFactory.java Sat Oct 05 17:52:16 2013 +0200
@@ -82,8 +82,7 @@
List<Object []> results = sqlQuery.list();
MorphologicWidth widths = new MorphologicWidth();
- for (int i = 0; i < results.size(); i++) {
- Object[] row = results.get(i);
+ for (Object [] row: results) {
log.debug("got station: " + (Double)row[0]);
widths.add(
(Double) row[0],
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java Sat Oct 05 17:52:16 2013 +0200
@@ -79,7 +79,7 @@
return getDensityAtKm(densities.get(years.get(0)), km);
}
else if (this.years.size() > 1) {
- for (int i = 0; i < years.size() -1; i++) {
+ for (int i = 0, I = years.size()-1; i < I; i++) {
int y1 = years.get(i);
int y2 = years.get(i + 1);
if (year >= y1 && year < y2) {
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadCalculation.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadCalculation.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadCalculation.java Sat Oct 05 17:52:16 2013 +0200
@@ -237,7 +237,7 @@
}
}
- for (int j = 0; j < kms.size(); j++) {
+ for (int j = 0, J = kms.size(); j < J; j++) {
calculateEpochKm(epochLoads, resLoad, kms.get(j));
}
resLoad.setDescription("");
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFacet.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFacet.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFacet.java Sat Oct 05 17:52:16 2013 +0200
@@ -153,7 +153,7 @@
int i = 0;
for (Map.Entry<Double, Double> kmLoad: sortedKmLoad.entrySet()) {
boolean matchFound = false;
- for (int k = 0; k < stations.size(); k++) {
+ for (int k = 0, S = stations.size(); k < S; k++) {
MeasurementStation station = stations.get(k);
if (Math.abs(station.getStation() - kmLoad.getKey()) < EPSILON) {
// Value has been taken at measurement station.
@@ -162,7 +162,7 @@
double endValue = 0d;
// Valid until next measurements stations begin of range,
// or end of current range if last value.
- if (k+2 <= stations.size()) {
+ if (k+2 <= S) {
endValue = stations.get(k+1).getRange().getA().doubleValue();
}
else {
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java Sat Oct 05 17:52:16 2013 +0200
@@ -323,7 +323,7 @@
sqlQuery.setDouble("endKm", endKm);
List<Object []> results = sqlQuery.list();
SedimentLoad[] loads = new SedimentLoad[results.size()];
- for (int i = 0; i < results.size(); i++) {
+ for (int i = 0, R = results.size(); i < R; i++) {
Object[] row = results.get(i);
loads[i] = new SedimentLoad(
(String) row[0],
@@ -345,7 +345,7 @@
List<Object []> results = sqlQuery.list();
SedimentLoad[] loads = new SedimentLoad[results.size()];
- for (int i = 0; i < results.size(); i++) {
+ for (int i = 0, R = results.size(); i < R; i++) {
Object[] row = results.get(i);
loads[i] = new SedimentLoad(
(String) row[0],
@@ -366,7 +366,7 @@
List<Object []> results = sqlQuery.list();
SedimentLoad[] loads = new SedimentLoad[results.size()];
- for (int i = 0; i < results.size(); i++) {
+ for (int i = 0, R = results.size(); i < R; i++) {
Object[] row = results.get(i);
loads[i] = new SedimentLoad(
((Date) row[0]).toString() + (Date) row[1],
@@ -455,7 +455,7 @@
? suspStations
: floatStations;
- for (int i = 0; i < results.size(); i++) {
+ for (int i = 0, R = results.size(); i < R; i++) {
row = results.get(i);
double km = (Double) row[2];
Range range = findMeasurementStationRange(relevantStations, km);
@@ -608,7 +608,7 @@
true,
(String)row[5]);
TDoubleArrayList kms = new TDoubleArrayList();
- for (int i = 0; i < results.size(); i++) {
+ for (int i = 0, R = results.size(); i < R; i++) {
row = results.get(i);
kms.add((Double)row[3]);
load.setLoadTotal((Double)row[3], (Double)row[2]);
@@ -680,7 +680,7 @@
(String)row[5]);
}
- for (int i = 0; i < results.size(); i++) {
+ for (int i = 0, R = results.size(); i < R; i++) {
Object[] row = results.get(i);
double km = (Double)row[3];
Range range = findMeasurementStationRange(stations, km);
@@ -799,7 +799,7 @@
false,
(String)row[5]);
- for (int i = 0; i < results.size(); i++) {
+ for (int i = 0, R = results.size(); i < R; i++) {
row = results.get(i);
SedimentLoadFraction fraction = new SedimentLoadFraction();
fraction.setUnknown((Double)row[3]);
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/states/StaticWQKmsState.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/StaticWQKmsState.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/StaticWQKmsState.java Sat Oct 05 17:52:16 2013 +0200
@@ -85,7 +85,7 @@
// is != -1
boolean qEmpty = true;
TDoubleArrayList qs = wqkms.allQs();
- for (int i = 0; i < qs.size(); i++) {
+ for (int i = 0, Q = qs.size(); i < Q; i++) {
if (qs.getQuick(i) != -1d) {
qEmpty = false;
break;
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityState.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityState.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/BedQualityState.java Sat Oct 05 17:52:16 2013 +0200
@@ -128,11 +128,11 @@
List<String> diameters = access.getBedDiameter();
List<String> loadDiameters = access.getBedloadDiameter();
List<DateRange> ranges = access.getDateRanges();
- for (int i = 0; i < ranges.size(); i++) {
+ for (int i = 0, R = ranges.size(); i < R; i++) {
DateRange range = ranges.get(i);
for (String diameter: diameters) {
int ndxTop = generateIndex(diameter);
- int ndxSub = generateIndex(diameter);
+ int ndxSub = ndxTop; // TODO: Is this correct?
ndxTop += 1;
ndxTop = ndxTop << 3;
ndxSub = ndxSub << 3;
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/exports/LegendProcessor.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/LegendProcessor.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/LegendProcessor.java Sat Oct 05 17:52:16 2013 +0200
@@ -119,7 +119,7 @@
// Remove the shapes of all but the first items,
// to prevent "overfill" of legenditemblock.
- for (int i = 0; i < itemList.size(); i++) {
+ for (int i = 0, I = itemList.size(); i < I; i++) {
if (i != 0) {
LegendItem litem = itemList.get(i);
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java Sat Oct 05 17:52:16 2013 +0200
@@ -83,7 +83,7 @@
doBlackboardPass(themeList, context, outName);
try {
- for (int i = 0; i < themeList.size(); i++) {
+ for (int i = 0, T = themeList.size(); i < T; i++) {
ManagedFacet theme = themeList.get(i);
if (theme == null) {
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/WaterlevelExporter.java Sat Oct 05 17:52:16 2013 +0200
@@ -390,7 +390,7 @@
}
}
} else {
- for (int i = 0; i < wqkms.size(); i++) {
+ for (int i = 0, N = wqkms.size(); i < N; i++) {
dp = wqkms.get(i, dp);
if (dp[2] < last + 1E-5 && dp[2] > first - 1E-5) {
filtered.add(dp[0], dp[1], dp[2]);
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/exports/minfo/BedQualityExporter.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/minfo/BedQualityExporter.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/minfo/BedQualityExporter.java Sat Oct 05 17:52:16 2013 +0200
@@ -15,7 +15,7 @@
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Arrays;
-import java.util.LinkedList;
+import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
@@ -71,18 +71,20 @@
BedDiameterResult[] beds = results[i].getBedResults();
for (int j = 0; j < beds.length; j++) {
TDoubleArrayList bkms = beds[j].getKms();
- for (int k = 0; k < bkms.size(); k++) {
- if (!kms.contains(bkms.get(k))) {
- kms.add(bkms.get(k));
+ for (int k = 0, K = bkms.size(); k < K; k++) {
+ double km = bkms.get(k);
+ if (!kms.contains(km)) { // XXX: O(N^2)
+ kms.add(km);
}
}
}
BedloadDiameterResult[] loads = results[i].getBedloadResults();
for (int j = 0; j < loads.length; j++) {
TDoubleArrayList lkms = loads[i].getKms();
- for (int k = 0; k < lkms.size(); k++) {
- if (!kms.contains(lkms.get(k))) {
- kms.add(lkms.get(k));
+ for (int k = 0, L = lkms.size(); k < L; k++) {
+ double km = lkms.get(k);
+ if (!kms.contains(km)) { // XXX: O(N^2)
+ kms.add(km);
}
}
}
@@ -94,8 +96,8 @@
}
kms.sort();
- List<double[]> rows = new LinkedList<double[]>();
- for (int i = 0; i < kms.size(); i++) {
+ List<double[]> rows = new ArrayList<double[]>(kms.size());
+ for (int i = 0, K = kms.size(); i < K; i++) {
double[] row = new double[cols];
double km = kms.get(i);
row[0] = km;
@@ -134,7 +136,7 @@
}
for (double[] d : rows) {
logger.debug(Arrays.toString(d));
- List<String> cells = new LinkedList<String>();
+ List<String> cells = new ArrayList<String>(d.length);
for (int i = 0; i < d.length; i++) {
if (!Double.isNaN(d[i])) {
NumberFormat nf = Formatter.getFormatter(context, 1, 3);
@@ -174,7 +176,7 @@
protected void writeCSVHeader(CSVWriter writer) {
logger.debug("writeCSVHeader()");
- List<String> header = new LinkedList<String>();
+ List<String> header = new ArrayList<String>();
if (results != null) {
header.add(msg(CSV_HEADER_KM, "km"));
for (int i = 0; i < results.length; i++) {
diff -r 90a3bcd8060a -r fe32a7f9655e artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java Sat Oct 05 12:49:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java Sat Oct 05 17:52:16 2013 +0200
@@ -151,7 +151,7 @@
}
BufferedImage result =
new BufferedImage(size[0], size[1], BufferedImage.TYPE_INT_RGB);
- for (int i = 0; i < charts.size(); i++) {
+ for (int i = 0, S = charts.size(); i < S; i++) {
logger.debug("index: " + i);
JFreeChart chart = charts.get(i);
ChartRenderingInfo info = new ChartRenderingInfo();
More information about the Dive4elements-commits
mailing list