0

I have suddenly been having a problem in regards to using an iterator while looping where it only goes through about ⅔ (two thirds) of the array before it seemingly just fails to work properly the last 20,000 elements or so after having worked with the same code previously. The relevant code block is as follows:

private void loadInvTable(){
    ArrayList<InventoryData.InventoryObject> invArray = new ArrayList<>();
    int rowCount = dtm.getRowCount();
    try {
        invArray = AllJsonData.allQuantityParserTest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Iterator<InventoryData.InventoryObject> invIt = invArray.iterator();
    for(int i = 0; i < rowCount; i++){
        String upc = dtm.getValueAt(i, dtm.findColumn("UPC")).toString();
        while (invIt.hasNext()){
            InventoryData.InventoryObject invObj = invIt.next();
            String test = invObj.getUpc().toString();
            if(invObj.getUpc().equals(upc)){
                System.out.println(invObj.getUpc());
                dtm.setValueAt(invObj.getNm(), i, dtm.findColumn("NM"));
                dtm.setValueAt(invObj.getLp(), i, dtm.findColumn("LP"));
                dtm.setValueAt(invObj.getMp(), i, dtm.findColumn("MP"));
                dtm.setValueAt(invObj.getHp(), i, dtm.findColumn("HP"));
                dtm.setValueAt(invObj.getDam(), i, dtm.findColumn("DAM"));
                invIt.remove();
                break;
            }
        }
    }
    invArray.clear();
}

The data it is comparing is an array parsed from a file and a JTable each with about 100,000 rows. The problem being is that when I do it in other ways it takes much longer but works properly:

private void loadInvTable() {
    ArrayList<InventoryData.InventoryObject> invArray = new ArrayList<>();

    try {
        invArray = AllJsonData.allQuantityParserTest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    int rowCount = dtm.getRowCount();
    for(InventoryData.InventoryObject inv : invArray){
        for(int i = 0; i < rowCount; i++){
            if(dtm.getValueAt(i, dtm.findColumn("UPC")).toString().equals(inv.getUpc())){
                dtm.setValueAt(inv.getNm(), i, dtm.findColumn("NM"));
                dtm.setValueAt(inv.getLp(), i, dtm.findColumn("LP"));
                dtm.setValueAt(inv.getMp(), i, dtm.findColumn("MP"));
                dtm.setValueAt(inv.getHp(), i, dtm.findColumn("HP"));
                dtm.setValueAt(inv.getDam(), i, dtm.findColumn("DAM"));
                break;
            }
        }
    }
}

private void loadInvTable() {
    ArrayList<InventoryData.InventoryObject> invArray = new ArrayList<>();

    InventoryData.InventoryObject invObj;

    try {
        invArray = AllJsonData.allQuantityParserTest();
        //System.out.println(ints);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Iterator<InventoryData.InventoryObject> invIt = invArray.iterator();
    int count = 0;
    int rowCount = dtm.getRowCount();
    while(invIt.hasNext()){
        invObj = invIt.next();
        for(int i = 0; i < rowCount; i++){
            if(dtm.getValueAt(i, dtm.findColumn("UPC")).toString().equals(invObj.getUpc())){
                dtm.setValueAt(invObj.getNm(), i, dtm.findColumn("NM"));
                dtm.setValueAt(invObj.getLp(), i, dtm.findColumn("LP"));
                dtm.setValueAt(invObj.getMp(), i, dtm.findColumn("MP"));
                dtm.setValueAt(invObj.getHp(), i, dtm.findColumn("HP"));
                dtm.setValueAt(invObj.getDam(), i, dtm.findColumn("DAM"));
                invIt.remove();
                break;
            }
        }
        count++;
    }
}

I tried to both rewrite and debug the problem section of the program, but I can't seem to find a way to code it so that it works nearly as quickly the first block and when debugging the code it seems to either hang on the code block and I need to kill the program or it runs and just skips/repeats one of the the rest of the elements in the array.

I have no idea what exactly I'm doing wrong and am even having trouble pinpointing what could be causing the issue or what had changed to break it and have been trying for days to get it working again. I was hoping for some help if anyone can point where the mistake is. If this question has already been asked elsewhere I apologize. I couldn't find a similar question anywhere. Thanks in advance for any help anyone can provide.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Seems to me that this is a great candidate for a profiler tool. You should try to debug with YourKit or JRebel. Here's a link with good info on profiling tools: https://xperti.io/blogs/best-java-profiler-tools/ – hfontanez Apr 26 '22 at 06:32
  • 1
    `invIt.remove()` You are removing elements from the `ArrayList` while you are iterating it. That's a big no-no. Maybe this will help: [Remove elements from collection while iterating](https://stackoverflow.com/questions/10431981/remove-elements-from-collection-while-iterating) – Abra Apr 26 '22 at 06:37
  • Maybe switch the for-loop with the while-loop. – Ralf Renz Apr 26 '22 at 06:58
  • @user16320675 i apologize, i am unsure if this answers your question. i used the table data to populate the file, then i edit the table and save the changes to the file but don't change the UPC. the table itself is just a defaultTableModel – warpinginbalance Apr 26 '22 at 07:01
  • @user16320675 oh, so i should be placing Iterator invIt = invArray.iterator(); within the first for loop instead? i hadn't realized that is how it needs to be set up, i had assumed it would also reset the removed ones as well – warpinginbalance Apr 26 '22 at 07:09
  • @Abra i apologize if I'm misunderstanding, but invIt is the name of the iterator and the name of the arrayList is invArray. should i be removing elements from the iterator in a different way? – warpinginbalance Apr 26 '22 at 07:13
  • @user16320675 thank you, i will try doing it using a hashMap as well and see if that helps. in regards to the removed ones, i call the .remove in the inner loop if the table data and the string from the file matches after editing the table since each one is unique to try and speed up the search per loop – warpinginbalance Apr 26 '22 at 07:16

0 Answers0