Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 如何确定arraylist是否缺少数组中的一个且仅缺少一个元素?_Java_Arrays_Arraylist - Fatal编程技术网

Java 如何确定arraylist是否缺少数组中的一个且仅缺少一个元素?

Java 如何确定arraylist是否缺少数组中的一个且仅缺少一个元素?,java,arrays,arraylist,Java,Arrays,Arraylist,我有一个名为“手”的字符串数组列表,它从3个不同的字符串数组、人、武器和房间中获取随机元素。是否有任何方法可以确定arraylist是否包含每个类别中除一个之外的所有元素?因此,如果“hand”包含字符串数组房间中9个字符串中的8个,它将返回该数组中没有的字符串?仅当指定数组中的“hand”正好缺少1个元素时,此方法才应适用。如果指定数组中缺少多个元素,则不应执行任何操作 import java.util.ArrayList; import java.util.List; public cla

我有一个名为“手”的字符串数组列表,它从3个不同的字符串数组、人、武器和房间中获取随机元素。是否有任何方法可以确定arraylist是否包含每个类别中除一个之外的所有元素?因此,如果“hand”包含字符串数组房间中9个字符串中的8个,它将返回该数组中没有的字符串?仅当指定数组中的“hand”正好缺少1个元素时,此方法才应适用。如果指定数组中缺少多个元素,则不应执行任何操作

import java.util.ArrayList;
import java.util.List;

public class Main {

public List<String> hand = new ArrayList<String>();

public final static String[] PEOPLE = new String[] {
        "Miss. Scarlet",
        "Mrs. Peacock",
        "Colonel Mustard",
        "Professor Plum",
        "Mrs. White",
        "Mr. Green"
    };

public final static String[] WEAPONS = new String[] {
        "Wrench",
        "Candlestick",
        "Pipe",
        "Rope",
        "Revolver",
        "Knife"
    };

public final static String[] ROOMS = new String[] {
        "Library",
        "Kitchen",
        "Study",
        "Conservatory",
        "Ballroom",
        "Lounge",
        "Hall",
        "Billiard Room",
        "Dining Room"
    };

public Main() {
    hand.add("Library");
    hand.add("Lounge");
    hand.add("Wrench");
    hand.add("Miss. Scarlet");
    hand.add("Mrs. Peacock");
    hand.add("Colonel Mustard");
    hand.add("Professor Plum");
    hand.add("Mrs. White");
}

public static void main(String[] args) {
    Main main = new Main();
}
}

我想这就是您要寻找的:在列表上使用removeAll方法。 所以,将数组转换为带有数组的列表。asList。。。 而不是从以前的阵列中移除所有手部集合。如果剩余列表的大小为1-这就是您要查找的内容

List<String> peoples = new ArrayList<>(Arrays.asList(PEOPLE));
peoples.removeAll(hands);
if (peoples.size() == 1)
{
    // here your hands List contained all items from PEOPLE, except 1
}
声明一个方法,该方法接受两个参数:常量列表和您的手,并且将返回一个字符串:缺少的字符串元素(如果是最后一个缺少的字符串元素)或null

调用此方法三次,每次传递手和三个常量列表中的一个

就这些。 在代码中,它可以给出:

public String findLastMissingElement(String[] constants, List<String> hand){
   String missingElement = null;
   for (String constant : constants){

       if (!hand.contains(constant) && missingElement==null){ 
          missingElement = constant;
       }
       else if (!hand.contains(constant)){ 
         return null;
       }
   }
   return missingElement;
}

现在还不清楚你在找什么。你能提供几个输入和预期输出的例子吗?我不打算做你的家庭作业,有比3个单独的字符串数组更好的方法来存储你的数据点,使你的任务更容易解决。检查大小。。。如果它是大小减一,那么检查哪一个丢失了,否则什么也不做…可能是重复的UserF40这不是我的家庭作业。我甚至不是学生。线程[main]挂起异常UnsupportedOperationException数组$ArrayStabstractList.removeint行:不可用抽象列表$Itr.remove行:不可用数组$ArrayStabstractCollection.removeAllCollection行:不可用main.check行:53 main。行:48 Main.mainString[]行:69使用另一个ArrayList创建:List people=new ArrayListStarlays.asListPEOPLE;
String missingPeople = findLastMissingElement(PEOPLE, hand);
String missingWeapon = findLastMissingElement(WEAPONS, hand);
String missingRoom = findLastMissingElement(ROOMS, hand);