Java arraylist是空的怀疑和问题

Java arraylist是空的怀疑和问题,java,list,arraylist,Java,List,Arraylist,我有个问题。 我想检查指定索引的ararylist是否为空。 用户可能在my arraylistindex 0到6中有7个输入存储项。 所以我想检查索引7是否为空,如果为空,我将循环回我的RoomSelection 我正在使用roomInfo4+XXismpty 用于检查整个arraylist是否为空的IsEmpty命令是juz吗 for (int x = 0; x < (Integer) roomInfo.get(2); x++) {//start of number of room l

我有个问题。 我想检查指定索引的ararylist是否为空。 用户可能在my arraylistindex 0到6中有7个输入存储项。 所以我想检查索引7是否为空,如果为空,我将循环回我的RoomSelection

我正在使用roomInfo4+XXismpty

用于检查整个arraylist是否为空的IsEmpty命令是juz吗

for (int x = 0; x < (Integer) roomInfo.get(2); x++) {//start of number of room loop(to check how many rooms user input)

    for (int i = 0; i < (Integer) roomInfo.get(4 + xx); i++) { //start of number of add-on loop(to check how many add-on user input)


        System.out.println("addOns array contains what? : " + addOns);    // for my own reference
        System.out.println("Enter Add-On option");
        ao2 = input.nextInt();
        while (ao2 > 4) {
            System.out.println("Please enter again! Choose only option 1 to 4");
            ao2 = input.nextInt();
        }
        addOnOpt = addOn[ao2 - 1];
        addOns.add(addOnOpt);
        addOnPrice = priceAdd[ao2 - 1];
        addOns.add(addOnPrice);
        System.out.println("Enter quantity required for Add-On option " + (i + 1) + ": ");
        quan = input.nextInt();
        addOns.add(quan);
        xx += 3;
        System.out.println(" not null yet");
        if ((roomInfo.isEmpty(4 + xx) == true) {//if condition to check whether is the arrayllist of position is not null
            System.out.println("null!");
            xx = 0;
            Selection();
        }


    }// end of add-on loop

}//end of number of room loop
如果要检查整个arraylist,我还可以使用什么方法检查索引7是否为空

for (int x = 0; x < (Integer) roomInfo.get(2); x++) {//start of number of room loop(to check how many rooms user input)

    for (int i = 0; i < (Integer) roomInfo.get(4 + xx); i++) { //start of number of add-on loop(to check how many add-on user input)


        System.out.println("addOns array contains what? : " + addOns);    // for my own reference
        System.out.println("Enter Add-On option");
        ao2 = input.nextInt();
        while (ao2 > 4) {
            System.out.println("Please enter again! Choose only option 1 to 4");
            ao2 = input.nextInt();
        }
        addOnOpt = addOn[ao2 - 1];
        addOns.add(addOnOpt);
        addOnPrice = priceAdd[ao2 - 1];
        addOns.add(addOnPrice);
        System.out.println("Enter quantity required for Add-On option " + (i + 1) + ": ");
        quan = input.nextInt();
        addOns.add(quan);
        xx += 3;
        System.out.println(" not null yet");
        if ((roomInfo.isEmpty(4 + xx) == true) {//if condition to check whether is the arrayllist of position is not null
            System.out.println("null!");
            xx = 0;
            Selection();
        }


    }// end of add-on loop

}//end of number of room loop
如果列表中没有项,isEmpty将返回true

要知道是否有第7项,请使用size方法检查列表的大小是否大于或等于7。

如果列表中没有项目,isEmpty将返回true

要知道是否有第7项,请使用大小方法检查列表的大小是否优于或等于7

空空如也 测试此列表是否没有元素

如果要检查整个arraylist,我还可以使用什么方法 用于检查索引7是否为空

for (int x = 0; x < (Integer) roomInfo.get(2); x++) {//start of number of room loop(to check how many rooms user input)

    for (int i = 0; i < (Integer) roomInfo.get(4 + xx); i++) { //start of number of add-on loop(to check how many add-on user input)


        System.out.println("addOns array contains what? : " + addOns);    // for my own reference
        System.out.println("Enter Add-On option");
        ao2 = input.nextInt();
        while (ao2 > 4) {
            System.out.println("Please enter again! Choose only option 1 to 4");
            ao2 = input.nextInt();
        }
        addOnOpt = addOn[ao2 - 1];
        addOns.add(addOnOpt);
        addOnPrice = priceAdd[ao2 - 1];
        addOns.add(addOnPrice);
        System.out.println("Enter quantity required for Add-On option " + (i + 1) + ": ");
        quan = input.nextInt();
        addOns.add(quan);
        xx += 3;
        System.out.println(" not null yet");
        if ((roomInfo.isEmpty(4 + xx) == true) {//if condition to check whether is the arrayllist of position is not null
            System.out.println("null!");
            xx = 0;
            Selection();
        }


    }// end of add-on loop

}//end of number of room loop
假设你有:

ArrayList<abc> list=new ArrayList<abc>();
检查此链接以了解ArrayList上的其他方法

空空如也 测试此列表是否没有元素

如果要检查整个arraylist,我还可以使用什么方法 用于检查索引7是否为空

for (int x = 0; x < (Integer) roomInfo.get(2); x++) {//start of number of room loop(to check how many rooms user input)

    for (int i = 0; i < (Integer) roomInfo.get(4 + xx); i++) { //start of number of add-on loop(to check how many add-on user input)


        System.out.println("addOns array contains what? : " + addOns);    // for my own reference
        System.out.println("Enter Add-On option");
        ao2 = input.nextInt();
        while (ao2 > 4) {
            System.out.println("Please enter again! Choose only option 1 to 4");
            ao2 = input.nextInt();
        }
        addOnOpt = addOn[ao2 - 1];
        addOns.add(addOnOpt);
        addOnPrice = priceAdd[ao2 - 1];
        addOns.add(addOnPrice);
        System.out.println("Enter quantity required for Add-On option " + (i + 1) + ": ");
        quan = input.nextInt();
        addOns.add(quan);
        xx += 3;
        System.out.println(" not null yet");
        if ((roomInfo.isEmpty(4 + xx) == true) {//if condition to check whether is the arrayllist of position is not null
            System.out.println("null!");
            xx = 0;
            Selection();
        }


    }// end of add-on loop

}//end of number of room loop
假设你有:

ArrayList<abc> list=new ArrayList<abc>();

检查此链接以了解ArrayList上的其他方法

如果ArrayList包含七个或更少的项,或者您在索引7中添加了null,则第八项索引7将为空;您需要测试这两个条件:

if(roomInfo.size() < 8 || roomInfo.get(7) == null) {
    // index 7 is "empty"
}

如果ArrayList包含七个或更少的项,或者您在索引7中添加了null,则第八项索引7将为空;您需要测试这两个条件:

if(roomInfo.size() < 8 || roomInfo.get(7) == null) {
    // index 7 is "empty"
}

IsEmpty方法仅检查列表是否没有元素


当元素添加到ArrayList时,其容量会自动增长,因此要检查第7位,请使用.size

检查列表的大小。IsEmpty方法仅检查列表是否没有元素

当元素添加到ArrayList时,其容量会自动增长,因此要检查第7位,请使用.size检查列表的大小。ArrayList上的空函数用于检查整个ArrayList是否为空。 您可能可以选择每个元素并进行检查。

arraylist上的isEmpty函数用于检查整个arraylist是否为空。
您可能可以选择每个元素并进行检查。

如果我的索引7没有任何内容,我就需要执行ifroomInfo.size如果我的索引7没有内容,我就需要执行ifroomInfo.size