dimanche 10 juin 2018

If Statement for nonexistence in ArrayList with Array? (Java)

I have

private void recovery()
{
    System.out.println("Recovery initiated...");
    ArrayList<String> linesFromLogFile = loadLogFile();
    ArrayList<String> linesFromUserFile = loadUserFile();
    for (int i = 0; i < linesFromUserFile.size(); i++)
    {
        String[] line = linesFromUserFile.get(i).split(",");
        String pageidUfile = line[0];
        String lsnUFile = line[1];
        for (int z = 0; z < linesFromLogFile.size(); z++)
        {
            String[] lineLogFile = linesFromLogFile.get(z).split(",");
            if(lineLogFile[2].equals(pageidUfile)&&Integer.parseInt(lineLogFile[0])>Integer.parseInt(lsnUFile)) 
            {
                linesFromUserFile.set(i, pageidUfile+","+lineLogFile[0]+","+lineLogFile[3]); 
            }
        }
    }
    overwriteUserFile(linesFromUserFile);
    System.out.println("Recovery finished...");
}

And I need an If Statement. I want to cover the case that "pageidUFile" doesn't match any "lineLogFile[2]" in "linesFromLogFile".

  • I think about adding a counter for if it doesnt match the current "lineLogFile[2]" and check if the counter is equal to the size of "linesFromUserFile". Because therefore it doesnt match any of them.

But there must be a better way of doing it. Do you have any Ideas? :)

Aucun commentaire:

Enregistrer un commentaire