我正在尝试使用for循环遍历HashMap
的所有部分。但问题是,在for循环体内,代码没有完全执行。也就是说,它只发生在某一点上然后停止。我的代码是:
public static void printCombination(String arr[], int n, int r)
{
// A temporary array to store all combination one by one
Log.e("ERROR","ATLEAST I'M INSIDE");
String data[]=new String[r];
for(int no = 0;no<arr.length;no++){
boolean decider = true;
if (latest.cart_names.size()>0) {
for (Map.Entry<String, Map<String, Integer>> entry : latest.cart_names.entrySet()) {
Log.e("ERROR", "ATLEAST I'M INSIDE 1");
String arrayElement = arr[no];
Map<String, Integer> outerMapValue = entry.getValue();
Log.e("ERROR", "ATLEAST I'M INSIDE 2");
if (outerMapValue.containsKey(arrayElement)) {
Log.e("ERROR", "ATLEAST I'M INSIDE 3");
if (outerMapValue.get(arrayElement) != null) {
Log.e("ERROR", "ATLEAST I'M INSIDE 4");
if (outerMapValue.get(arrayElement) > 0) {
return;
}else{
Log.e("ERROR", "ATLEAST I'M INSIDE 5");
decider = false;
}
}
}
}
Log.e("ERROR",Boolean.toString(decider));
Log.e("ERROR",Integer.toString(finalmap.size()));
if (finalmap.size()==0) {
if (decider) {
Log.e("ERROR", "ATLEAST I'M INSIDE 8");
for (int rt = 0; rt < latest.cart_names.size(); rt++) {
Log.e("ERROR", "ATLEAST I'M INSIDE 6");
finalmap.add(rt,arr[no]);
}
}
}else{
if (decider) {
int sum1 = 0;
int sum2 = 0;
for (Map.Entry<String, Map<String, Integer>> entry : latest.cart_names.entrySet()) {
sum1 += latest.cart_names.get(entry.getKey()).get(finalmap);
sum2 += latest.cart_names.get(entry.getKey()).get(arr[no]);
}
Log.e("ERROR",Integer.toString(sum1));
Log.e("ERROR",Integer.toString(sum2));
if (sum2>sum1){
for (int rt = 0; rt < latest.cart_names.size(); rt++) {
Log.e("ERROR", "ATLEAST I'M INSIDE 7");
finalmap.add(arr[no]);
}
}
}
}
}}
它不是记录行:
Log.e("ERROR",Boolean.toString(decider));
Log.e("ERROR",Integer.toString(finalmap.size()));
答案 0 :(得分:3)
满足此条件并返回/退出方法......
if (outerMapValue.get(arrayElement) > 0) {
return;
作为@petey和@Andrew L发表评论(感谢您的建议(:)
您应该考虑查看Java最佳做法以及如何使用branching control statement
continue和break语句可用于跳过for的当前迭代, while,或do-while循环。
答案 1 :(得分:0)
由于循环中的return
语句,我猜它没有记录您列出的行。如果您希望循环在该点终止并仍然记录行,则应使用break
代替。