【问题标题】:How to only show the available elements in an array?如何仅显示数组中的可用元素?
【发布时间】:2021-04-10 19:10:58
【问题描述】:

如果我不“删除”数组中间的元素之一,我可以放置和“删除”数组中的元素以及打印它们,因为我的代码只会让它显示到 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。

我应该改变我的打印方式还是我“消除”的方式以及如何?

【问题讨论】:

  • 您不能从数组中“删除”元素,数组永远不会稀疏,并且它们从初始化时就具有固定的长度。使用List
  • 当我说“删除”时,它会将数字设为 0。在这种情况下,该人将输入 2 作为输入,而 2 将变为 0。
  • 你能否发送一个关于列表的链接顺便说一句,我对此有点陌生,我还没有看过列表

标签: java arrays filtering


【解决方案1】:

您可以为此使用方法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, 

【讨论】:

    【解决方案2】:

    首先,为什么您的代码会这样运行?循环内的代码在循环的每次迭代中运行。所以,如果你有一个 if 语句,并且 if 语句的主体包含一个 return,那么只要该 if 语句在任何给定的迭代中为真,该方法就会立即返回。

    现在,修复您的代码。要跳过给定的迭代,请使用 continue 语句。这使得 for 循环继续进行下一次迭代,但 not 从该方法返回。对于您的“没有用户”检查,您可以使用布尔变量。您将其初始化为 true,并且无论何时打印出用户,都将其设置为 false。这样,在 for 循环之后,如果变量仍然为真,你就知道你没有用户。这是我对您的代码应用的两个修订:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      相关资源
      最近更新 更多