检查数组的第一个元素需要是唯一的
Check first element of the array needs to be unique
我有一个分号分隔的文本文件。这个想法是逐行读取文本文件。每行将被拆分为一个数组元素。
现在我想做一些检查,例如 ID(称为 "Referenz" 的第一个元素)是否唯一,是否全部强制 "fields" 填写等...
我想我必须拿走身份证并将其放入列表中。对于下一行,我必须将 ID 与列表中的 ID 进行比较?
所以问题是正确的方法以及什么/如何实现它。
到目前为止,这是我的代码:
public class Test_Line2Array {
public static void main(String[] args) {
String strLine = "Referenz;field2;field3;field4;field5;field6;field7;Titel;Name1;Name2;Name3;field8;field9;field10;field11;field12;field13;field14;Street;field15;ZIP;field16;city;field17;dob;field18;field19;field20;field21;field22;field23;field24;field25;field26;field27;field28;field29;field30;field31;field32;field33;field34;field35;field36;field37;field38;field39;phone;mobile;CustomField1;CustomField2;CustomField3;CustomField4;CustomField5;CustomField6;CustomField7;CustomField8;CustomField9;CustomField10";
//declaration
String[] stringArray;
String delimiter = ";";
// allocates memory for 59 strings
stringArray = new String[59];
// split the String after separator ";"
stringArray = strLine.split(";", -1);
// print array
for(int j = 0; j < stringArray.length; j++) {
System.out.println(j + " " + stringArray[j]);
}
}
要检查第一个元素是否唯一,可以使用以下方法:
Collections.frequency(Arrays.asList(stringArray), stringArray[0]) == 1
如果 stringArray
的第一个元素是唯一的,则此 returns a boolean
即 true
,否则 false
.
我建议您使用分隔符 ;
拆分字符串并将分隔的字符串添加到 List
,您可以在其中使用 Collections.frequency()
静态方法轻松验证返回数字作为 int
的发生。
String[] values = strLine.split(";");
List<String> list = Arrays.asList(values);
if (Collections.frequency(list, list.get(0) > 1) {
System.out.println("The first value is not unique in the list");
}
从 Java 8 开始可以随意使用 Stream:
if (list.stream().filter(a -> a.equals(list.get(0))).count() > 1) {
System.out.println("The first value is not unique in the list");
}
对于每一行,将其 Referenz
放在 HashSet
中。然后检查后续 Referenz
是否唯一就像 referenzSet.contains(theNewReferenz)
一样简单
// allocates memory for 59 strings
stringArray = new String[59];
// split the String after separator ";"
stringArray = strLine.split(";", -1);
初始化字符串[59] 对您没有帮助; split 方法只是返回一些东西,然后立即覆盖它。
如果您需要检查任何重复项,使用 HashSet 会有所帮助。
如果您只需要确保 first 元素不重复,您可以循环执行。你已经有了一个,所以...
// print array
for(int j = 0; j < stringArray.length; j++) {
if (stringArray[0].equals(stringArray(j)) {
System.out.println("Duplicate!");
}
System.out.println(j + " " + stringArray[j]);
}
}
我有一个分号分隔的文本文件。这个想法是逐行读取文本文件。每行将被拆分为一个数组元素。
现在我想做一些检查,例如 ID(称为 "Referenz" 的第一个元素)是否唯一,是否全部强制 "fields" 填写等...
我想我必须拿走身份证并将其放入列表中。对于下一行,我必须将 ID 与列表中的 ID 进行比较?
所以问题是正确的方法以及什么/如何实现它。
到目前为止,这是我的代码:
public class Test_Line2Array {
public static void main(String[] args) {
String strLine = "Referenz;field2;field3;field4;field5;field6;field7;Titel;Name1;Name2;Name3;field8;field9;field10;field11;field12;field13;field14;Street;field15;ZIP;field16;city;field17;dob;field18;field19;field20;field21;field22;field23;field24;field25;field26;field27;field28;field29;field30;field31;field32;field33;field34;field35;field36;field37;field38;field39;phone;mobile;CustomField1;CustomField2;CustomField3;CustomField4;CustomField5;CustomField6;CustomField7;CustomField8;CustomField9;CustomField10";
//declaration
String[] stringArray;
String delimiter = ";";
// allocates memory for 59 strings
stringArray = new String[59];
// split the String after separator ";"
stringArray = strLine.split(";", -1);
// print array
for(int j = 0; j < stringArray.length; j++) {
System.out.println(j + " " + stringArray[j]);
}
}
要检查第一个元素是否唯一,可以使用以下方法:
Collections.frequency(Arrays.asList(stringArray), stringArray[0]) == 1
如果 stringArray
的第一个元素是唯一的,则此 returns a boolean
即 true
,否则 false
.
我建议您使用分隔符 ;
拆分字符串并将分隔的字符串添加到 List
,您可以在其中使用 Collections.frequency()
静态方法轻松验证返回数字作为 int
的发生。
String[] values = strLine.split(";");
List<String> list = Arrays.asList(values);
if (Collections.frequency(list, list.get(0) > 1) {
System.out.println("The first value is not unique in the list");
}
从 Java 8 开始可以随意使用 Stream:
if (list.stream().filter(a -> a.equals(list.get(0))).count() > 1) {
System.out.println("The first value is not unique in the list");
}
对于每一行,将其 Referenz
放在 HashSet
中。然后检查后续 Referenz
是否唯一就像 referenzSet.contains(theNewReferenz)
// allocates memory for 59 strings
stringArray = new String[59];
// split the String after separator ";"
stringArray = strLine.split(";", -1);
初始化字符串[59] 对您没有帮助; split 方法只是返回一些东西,然后立即覆盖它。
如果您需要检查任何重复项,使用 HashSet 会有所帮助。
如果您只需要确保 first 元素不重复,您可以循环执行。你已经有了一个,所以...
// print array
for(int j = 0; j < stringArray.length; j++) {
if (stringArray[0].equals(stringArray(j)) {
System.out.println("Duplicate!");
}
System.out.println(j + " " + stringArray[j]);
}
}