java javaFx ArrayList -error in clear and as returning function

Viewed 41

I have a simple class, that should return some ArrayList of String and Boolean.
The class does not work correct, due to error of the arraylist. In order to find the errors I did some modification and tests. Please, see code and its output below.
The arrayLlist.clear() does not work, and in addition the strings (strx) are added more times than specified. The call to the submethod getTheListsOfFiles(ffx, textArea) causes the undesired population of the ArrayLists dxFilesX and dyFilesY. But why? As seen in the code below the call is a functioni.e. in thef orm of dxFilesX = getTheListsOfFiles(ffx, textArea)

As I am new to this, I would appreciate your response intended to explain this unexpected behavior of this method.

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.control.TextArea;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class GetTheFiles {
    private String cwd;
    private String nwdImg;
    private String nwdFiles;
    private String fileUpperBound;
    private Boolean createResFiles;
    private Boolean createResImg;
    private Boolean readedFiles = true;
    private TextArea textArea;
    FileChooser fileChooser = new FileChooser();
    List<File> listDefinionFiles = new ArrayList<>();
    MessageDisplay message = new MessageDisplay();
    CreateNewDirectory createDir = new CreateNewDirectory();
    readAllFilesToList readfiles = new readAllFilesToList(); //
    // constr
    public void GetTheFiles() { }

    // Th method
    public void getTheFileLists(ArrayList<String> dxFilesList,
                                ArrayList<String> dyFilesList,
                                ArrayList<String> theStringContainerList,
                                ArrayList<Boolean> theBooleanContainerList,
                                TextArea textArea) {
        ArrayList<String> dxFilesX = new ArrayList<>();
        ArrayList<String> dyFilesY = new ArrayList<>();
        this.textArea = textArea;
        listDefinionFiles = fileShooser(textArea);  // return List<File> 
        System.out.println("listDefinionFiles :" + listDefinionFiles);
        splitDefinitionFiles(listDefinionFiles, theStringContainerList, theBooleanContainerList, textArea);
        System.out.println("listDefinionFiles :" + listDefinionFiles);
        System.out.println("theStringContainerList :" + theStringContainerList);
        System.out.println("theBooleanContainerList: " + theBooleanContainerList);
        File ffx = listDefinionFiles.get(0);  //: C:\java\pr\java\\dyfiles.txt
        File ffy = listDefinionFiles.get(1);  // : C:\java\pr\java\pers8_8\dyfiles.txt

        dxFilesX.clear();
        dyFilesY.clear();
        // when disabling these two invoke, the list is cleared and results are correct
        // (1),(2) list contaning name of files as String
        dxFilesX = getTheListsOfFiles(ffx, textArea);
        dyFilesY = getTheListsOfFiles(ffy, textArea);
        System.out.println(" from file dir 1- dxFiles  : " + dxFilesX);
        System.out.println(" from file dir 2- dyFiles  : " + dyFilesY);
        // not cleared as expected when the (1) and (2) are in function
        dxFilesX.clear();
        dyFilesY.clear();
        String strx1 = "AA";
        String strx2 = "BB";
        String strx3 = "CC";
        // the outputs 
        System.out.println(" strx1 strx2  str3  : " + strx1 + " ," + strx2 + " , " + strx3);
        System.out.println(" -clear dxFilesX  : " + dxFilesX);
        System.out.println(" -clear dyFilesY  : " + dyFilesY);
        System.out.println("--");
        dxFilesX.add(strx1);
        System.out.println("strx1 added to dxFilesX 0: " + dxFilesX);
        dxFilesX.add(strx2);
        System.out.println("strx2 added to dxFilesX 1: " + dxFilesX);
        dxFilesX.add(strx3);
        System.out.println("strx3 added to dxFilesX 2: " + dxFilesX);
        System.out.println("--");
        dyFilesY.add(strx1);
        System.out.println("strx1 added to dyFilesY 0:  " + dyFilesY);
        dyFilesY.add(strx2);
        System.out.println("strx2 added to dyFilesY 1:  " + dyFilesY);
        dyFilesY.add(strx3);
        System.out.println("strx3 added to dyFilesY 2:  " + dyFilesY);
        System.out.println(" - ** the final dxFilesX  : " + dxFilesX);
        System.out.println(" - ** the final dyFilesY  : " + dyFilesY);
    }

/*
 * The printing outputs: 
 * from file dir 1- dxFiles  : [acc11.D2s,acc22.D2s]  
 * from file dir 2- dyFiles  : [acc11.D2s, acc22.D2s] 
 * strx1 strx2  str3  : AA ,BB , CC  -clear dxFilesX  : []  -clear
 * dyFilesY  : []
 * -- 
 * strx1 added to dxFilesX 0: [AA] 
 * strx2 added to dxFilesX 1: [AA, BB] 
 * strx3 added to dxFilesX 2: [AA, BB, CC]
 * -- 
 * strx1 added to dyFilesY 0:  [AA, BB, CC, AA] 
 * strx2 added to dyFilesY 1:  [AA, BB, CC, AA, BB] 
 * strx3 added to dyFilesY 2:  [AA, BB, CC, AA, BB, CC]
 * - 1--** the final dxFilesX  : [AA, BB, CC, AA, BB, CC]
 * - 2--** the final dyFilesY  : [AA, BB, CC, AA, BB, CC]
 * However, when commenting the methods denoted (1) and (2) above
 * the outputs are as expected - correct: 
 * from file dir 1- dxFiles  : []
 * from file dir 2- dyFiles  : []  
 * strx1 strx2  str3  : AA ,BB , CC  
 * -clear dxFilesX  : []  
 * -clear dyFilesY  : []
 * --
 * strx1 added to dxFilesX 0: [AA]
 * strx2 added to dxFilesX 1: [AA, BB]
 * strx3 added to dxFilesX 2: [AA, BB, CC]
 * --
 * strx1 added to dyFilesY 0:  [AA]
 * strx2 added to dyFilesY 1:  [AA, BB]
 * strx3 added to dyFilesY 2:  [AA, BB, CC]
 *   - 1-** the final dxFilesX  : [AA, BB, CC]
 *   - 2-** the final dyFilesY  : [AA, BB, CC]
 * The dxFiles.clear() and dyFiles.clear() are also not working as expected in the first case.
 * In addion the strings are added several times, more times than the code shoudl do. Why?. 
 * Somebody that could explain this. I use version 18 of java and javafx.
 */
}
0 Answers
Related