简体   繁体   English

从数组字符串中删除重复项

[英]Remove duplicates form arraystring

I have filled in an ArrayList of strings with suppliernumbers. 我用供应商编号填充了一个ArrayList字符串。 This list contains duplicates values so I want to delete them with the HashSet . 此列表包含重复值,因此我想使用HashSet删除它们。

I get following error: Invalid expression as statement On line => Set set = new HashSet(leveranciers); 我收到以下错误:无效的表达式作为语句在线=>设置set = new HashSet(leveranciers); (Set underlined) Any idea why? (带下划线的内容)知道为什么吗?

String[] leveranciers = new String[wdContext.nodeShoppingCart().size()];

for(int i = 0; i<wdContext.nodeShoppingCart().size(); i++){

      String productnumber = wdContext.nodeShoppingCart().getShoppingCartElementAt(i).getMatnr()

      wdThis.wdGetAchatsIndirectController().GetDetails(productnumber, "NL");
      leveranciers[i] = wdContext.currentEt_DetailsElement().getLifnr();
}

 //Remove duplicates from array
        Set<String> set = new HashSet<String>(leveranciers);
        set.toArray(new String[0]);


for(int y = 0; y<set.size();y++){
    PdfPTable table = GetTable(set[y]);
    byte[] pdf = wdThis.wdGetAchatsIndirectController().GetPDFFromFolder("/intranetdocuments/docs/AchatsIndirect", table);
    wdThis.wdGetAchatsIndirectController().PrintPDF(pdf);
}

HashSet doesn't have a constructor which accepts an array. HashSet没有接受数组的构造函数。 Have a look at HashSet documentation. 看看HashSet文档。 http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

You can achieve your goal by using Arrays.asList method like that: 您可以使用Arrays.asList方法来实现您的目标:

final String[] strings = new String[] {"ab", "ba", "ab"};
final Set<String> set = new HashSet<String>(Arrays.asList(strings));

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

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