如何在此代码中消除输出结果中的重复

时间:2018-10-16 10:05:32

标签: java arrays algorithm

public class ValidInvalidNodes {
public static void main(String[] args) {

    int[][] arr = { 
            { 5, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },             
            { 0, 5, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 5, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            { 0, 0, 0, 5, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, 
            { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, 0, 0 }, 
            { 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0 },        
            { 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0 },  
            { 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 1, 0, 0, 0, 0 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, -1, 0, 0, 0 },  
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, -1, 0, 0, 0 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1, 0, 0 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1, 0 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, -1 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -1 }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 }, 
    };
    int position = 0;
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            if (arr[i][j] == 1) {
                position = j;

                if (arr[position][j] == 5)

                    System.out.print(" Valid Static Node V" + j + " \n");

            }
            if (arr[i][j] == -1) {
                position = j;

                if (arr[position][j] == 5)

                    System.out.print(" InValid Static Node V" + j + " \n");
            }
            if (arr[i][j] == -1) {
                position = j;

                if (arr[position][j] == 55)

                    System.out.print(" InValid Mobile Node V" + j + " \n");
            }

            if (arr[i][j] == 1) {
                position = j;

                if (arr[position][j] == 55)

                    System.out.print(" Valid Mobile Node V" + j + " \n");  

            }

        }
    }
    // System.out.println("");
}

}

输出:

Valid Static Node V1 
 Valid Static Node V2 
 Valid Static Node V3 
 Valid Static Node V4 
 InValid Static Node V5 
 InValid Static Node V6 
 Valid Static Node V7 
 Valid Static Node V8 
 Valid Static Node V9 
 Valid Mobile Node V10 
 InValid Static Node V11 
 InValid Mobile Node V12 
 InValid Static Node V16 
 InValid Static Node V16 
 InValid Static Node V13 
 InValid Static Node V13 
 Valid Static Node V14 
 Valid Static Node V14 
 Valid Static Node V14 
 Valid Static Node V14 
 InValid Static Node V15 
 InValid Static Node V15 
 InValid Static Node V16 
 InValid Static Node V17 
 InValid Static Node V18 
 InValid Static Node V18 
 InValid Static Node V18 

1 个答案:

答案 0 :(得分:1)

您可以将所有行放在java.util.LinkedHashSet<String>中。 Set表示它将仅保留不同的项目。我们将使用LinkedHashSet而不是常规HashSet的原因是为了保留输入顺序。

将它们全部添加到集合中后,我们将其打印出来。

这里是您的代码的修改:

int[][] arr = { 
        { 5, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },             
        { 0, 5, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 5, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 5, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, 0, 0 }, 
        { 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0 },        
        { 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0 },  
        { 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 1, 0, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, -1, 0, 0, 0 },  
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, -1, 0, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1, 0, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1, 0 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, -1 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, -1 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -1 }, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 }, 
};
java.util.Set<String> outputLines = new java.util.LinkedHashSet<>();
int position = 0;
for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[i].length; j++) {
        if (arr[i][j] == 1) {
            position = j;

            if (arr[position][j] == 5)
                outputLines.add("Valid Static Node V" + j);
        }
        if (arr[i][j] == -1) {
            position = j;

            if (arr[position][j] == 5)
                outputLines.add("InValid Static Node V" + j);
        }
        if (arr[i][j] == -1) {
            position = j;

            if (arr[position][j] == 55)
                outputLines.add("InValid Mobile Node V" + j);
        }
        if (arr[i][j] == 1) {
            position = j;

            if (arr[position][j] == 55)
                outputLines.add("Valid Mobile Node V" + j);  
        }
    }
}

for(String outputLine : outputLines){
  System.out.println(outputLine);
}

哪个会输出:

Valid Static Node V1
Valid Static Node V2
Valid Static Node V3
Valid Static Node V4
InValid Static Node V5
InValid Static Node V6
Valid Static Node V7
Valid Static Node V8
Valid Static Node V9
Valid Mobile Node V10
InValid Static Node V11
InValid Mobile Node V12
InValid Static Node V16
InValid Static Node V13
Valid Static Node V14
InValid Static Node V15
InValid Static Node V17
InValid Static Node V18

Try it online.


您还可以简化嵌套循环中的if-check。例如:

position = j;
if (arr[i][j] == 1) {
    if (arr[position][j] == 5)
        outputLines.add("Valid Static Node V" + j);
    else if(arr[position][j] == 55)
        outputLines.add("Valid Mobile Node V" + j);
}
else if (arr[i][j] == -1) {
    if (arr[position][j] == 5)
        outputLines.add("InValid Static Node V" + j);
    else if (arr[position][j] == 55)
        outputLines.add("InValid Mobile Node V" + j);
}

Try it online.



自从OP以后要求没有任何库的解决方案以来,这里是一种替代方法:

// Loop over the diagonal
for(int diag = 0; diag < arr.length; diag++){
  String nodeType = arr[diag][diag] == 5?
                     "Static"
                    :
                     "Mobile"; // Assumes the nodeType is ALWAYS one of 5 or 55
  // Loop over the rows above the diagonal for the current column
  int node = 0;
  for(int row = 0; row < diag; row++){
    // Bitwise-OR the node with the current value
    //  0  | 0 remains 0
    //  0  | 1 becomes 1
    //  0  | -1 becomes -1
    //  1  | 1 remains 1
    //  -1 | -1 remains -1
    //  (NOTE: 1 | -1 becomes -1, but I assumed a column will never contain both a 1 and -1)
    node |= arr[row][diag];
  }
  String nodeValid = node == 1?
                      "Valid" 
                     :node == -1?
                      "Invalid"
                     :
                      null;
  // Only print the node if the column contained a 1 or -1:
  if(nodeValid != null)
    System.out.println(nodeValid + " " + nodeType + " Node V" + diag);
}

Try it online.