是否可以在数组中使用If Else语句?

Joe*_*oey -3 java arrays methods loops function

import java.util.Arrays;

public class Arrays2 {

  public static void main(String[] args){
    int [] array = {1,2,10,20};

    addUp(array);
    showUp(array);
  }

  public static void addUp(int x[]){
    System.out.println("AddUp Function");
    for (int i = 0; i < x.length; i++){
      System.out.print(x[i]+"\t");
    }
    System.out.println ("\n");

  }

  public static void showUp(int y[]){
    System.out.println("ShowUp Function which Multiplies by 2!");
    **If(array[] >= 10)**
    for (int k = 0; k < y.length; k++){
      System.out.print(y[k]*2+"\t");
    }
  } 
}
Run Code Online (Sandbox Code Playgroud)

如何在数组中应用if-else语句?在我的showUp方法中,如果值大于10,它应该乘以数组中的元素.

tob*_*s_k 5

你可能想要这样的东西:

for (int k = 0; k < y.length; k++) {
    if (y[k] >= 10) {
        System.out.print(y[k] * 2 + "\t");
    }
}
Run Code Online (Sandbox Code Playgroud)

你写的东西看起来和filter其他一些编程语言类似,但Java没有.您必须迭代数组并在循环使用if检查.