HashSet 在添加到对象后更改其值

HashSet changes its values after adding to object

我正在尝试向 ArrayList 添加构造函数具有 3 个参数(int、int、hashset)的对象。当我添加一个新对象时,哈希集中的值会以某种方式发生变化,因此我添加了错误的值。例如,我创建了 3 个对象,添加了这 3 组整数:

142082 74016 122517 57432 97112  
142082 150224 112373 129671 57432 97112 138427 115659 102283 147774  
142082 31491 129671 855 57432 73545 123160 124682 147774 61966 58590 

但是对象收到了这些:

70213 131022 118104 137949 4003 13798 23598 129525  
70213 131022 118104 137949 4003 13798 23598 129525  
70213 131022 118104 137949 4003 13798 23598 129525 

我不明白为什么集合会改变它的值。这是函数代码:

//Array of ParagData objects
public static ArrayList<ParagData> getAllParagsDatas(String indexed_data_tab) throws SQLException{
    ArrayList<IndexedItem> allIndexedItems = new ArrayList<>();
    ArrayList<ParagData> allParags = new ArrayList<>();
    HashSet<Integer> wordsId = new HashSet<>();
    //Array of all indexed words
    allIndexedItems = GuiFindFrags.myConnection.getAllIndexedItems(indexed_data_tab);

    int curCycledParag = 1; //current paragraph num
    int curCycledTextId = 1; //current text id

    for(int i = 0; i < allIndexedItems.size(); i++){
        if((allIndexedItems.get(i).textId == curCycledTextId) && allIndexedItems.get(i).paragNum == curCycledParag){
            if(allIndexedItems.get(i).wordId != 0) wordsId.add(allIndexedItems.get(i).wordId);
        }
        else {          
            allParags.add(new ParagData(allIndexedItems.get(i).textId, allIndexedItems.get(i).paragNum, wordsId));
            wordsId.clear();
            curCycledParag = allIndexedItems.get(i).paragNum;
            curCycledTextId = allIndexedItems.get(i).textId;
        }
    } 
    return allParags;
}

在这段代码中我:

allIndexedItems = GuiFindFrags.myConnection.getAllIndexedItems(indexed_data_tab);
  1. 获取对象的ArrayList allIndexedItems,描述文本中的单词(文本在mysql table中)。该对象具有以下参数:

    • int text_id(在哪个文本中找到了这个词)
    • int parag_num(在文本的哪一段找到的)
    • int word id -(词典中单词的 id 也在 mysql table 中)

    此对象的构造函数中还有其他参数,但此处未使用。 所以这个 ArrayList 描述了所有文本中的所有单词。

  2. int curCycledParag = 1; //current paragraph num
    int curCycledTextId = 1; //current text id
    

为当前文本和当前段落创建变量。他们会在周期内改变。

  1. 同样在开始时我创建了 HashSet<Integer> wordsId - 在这个 HashSet 中所有的 word_id of one 文本段应该被添加. 所以我开始循环将 word_ids 添加到 HashSet 而每个单词的 text_id (allIndexedItems.get(i).textId) = 当前文本 (变量 curCycledTextId) 和单词段落 ( allIndexedItems.get(i).paragNum) = 变量值 curCycledParag

    所以:当段落中的所有单词都添加到 HashSet 时,我必须创建另一个对象来描述段落 - 对象 ParagData(它有:

    • int text_id(本段所在文本的id)
    • int parag_num(文本中的段落数)
    • HashSet wordIdsSet - (段落的所有单词id)

    并且在开始时我创建了一个 ArrayList<ParagData> allParags 用于添加所有段落对象。函数return 这个arrayList。 因此,当所有段落单词都添加到 HashSet 时,我创建了一个新对象 ParagData 并将其添加到 ArrayList allParags。之后我清理 HashSet 并使用它老化来添加下一段的单词 ID。

    else {              
        allParags.add(new ParagData(allIndexedItems.get(i).textId, allIndexedItems.get(i).paragNum, wordsId));
        wordsId.clear();
        curCycledParag = allIndexedItems.get(i).paragNum;
        curCycledTextId = allIndexedItems.get(i).textId;
    }
    return allParags;
    

当我将新的 ParagData 对象添加到 ArrayList 时,它的值会发生变化 - 因此不会将正确的值添加到 allParags ArrayList

为每个 ParagDat 创建一个新的 Hashset 对象。 Hashset 在创建后不是不可变的。你处理的是同一个对象。