字符串向ArrayList添加超过需要的字符串

时间:2016-06-18 00:30:52

标签: java

我正在尝试将字符串拆分并将其添加到数组列表中。这是代码:

String[] textDelimit = fullNameOfProduct.split(" ");
for(int i = 0; i < textDelimit.length; i++){
    //removes all symbols to check for dictionary match
    textDelimit[i] = textDelimit[i].replaceAll("[^\\p{L}\\p{Nd}]+", "");
    System.out.print(textDelimit[i] + " ~ ");
    //Adds cleaned word to list
    productTitleWords.add(textDelimit[i]);
}
System.out.println();
//Prints all cleaned words
for(int i = 0; i < productTitleWords.size(); i++){
    System.out.print(productTitleWords.get(i) + " ");
}

这是原始字符串

WD Blue 1TB SATA 6 Gb/s 7200 RPM 64MB Cache 3.5 Inch Desktop Hard Drive (WD10EZEX)

以下是为textDelimit打印的内容:

WD ~ Blue ~ 1TB ~ SATA ~ 6 ~ Gbs ~ 7200 ~ RPM ~ 64MB ~ Cache ~ 35 ~ Inch ~ Desktop ~ Hard ~ Drive ~ WD10EZEX ~

以下是打印出的productTitleWords

WD Blue 1TB SATA 6 Gbs 7200 RPM 64MB Cache 35 Inch Desktop Hard Drive WD10EZEX Hard Blue Drive Inch 7200 Blue Hard Blue Hard Drive Cache Drive Inch Blue Inch Blue Hard Hard Blue Drive Inch 7200 Blue Hard Blue Hard Drive Cache Drive Inch Blue Inch Blue Hard

我需要第二个printline语句中的内容在productTitleWords arrayList中,但是当打印出该arrayList时,你得到第三个print语句。请帮忙!

1 个答案:

答案 0 :(得分:1)

在运行方法之前,您是否忘记清除productTitleWords