简体   繁体   English

如何检查数组是否包含元素?

[英]How to check if an array contains elements?

I'm new to Java.我是 Java 新手。

I have an array that looks like this (Array) .我有一个看起来像这样的数组(Array) (I've used Array.toString to print this). (我使用 Array.toString 来打印这个)。 This is how the array is made: arraymade .这是数组的制作方式: arraymade And this is read and made from a file in this format:这是从以下格式的文件中读取和制作的:

Nilsson;Lars;u;g;vg;vg
Svensson;Lena;vg;g;g;vg
Jonsson;Jonas;g;g;vg;g

I'm trying to make a method that returns either "u", "vg" or "g" depending on what the objects array contains.我正在尝试创建一个方法,根据对象数组包含的内容返回“u”、“vg”或“g”。

@Override
public String getKursbetyg() {



    if (Arrays.asList(uppgiftsbetyg).contains("u")) {
        kursbetyg = "u";
        return kursbetyg;

    } else if (Arrays.asList(uppgiftsbetyg).contains("if the array contains 2 vg strings")) {
        kursbetyg = "vg";
        return kursbetyg;

    } else {
        kursbetyg = "g";
        return kursbetyg;
    }

}

This is the code I have now but it doesn't seem to work as it only returns "g" even though the array contains "u".这是我现在拥有的代码,但它似乎不起作用,因为即使数组包含“u”,它也只返回“g”。

Also as you can see I want the method to only return vg when 2 vg:s are found because you need 2 vg:s to get the grade vg.同样如您所见,我希望该方法仅在找到 2 个 vg:s 时返回 vg,因为您需要 2 个 vg:s 才能获得等级 vg。 So how do I check for double elements here ?那么我如何在这里检查双元素?

First, you should rethink your requirements.首先,您应该重新考虑您的要求。 The presence of one string (like u ) does not preclude the absence of the others (like two vg s or a g ) nor does the absence of u and two vg s prove the presence of a g .一个字符串的存在(如u )并不排除其他字符串的不存在(如两个vg或一个g ),也不排除u和两个vg的存在证明g的存在。 Your posted examples are already inconsistent in this regard.您发布的示例在这方面已经不一致。

Besides that, your method lacks a declaration of the variable kursbetyg and I really hope, you are not modifying a field as a side effect of this “get…” method.除此之外,您的方法缺少变量kursbetyg的声明,我真的希望,您不会将字段修改为“get...”方法的副作用。

You can search for two occurrences straight-forward by searching a sublist excluding the first occurrence:您可以通过搜索排除第一次出现的子列表来直接搜索两次出现:

public String getKursbetyg() {
    List<String> list = Arrays.asList(uppgiftsbetyg);
    if(list.contains("u")) {
        return "u";
    }
    int index=list.indexOf("vg");
    if(index>=0 && list.subList(index+1, list.size()).contains("vg")) {
        return "vg";
    }
    return "g";
}

a solution closer to your original structure, which also might be considered simpler is更接近原始结构的解决方案,也可能被认为更简单

public String getKursbetyg() {
    List<String> list = Arrays.asList(uppgiftsbetyg);
    if(list.contains("u")) {
        return "u";
    } else if(Collections.frequency(list, "vg")>=2) {
        return "vg";
    } else {
        return "g";
    }
}

But it might do more work than necessary by unnecessarily counting occurrences of vg beyond 2 .但是通过不必要地计算vg超过2出现次数,它可能会做更多不必要的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM