如何优化这个功能?

时间:2012-09-26 15:26:11

标签: java optimization

如何尽可能优化此功能

public void r1(String st1, int[] ar1) {
    String inox = "newsearch";
    for (int j = 0; j < ar1.length; j++) {
        if (st1.equals(inox) && ar1[j] * 2 > 20) {
            Integer intx = new Integer(ar1[j]);
            intx = intx * 2;
            System.out.print(intx.toString());
        }
    }
}

4 个答案:

答案 0 :(得分:6)

这是相当奇怪的代码,但它与。

相同
public void r1(String st1, int[] ar1) {
    if (!str1.equals("newsearch")) return;

    for (int j : ar1) {
        int j2 = j * 2;
        if (j2 > 20) 
            System.out.print(j2);
    }
}

答案 1 :(得分:0)

public void r1(String st1, int[] ar1) {
    String inox = "newsearch";
    if (st1.equals(inox) {
        for (int j = 0; j < ar1.length; j++) {
             if (ar1[j] > 10) {
                System.out.print(ar1[j] * 2);
            }
        }
    }
}

答案 2 :(得分:0)

public void r1(String st1, int[] ar1) {
  if (st1.equals("newsearch") {
    for (int j = 0; j < ar1.length; j++) {
      if (ar1[j] > 10) {
        System.out.print(ar1[j] * 2);
      }
    }
  }
}

答案 3 :(得分:0)

建立在Peter Lawreys版本之上:

public void r1(String st1, int[] ar1) {
    if (!str1.equals("newsearch"))
        return;
    for (int j : ar1)
        if (j > 10)
            System.out.print(j << 1);
}