Java 添加到数组并返回布尔值
我想向数组中添加一个对象,如果数组未满,则返回true,以便添加项;如果数组中已满,则返回false 我相信我现在所说的只是部分正确Java 添加到数组并返回布尔值,java,arrays,Java,Arrays,我想向数组中添加一个对象,如果数组未满,则返回true,以便添加项;如果数组中已满,则返回false 我相信我现在所说的只是部分正确 private Object[] theList; //constructors omitted @Override public boolean add(Object toAdd) { for(int i=0; i < theList.length;i++){ if(toAdd != null){
private Object[] theList;
//constructors omitted
@Override
public boolean add(Object toAdd) {
for(int i=0; i < theList.length;i++){
if(toAdd != null){
theList[i] = toAdd;
}
}
return true;
}
私有对象[]列表;
//省略构造函数
@凌驾
公共布尔添加(对象添加){
对于(int i=0;i
我认为使用元素计数器是保持跟踪的正确方法
数组中当前的元素数
如果有必要,该方法将从接口重写
不确定它是基于循环和条件返回true,还是不管发生什么都返回true。第二次编辑,根据同行贡献者的建议,另一个解决方案。我还发布了对评论中问题的理解。如果理解有任何偏差,请提出建议 请看一个可能的解决方案:
public class TestCode {
public static void main(String[] args) {
/*
* Question:
* I would like to add an object to an array and
* return true if the array is not full so the item can be added,
* and false if the array is full of items.
*/
Object[] objects = new Object [] {1, 2, null, 4, 5, null , 7};
SomeClass someClass = new SomeClass(objects);
// just for sake of passing;
Integer toAdd = 10;
boolean isNotFull = someClass.add(toAdd);
System.out.println("Array after adding: "+Arrays.toString(objects));
System.out.println("Array is Not full:"+isNotFull);
isNotFull = someClass.add(toAdd);
System.out.println("Array after adding: "+Arrays.toString(objects));
System.out.println("Array is Not full:"+isNotFull);
isNotFull = someClass.add(toAdd);
System.out.println("Array after adding: "+Arrays.toString(objects));
System.out.println("Array is Not full:"+isNotFull);
}
}
class SomeClass {
private Object[] theList;
//constructors omitted
public SomeClass(Object[] theList) {
this.theList = theList;
}
/*
* returns true if array is not full and element is inserted.
*/
public boolean add(Object toAdd) {
int i = 0;
boolean flag = false; // if flag is true, the array is not empty
// question says:
// "I would like to add an object to an array"
// means we need to first find an empty slot to insert in the array
for( ; i < theList.length;i++){
// to check if there is any first empty space in the array
// so that the element can be inserted.
// (finding the empty slot
if(theList[i] == null){
// to check if elements passed as an aargument itself is false
if (toAdd!=null) {
theList[i] = toAdd;
break;
}
}
}
for (;i <theList.length; i++) {
if(theList[i] == null){
flag = true;
}
}
return flag;
}
}
您尚未初始化
对象[]
ObjectArraytheList
。因此,它将无法向此添加新元素
在这里,我使用ArrayList
类为您的问题提供解决方案ArrayList
是一个可自动增长的数组,在运行时动态增加其大小
看起来您正试图在数组中查找第一个
null
值,如果存在这样的值,请将其替换为toAdd
对象并返回true
这方面的代码大致如下:
private Object[] theList;
@Override
public boolean add(Object toAdd) {
for(int i=0; i < theList.length; i++) {
if(theList[i] == null){
theList[i] = toAdd;
return true;
}
}
return false;
}
显然,使用
数组列表更方便。首先,您必须初始化数组。否则,它将返回空指针异常。这是我的密码。希望它能帮助
class Demo{
static boolean result;
public static void main(String args[]){
Object ob= new Object();
result = A.add(ob);
System.out.println(result);
Object ob1= new Object();
result = A.add(ob1);
System.out.println(result);
}
}
class A{
static private Object[] theList = new Object[10];
public static boolean add(Object ob){
boolean result = false;
for(int i=0; i<theList.length; i++){
if(ob != null){
theList[i] = ob;
result = true;
}
}
return result;
}
}
类演示{
静态布尔结果;
公共静态void main(字符串参数[]){
Object ob=新对象();
结果=A.add(ob);
系统输出打印项次(结果);
对象ob1=新对象();
结果=A.add(ob1);
系统输出打印项次(结果);
}
}
甲级{
静态私有对象[]列表=新对象[10];
公共静态布尔添加(对象ob){
布尔结果=假;
for(int i=0;i对象数组的默认值为null
。这样您就可以检查元素是否为null
for(int i=0:i<theList.length;i++) {
if(theList[i] == null && toAdd!=null) // Element is null or not
theList[i]=toAdd;
return true;
}
return false;
用于(int i=0:i您可能想考虑使用一个数组,因为它们是可扩展的,没有设置的大小。因此,每当添加某些东西时,它只会增加数组列表而不是改变索引的值。更好地使用<代码>集合<代码> >代码> ARRAYLAMP类如果在执行时不知道数据数组的长度。程序Thanks@VikrantKashyap这不是我的问题。我只是回答了。我知道这有点荒谬,但只是给出了答案。1)什么三元运算符?2)length
将返回数组的容量,而不管数组的内容如何。@RobbyCornelissen更改了代码。早些时候误读了问题。您的代码(就像OP的代码一样)将立即用您尝试添加的第一个值填充数组的所有位置。@VikrantKashyap请保持礼貌。您不是世界上唯一的Java开发人员。是的,ArrayList更方便,但我的老师想用数组来挑战我们。
private Object[] theList;
@Override
public boolean add(Object toAdd) {
for(int i=0; i < theList.length; i++) {
if(theList[i] == null){
theList[i] = toAdd;
return true;
}
}
return false;
}
@Override
public boolean add(Object toAdd) {
if (toAdd != null) {
for(int i=0; i < theList.length; i++) {
if(theList[i] == null){
theList[i] = toAdd;
return true;
}
}
}
return false;
}
class Demo{
static boolean result;
public static void main(String args[]){
Object ob= new Object();
result = A.add(ob);
System.out.println(result);
Object ob1= new Object();
result = A.add(ob1);
System.out.println(result);
}
}
class A{
static private Object[] theList = new Object[10];
public static boolean add(Object ob){
boolean result = false;
for(int i=0; i<theList.length; i++){
if(ob != null){
theList[i] = ob;
result = true;
}
}
return result;
}
}
for(int i=0:i<theList.length;i++) {
if(theList[i] == null && toAdd!=null) // Element is null or not
theList[i]=toAdd;
return true;
}
return false;