我有两个数组,其中包含格式作为字符串。例如:
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,但显然我在比较条件中犯了错误。请帮助修复错误。
您可以转换为列表并使用该方法
containsAll结果:
我们检查另一个数组的一个元素数组中的完整出现: