2015-03-03 60 views
0

我有两个数组列表。在Java中的数组列表中不匹配产品名称

capturedForeginProduct:国外产品名称

capturedLocalProducts:本地产品名称

capturedForeginProduct数组列表包含下列国外产品的名称。

华盛顿苹果饮料

令牌苹果饮料

斯金纳苹果饮料

capturedLocalProducts数组列表包含以下本地产品名称。

SUNFRESH苹果饮料RTS 1L

APPY FIZZ闪亮苹果饮料

APPY苹果饮料250毫升PET瓶

我用下面的代码片段打击capturedForeginProduct匹配的产品名称数组列表到capturedLocalProducts数组列表。

if(capturedForeginProduct.get(i).equals(capturedLocalProducts.get(j))) { 

但它不匹配任何产品。基本上我的最终结果应该如下。

华盛顿苹果饮料

令牌苹果饮料

斯金纳苹果饮料

应该配合,

SUNFRESH苹果饮料RTS 1L

APPY FIZZ苹果汽酒饮料

APPY苹果饮料250毫升PET瓶

为每个产品都包含 “苹果” 的措辞。我不介意它是否包含首都,但如果这个词是可用的,那么它应该匹配。

这是我的代码,为了执行这个特定的任务。

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

    for (int j = 0; j < capturedLocalProducts.size(); j++) { 
     // if(capturedForeginProduct.get(i).contains("Garlic")) { 
     // if (Pattern.compile(Pattern.quote(capturedLocalProducts.get(j)), Pattern.CASE_INSENSITIVE).matcher(capturedForeginProduct.get(i)).find() || capturedLocalProducts.get(j).toLowerCase().contains(capturedForeginProduct.get(i).toLowerCase())) { 

     if (capturedForeginProduct.get(i).equals(capturedLocalProducts.get(j))) { 
      // if(capturedLocalProducts.get(i).equals("SUNFRESH Mango Drink RTS 1L")) { 

      log.debug("Matching Second Chance .. : " + "\t" + capturedForeginProduct.get(i) + "\t" + capturedLocalProducts.get(j)); 
      firstForeignProducts.add(capturedForeginProduct.get(i)); 
      firstLocalProducts.add(capturedLocalProducts.get(j)); 
     } else { 
      log.debug("Un Matching Second Chance .. : " + "\t" + capturedForeginProduct.get(i) + "\t" + capturedLocalProducts.get(j)); 
     } 
    } 
} 

谢谢。

+2

听起来像经典不实现equals()(是和散列码)的方法,但请你可以发布一些代码... – Adam 2015-03-03 07:41:30

+0

当然我会更新问题@亚当 – 2015-03-03 07:42:33

+2

你应该比较不是整个字符串,但部分,因为我认为“华盛顿苹果饮料”和“SUNFRESH苹果饮料RTS 1L”是每一个str ing - 当你想根据内容的一部分比较它们时,'equals'不是正确的方法,因为'String#equals'检查整个字符串是否相等。 – Smutje 2015-03-03 07:43:50

回答

0

您可以检查是否有两句相互字如下:

String sentence1 = "Washington Apple Drink"; 
String sentence2 = "SUNFRESH Apple Drink RTS 1L"; 

List<String> sen1 = Arrays.asList(sentence1.split(" ")); 
List<String> sen2 = Arrays.asList(sentence2.split(" ")); 

for (String s : sen1) { 
    if (sen2.contains(s)) { 
     System.out.println("The word " + s + " appears in both sentences"); 
    } 
} 

输出

The word Apple appears in both sentences 
The word Drink appears in both sentences 
+1

它的工作!高尚的^ _ ^ – 2015-03-03 08:08:21

0

您需要拆分名称并比较每个部分。

String foreign = capturedForeginProduct.get(i); 
String local = capturedLocalProducts.get(j); 

String[] foreignWords = foreign.split(" "); 
String[] localWords = local.split(" "); 

最后迭代两个数组并比较每个单词。

相关问题