java:如果语句跳过以下语句
问题描述:
我有一个2D-String-array,我正在逐行查找最小值。当我发现最小值时,我将找到的最小值的行添加到ArrayList中,以便在下一次迭代中跳过该行以查找数组的第二个最小值。我这样做直到每个行号都是ArrayList的一部分。java:如果语句跳过以下语句
见到目前为止我的代码:
List assignedRow = new ArrayList();
for(int t = 0; t < excelMatrix.length-1; t++){
double lowest = Double.parseDouble(excelMatrix[0][1]);
int row = 0, column = 0;
for(int r = 0; r < excelMatrix.length-1; r++){
if(assignedRow.contains(r) == true) continue;
for(int c = 1; c < excelMatrix[r].length; c++){
double value = Double.parseDouble(excelMatrix[r][c]);
if(lowest > value) {
lowest = value;
row = r;
column = c;
}
}
}
assignedRow.add(row);
}
的代码工作正常,到目前为止,直到最小是0排此后,它一直执行下去的条件。
我现在正在寻找一些东西,允许我回到for循环,使用下一个更高的r,它不是ArrayList的一部分。
我希望我明确了我的问题。
答
我希望这会起作用。
List assignedRow = new ArrayList();
double lowest = 0;
while (assignedRow.size() < excelMatrix.length) {
// find intial lowest value from non assign row to compare
for (int t = 0; t < excelMatrix.length - 1; t++) {
if (!assignedRow.contains(t)) {
lowest = Double.parseDouble(excelMatrix[t][0]);
break;
}
}
int row = 0, column = 0;
for (int r = 0; r < excelMatrix.length - 1; r++) {
if (assignedRow.contains(r)) continue;
for (int c = 0; c < excelMatrix[r].length; c++) {
double value = Double.parseDouble(excelMatrix[r][c]);
if (lowest > value) {
lowest = value;
row = r;
column = c;
}
}
}
assignedRow.add(row);
}
如果最低值存储在任意行的第一列,因为你开始从列索引1迭代(第三for循环int c=1
和lowest = Double.parseDouble(excelMatrix[0][1]
)
+0
非常感谢你!有用! :d – boersencrack
只是一个侧面说明(这不是你的代码将无法正常工作问题):'if(assignedRow.contains(r)== true)'只是写了'if(assignedRow.contains(r))'很长的路要走。 'contains' * already *返回一个'boolean',你不需要使用'== true'来获得一个。我的意思是,你在哪里停下来? if((assignedRow.contains(r)== true)== true)'? if(((assignedRow.contains(r)== true)== true)== true)'? **; - )** –
恐怕我不能完全理解你想要做什么。 –
'if(assignedRow.contains(r)== true)'只会跳过那些已经有最小值的行。如果最小和第二最小值在同一行中,则会出现问题 – Sanjeev