检查数组是否已满

编程入门 行业动态 更新时间:2024-10-26 05:27:38
本文介绍了检查数组是否已满的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试检查数组是否已满,并打印出无法输入更多书籍的用户.

I am trying to do check if an array is full and print out to the user he is unable to enter more books.

static Object[][] books=new Object[2][];

我正在向用户询问3个值,并将其存储到另一个名为行"的数组中.

I am asking 3 values from the user and am storing to another array called "row".

Object[]row=new Object[3];

此后,我遍历books数组,并检查其中是否具有空值,并使用给定的用户值添加"row"数组.问题是输入两行值后,如果books数组已满,我无法给出任何反馈.

After that i loop through the books array and check if it has a null value inside of it and add the "row" array with the given values of the user. the problem am having is i cant give any feedback if books array is full after entering two rows of values.

boolean empty=false; while(empty==false){ for (int i = 0; i < books.length; i++) { if(books[i]==null){ books[i]=row; empty=true; break; } } }

推荐答案

为什么没有变量

int bookCounter = 0;

您可以在每次添加/删除书籍时增加/减少,然后对照最大书籍数(我认为是 books.length )进行检查

which you can increase/decrease everytime you add/delete books and then just check it against your max number of books (which I assume is books.length)

通过这种方式,您不需要遍历数组,这效率不高.

In this way you don't need to loop over the array, which is not efficient.

更多推荐

检查数组是否已满

本文发布于:2023-11-22 15:18:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617945.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   已满

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!