RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 812200
Accepted
YuriiS
YuriiS
Asked:2020-04-10 21:48:52 +0000 UTC2020-04-10 21:48:52 +0000 UTC 2020-04-10 21:48:52 +0000 UTC

检查一个数组的元素是否与另一个数组的元素匹配

  • 772

我有两个数组,其中包含格式作为字符串。例如:

formatsStorage [png, txt, doc]
formatsFile [jpg, txt, doc]

数组元素可以按任何顺序排列。例子:

formatsStorage [doc, png, txt]
formatsFile [txt, jpg, doc]

我需要创建一个方法来检查 , 中formatsFile的所有格式是否与formatsStorage. true如果数组中的每种格式formatsFile都在数组中formatsStorage,则该方法应返回,false如果数组formatsFile包含不在数组中的格式,则返回formatsStorage。

这是我到目前为止所做的,但它不能正常工作:

public boolean checkFormatAll(List<File> filesFrom, Storage storageTo) throws Exception {
    if (filesFrom == null || storageTo == null)
        throw new Exception("Incoming data contains an error");

    String[] formatsStorage = storageTo.getFormatSupported().split(",");

    Set<String> strings = new HashSet<>();

    for (File file : filesFrom) {
        strings.add(file.getFormat());
    }

    String[] formatsFile = strings.toArray(new String[strings.size()]);

    System.out.println("Formats in Storage " + Arrays.toString(formatsStorage));
    System.out.println("Formats in file " + Arrays.toString(formatsFile));

    boolean format = true;
    int countStorage = 0;
    int countFile = 0;

    /*if (formatsFile.length == 1){
        for (String fileFormat : formatsFile){
            for (String storageFormat : formatsStorage){
                if (fileFormat != null && storageFormat != null && fileFormat.equals(storageFormat.trim())){
                    return true;
                }
                else format = false;
            }
        }
    }*/  //Не знаю, оставлять это в отдельном ифе или всё делать в одном ?

    for (String fileFormat : formatsFile) {
        if (fileFormat != null) {
            for (String storageFormat : formatsStorage) {
                if (storageFormat != null && fileFormat.equals(storageFormat.trim()) && formatsFile.length == 1) {
                    return true;
                } else format = false;

                if (storageFormat != null && !fileFormat.equals(storageFormat.trim()) && formatsFile.length != 1) {
                    continue;
                } else format = false;

                if (storageFormat != null && !fileFormat.equals(storageFormat.trim()) && formatsFile.length - 1 != countFile) {
                    continue;
                }

                if (storageFormat != null && fileFormat.equals(storageFormat.trim())) {
                    format = true;
                    break;
                }

                System.out.println("Format in box storage - " + storageFormat + "; Line array Storage - " + countStorage);
                System.out.println("Format in box file - " + fileFormat + "; Line array File - " + countFile);

                countStorage++;
            }
            countFile++;
        }
    }
    return format;
}

我知道您需要将数组的每个元素与数组formatsFile的元素进行比较formatsStorage。为此,我通过 elements 进行双循环formatsStorage,但显然我在比较条件中犯了错误。请帮助修复错误。

java
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    DaysLikeThis
    2020-04-11T09:16:45Z2020-04-11T09:16:45Z

    您可以转换为列表并使用该方法containsAll

    public static void main(String[] args) {
        String arr1[] = {"doc", "png", "jpg"};
        String arr2[] = {"png", "jpg", "doc"};
        String arr3[] = {"png", "jpg"};
    
        List<String> list1 = Arrays.asList(arr1);
        List<String> list2 = Arrays.asList(arr2);
        List<String> list3 = Arrays.asList(arr3);
    
        System.out.println(list2.containsAll(list1));
        System.out.println(list3.containsAll(list1));
    }
    

    结果:

    true
    false
    
    • 2
  2. user420361
    2021-12-13T21:33:17Z2021-12-13T21:33:17Z

    我们检查另一个数组的一个元素数组中的完整出现:

    public static void main(String[] args) {
        String[] arr1 = {"png", "txt", "doc"};
        String[] arr2 = {"jpg", "txt", "doc"};
        String[] arr3 = {"doc", "txt", "png"};
        String[] arr4 = {"png", "txt"};
    
        System.out.println(containsAll(arr1, arr2)); // false
        System.out.println(containsAll(arr1, arr3)); // true
        System.out.println(containsAll(arr3, arr4)); // true
        System.out.println(containsAll(arr3, arr4, true)); // false
    }
    
    // первый массив включает в себя второй массив,
    // без учета дубликатов и порядка элементов
    public static <T> boolean containsAll(T[] arr1, T[] arr2) {
        return containsAll(arr1, arr2, false);
    }
    
    // первый массив включает в себя второй массив, и, если
    // 'full == true', то первый массив состоит из элементов
    // второго массива, без учета дубликатов и порядка элементов
    public static <T> boolean containsAll(T[] arr1, T[] arr2, boolean full) {
        if (arr1 == null || arr2 == null)
            return false;
    
        // количество различных элементов в первом массиве
        long length1 = Arrays.stream(arr1).distinct().count();
        // количество различных элементов во втором массиве
        long length2 = Arrays.stream(arr2).distinct().count();
    
        if (length1 < length2 || full && length1 != length2)
            return false;
    
        return Stream.of(arr1, arr2)
                // выкидываем повторяющиеся элементы
                // из каждого массива и объединяем массивы
                .flatMap(a -> Arrays.stream(a).distinct())
                // количество различных элементов в общем
                // массиве должно быть равно количеству
                // различных элементов в первом массиве
                .distinct().count() == length1;
    }
    
    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5