Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查是否已将数据添加到数组java_Java_Arraylist - Fatal编程技术网

检查是否已将数据添加到数组java

检查是否已将数据添加到数组java,java,arraylist,Java,Arraylist,我想检查数据是否已添加到数组中。当数据插入数组时,我想向用户发送一个messagebox 我现在拥有的是 public static ArrayList<ModuleData> modules; public Module() { modules = new ArrayList<ModuleData>(); } public void addModule(String naamModule, int modulenr, int aantalUren, int

我想检查数据是否已添加到数组中。当数据插入数组时,我想向用户发送一个messagebox

我现在拥有的是

public static ArrayList<ModuleData> modules;

public Module()
{
    modules = new ArrayList<ModuleData>();
}

public void addModule(String naamModule, int modulenr, int aantalUren, int weekBegin, int weekEind, String opleiding, int opleidingJaar)
{
    modules.add(new ModuleData(naamModule, modulenr, aantalUren, weekBegin, weekEind, opleiding, opleidingJaar));
}
公共静态数组列表模块;
公共模块()
{
modules=newarraylist();
}
public void addModule(字符串naamModule、int modulern、int aantalUren、int weekBegin、int weekEind、字符串opleiding、int opleidingJaar)
{
添加(新模块数据(naamModule、modulenr、aantalUren、weekBegin、weekEind、opleiding、opleidingJaar));
}
我在考虑如果modules.add成功返回true或false

public boolean addModule(String naamModule, int modulenr, int aantalUren, int weekBegin, int weekEind, String opleiding, int opleidingJaar)
{
    boolean added = true;
    try {
        modules.add(new ModuleData(naamModule, modulenr, aantalUren, weekBegin, weekEind, opleiding, opleidingJaar));
    } catch (Exception e) {
       added = false;
    }
    return added;
}
添加日志和处理方法输入也很好


添加日志和处理方法输入也会很好。

ArrayList
是一个
集合
,因此
模块
将返回
true
,如果在插入时更改(如上所述)。如果收到
true
,则添加了一些内容

public void addModule(String naamModule, int modulenr, int aantalUren, int weekBegin, int weekEind, String opleiding, int opleidingJaar){
    boolean dataAdded = false;
    try {
        dataAdded = modules.add(new ModuleData(naamModule, modulenr, aantalUren, weekBegin, weekEind, opleiding, opleidingJaar));
    } catch (Exception e) {
       // handle exception
    }

    if( dataAdded ){
       // notify user about success 
    }
}

或者您可以重写
addModule()
方法以返回
dataAdded
并在外部处理响应。

ArrayList
是一个
集合
,因此
模块
如果在插入时发生更改,将返回
true
。如果收到
true
,则添加了一些内容

public void addModule(String naamModule, int modulenr, int aantalUren, int weekBegin, int weekEind, String opleiding, int opleidingJaar){
    boolean dataAdded = false;
    try {
        dataAdded = modules.add(new ModuleData(naamModule, modulenr, aantalUren, weekBegin, weekEind, opleiding, opleidingJaar));
    } catch (Exception e) {
       // handle exception
    }

    if( dataAdded ){
       // notify user about success 
    }
}

或者您可以重写
addModule()
方法以返回
dataAdded
并在外部处理响应。

什么是
modules
,一个集合?我编辑了这个问题,您可以试着说
returnmodules.add(…)
,因为ArrayList的
add()
如果调用导致集合更改,则方法返回true。因此,如果添加了返回值,则返回值为真,否则返回值为假。如果您发现我的答案令人满意,请将其设置为已接受(stackoverflow.com/help/accepted answer)。什么是
模块
,集合?我编辑了问题,您可以尝试说
返回模块。添加(…)
,因为ArrayList的
添加()
如果调用导致集合更改,则方法返回true。因此,如果它被添加,则返回值将为真,否则返回值将为假。如果您发现我的答案令人满意,请将其设置为已接受(stackoverflow.com/help/accepted answer)。