在 for 循环 java 中创建 table

Creating a table in a for loop java

我有 2 个数组,其值是单词,第一个 table 中的每个单词都与一个文本(字符串)相关联,现在第二个 table 中的每个单词显示了多少次(int ) 在文本 (String) 中重复。预期的 table 应该是这样的:

这是我到目前为止编写的代码:

keyW = txtKeyword.getText();
search = textField.getText();
System.out.println("String for car = " + search);
System.out.println("String keyword = " + keyW);
btnUpload.setEnabled(false);
btnNewButton_1.setEnabled(false);
btnNewButton.setEnabled(false);
txtKeyword.setEnabled(false);
textField.setEditable(false);

//waitLabel.setVisible(true);
int iar = 0;
int item;

Map<String, Integer> dictionary = new HashMap<String, Integer>();

String[] searchArray = search.split(",");
String[] itemsFromArray1 = new String[searchArray.length];
//Keyword1 = ("Searched Key"+ "\r\n\t ");
//listKeys.add(Keyword1);           
for (iar = 0; iar < searchArray.length; iar++) {
    itemsFromArray1[iar] = searchArray[iar].trim();
    Keyword1 = (searchArray[iar]);
    //listKeys.add(Keyword1);
}

String[] items = keyW.split(",");

for (item = 0; item < searchArray.length; item++) {

    WebDriver driver = new HtmlUnitDriver();
    ((HtmlUnitDriver) driver).setJavascriptEnabled(true);

    driver.get("https://en.wikipedia.org/wiki/" + searchArray[item]);
    tstr1 = driver.findElement(By.xpath("//*[@id='content']")).getText();
    driver.quit();

    String[] itemsFromArray = new String[items.length];
    for (int i = 0; i < items.length; i++) {
        itemsFromArray[i] = items[i].trim();

    }
    for (String string : itemsFromArray) {

        int i = countWords(tstr1, string);

        dictionary.put(searchArray[item].concat(string), i);

        System.out.println("ARRAY " + dictionary);

    }
}

private static int countWords(String tstr1, String string) {
    tstr1 = tstr1.toLowerCase();
    string = string.toLowerCase();
    int posCount = 0;
    String positive = string;
    Pattern positivePattern = Pattern.compile(positive);
    Matcher matcher = positivePattern.matcher(tstr1);
    while (matcher.find()) {
        posCount++;
    }
    return posCount;
}

我试图用 Map<String, Integer> dictionary = new HashMap<String, Integer>(); 实现此目的,但结果 (dictionary.put(searchArray[item], i);) 是错误的。谁能告诉我如何解决这个问题。谢谢!

****更新****

现在控制台中的结果是这样的:

ARRAY { boyanimal=4, catfree=18, catanimal=60,  boyfree=2, catgender=0,  boygender=6,  windowfree=5}
ARRAY { boyanimal=4, catfree=18, catanimal=60,  boyfree=2,  windowanimal=4, catgender=0,  boygender=6,  windowfree=5}
ARRAY { boyanimal=4, catfree=18, catanimal=60,  boyfree=2,  windowanimal=4, catgender=0,  boygender=6,  windowgender=0,  windowfree=5}

有重复的值。如何使显示像 table?

试试这个:

Map<String, Integer> tableMap = new HashMap<String, Integer>();

将密钥保留为:

tableMap.put("Word1-Search1",23);

使用此功能,您将始终拥有每个键的唯一组合。

我希望你不想将数据存储在数据结构中?相反,您应该使用二维 String 数组来存储它。

正在回答您的最新更新:

我想你因为这条线得到了多份副本。

dictionary.put(searchArray[item].concat(string), i);

我认为 concat 应用于整行元素。我会用我的调试器来分析这个,看看 searchArray[item] 的值是什么以及 string 的值是什么。