如何只显示数组中可用的元素?

lhcgjxsq  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(332)

如果我不“删除”数组中间的一个元素,我可以在数组中放置和“删除”元素,也可以打印它们,因为我的代码只在0出现之前显示,如果0是数组中的一个元素,打印就会停止。我试过这样做:

public static void showUsers() {
    for (int i = 0; i <= 100; i++) {
        if (users[i] == 0 && i == 0) {
            System.out.println("There's no users");
            //goes back to the main menu

            return;
        } else if (users[i] == 0) {
            //goes back to the main menu

            return;
        }
        System.out.println("List of Users: " + users[i]);
    }
    //goes back to the menu

    return;
}

我也尝试过使用do while循环:

public static void showUsers() {
    int i = 0;

    if (users[i] == 0 && i == 0) {
        System.out.println("There's no users");
        //goes back to the main menu

        return;
    }

    do {
        if (users[i] == 0) {
            i++;
            return;
        }
        System.out.println("List of Users:" + users[i]);
        i++;
    } while (users[i] == 0 || i <= 100);

    //goes back to the main menu

    return;
}

因此,如果我创建3个用户,(1,2,3,0,0,0,…),它将打印数字1,2和3,但如果我“删除”数字2,那么数组将变成(1,0,3,0,0,0,…),它将只打印数字1。我试过的两个案子都是这样。
我想要的是,在“删除”数字2之后,打印数字1和3。
我应该改变打印的方式还是“消除”的方式以及如何改变?

wz8daaqr

wz8daaqr1#

首先,为什么你的代码会这样?循环中的代码在循环的每次迭代中都会运行。因此,如果其中有一个if语句,并且if语句的主体包含一个return,那么在任何给定的迭代中,只要if语句为true,方法就会立即返回。
现在,修复你的代码。要跳过给定的迭代,请使用 continue 声明。这使得for循环继续到下一个迭代,但不会从方法返回。对于“没有用户”检查,可以使用布尔变量。将其初始化为true,并且无论何时打印出用户,都将其设置为false。这样,在for循环之后,如果变量仍然为true,就知道没有用户。以下是我对你的代码的两个修订:

public static void showUsers(){
    // declaring the boolean variable:
    boolean noUsers = true;

    System.out.println("List of Users:");

    // for loop loops over all the values of the array, not 100 times
    for (int i = 0; i < users.length; i++) {
        if (users[i] == 0) {
            // go to the next iteration
            continue;
        }

        // if we got this far, this element is not 0
        noUsers = false;
        System.out.println(users[i]);
    }

    //if noUsers is still true, we know there were no users other than 0's
    if (noUsers) {
        System.out.println("There's no users");
    }
}

我们可以在这里做的一个改进是使用增强的for循环。这允许您除去该局部变量 i (它仍然在内部使用,但我们不担心这里的性能)并且只处理数组元素。以下是我的更新版本:

public static void showUsers(){
    boolean noUsers = true;

    System.out.println("List of Users:");

    // enhanced for loop is more concise and it covers up the i variable:
    for (int user : users) {
        if (user == 0) {
            continue;
        }

        noUsers = false;
        System.out.println(user);
    }

    if (noUsers) {
        System.out.println("There's no users");
    }
}

另一个改进是,如果希望“用户列表:”标题仅在有用户时出现。看看你的代码,这似乎是你想要的。要实现这一点,您只需缓冲输出并稍后打印,而不是立即打印。如果使用stringbuilder缓冲输出,则可以在for循环之后检查stringbuilder是否为空,从而消除对布尔变量的需要。下面是我将如何实现这一点:

public static void showUsers(){
    // StringBuilder lets you build up a string without printing out
    // it is basically the same as a string, but it only performs the concatenation when
    // you tell it to
    StringBuilder output = new StringBuilder();

    for (int user : users) {
        if (user == 0) {
            continue;
        }

        // add the user to the StringBuilder instead of printing it out:
        output.append(user).append(System.lineSeparator());
    }

    if (output.length() == 0) {
        // if there are no users, the heading is not printed out
        System.out.println("There's no users");
    } else {
        System.out.println("List of Users:");

        // this causes an implicit call to the StringBuilder's toString() method,
        // which performs the concatenation and gives you a string you can print out
        System.out.println(output);
    }
}
rnmwe5a2

rnmwe5a22#

你可以用这个方法 IntStream.filter 为此目的:

int[] users = {1, 0, 2, 3, 0, 0};

Arrays.stream(users)
        // filter out zero users
        .filter(user -> user != 0)
        // print users in one line
        .forEach(user -> System.out.println(user + ", "));

输出:

1, 2, 3,

相关问题