我有一个以.xml或.gxml结尾的文件名数组.我将每个元素放入另一个数组中.我需要确保不添加两次相同的文件名.
所以真正的问题是如何确保相同的元素不会两次添加到数组中?
使用Set而不是数组进行处理,以确保它不会出现两次
Set<String> fileNames = new HashSet<String>();
fileNames.add("1.txt");
fileNames.add("2.txt");
// not necessarily in that order with HashSet
System.out.println(fileNames); //[1.txt,2.txt]
fileNames.add("1.txt");// it will not add this one
System.out.println(fileNames); //[1.txt,2.txt]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
557 次 |
| 最近记录: |