简体   繁体   中英

Only final entry is added to ArrayList

I am busy with a program for my final-year project. The following method needs to return a list of combinations, and a combination is a list of LoadCases (variableLoad and permanentLoad are subclasses of LoadCase):

public ArrayList<ArrayList<LoadCase>> strUfCombinations()
{
    ArrayList<ArrayList<LoadCase>> combinationslist = new ArrayList<ArrayList<LoadCase>>();
    int i = 0;

    for(int x = 0; x < allvariableLoads.size(); x++)
    {
        combinationslist.add(new ArrayList<LoadCase>());
    }

    ListIterator<ArrayList<LoadCase>> cListItr = combinationslist.listIterator();

    while(cListItr.hasNext())
    {           
        cListItr.next();
        ArrayList<LoadCase> combination = new ArrayList<LoadCase>();

        for(int j = 0; j < allvariableLoads.size(); j++)
        {
            allvariableLoads.get(j).setIsLeading(false);
        }
        allvariableLoads.get(i).setIsLeading(true);

        Iterator<VariableLoad> vLoadIterator = allvariableLoads.iterator();
        while(vLoadIterator.hasNext())
        {
            VariableLoad vload = vLoadIterator.next();
            if(vload.getIsLeading()==true)
            {
                vload.finalfactor = vload.partialFactor.strUf;
            }
            if(vload.getIsLeading()== false)
            {
                vload.finalfactor = vload.combinationFactor * vload.partialFactor.strUf;
            }
            combination.add(vload);
        }

        Iterator<PermanentLoad> pLoadIterator = permanentLoads.iterator();
        while(pLoadIterator.hasNext())
        {
            PermanentLoad pload = pLoadIterator.next();
            pload.finalfactor = pload.partialFactor.strUf;
            combination.add(pload);
        }

        Iterator<LoadCase> combItr = combination.iterator();
        while(combItr.hasNext())
        {
            LoadCase test = combItr.next();
            System.out.print(test.finalfactor+test.name+"  ");
        }
        System.out.println();
        cListItr.set(combination);
        i++;
    }
    System.out.println("\n");

    return combinationslist;
}

I am using a test class to run it:

public static void main(String[] args) 
{
    PermanentLoad testload = new PermanentLoad("DL1", "self");
    PermanentLoad testload2 = new PermanentLoad("DL2", "geofac");
    PermanentLoad testload3 = new PermanentLoad("DL3", "fluid");
    PermanentLoad testload4 = new PermanentLoad("DL4", "geounf");

    VariableLoad vload1 = new VariableLoad("VL1", "imposed", "A");
    VariableLoad vload2 = new VariableLoad("VL2", "wind", "normal");
    VariableLoad vload3 = new VariableLoad("VL3", "imposed", "E2");
    VariableLoad vload4 = new VariableLoad("VL4", "thermal", "normal");

    CasesManager manager = new CasesManager();
    manager.addCase("DL1", "self", "null");
    manager.addCase("DL2", "geofac", "null");
    manager.addCase("DL3", "fluid", "null");
    manager.addCase("DL4", "geounf", "null");
    manager.addCase("VL1", "imposed", "A");
    manager.addCase("VL2", "wind", "normal");
    manager.addCase("VL3", "imposed", "E2");
    manager.addCase("VL4", "thermal", "normal");
    manager.addCase("VL5", "cranes", "normal");

    System.out.println("\n");

    CombinationCalculator calc = new CombinationCalculator(manager);
    ArrayList<ArrayList<LoadCase>> combinations =  calc.strUfCombinations();

    Iterator<ArrayList<LoadCase>> cListItr = combinations.iterator();
    while(cListItr.hasNext())
    {
        Iterator<LoadCase> combItr = cListItr.next().iterator();
        while(combItr.hasNext())
        {
            LoadCase test = combItr.next();
            System.out.print(test.finalfactor+test.name+"  ");
        }
        System.out.println();
    }

The output when I run the test class is:

1.6VL1  0.96VL3  0.48VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  
0.48VL1  1.6VL3  0.48VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  
0.48VL1  0.96VL3  1.6VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  
0.48VL1  0.96VL3  0.48VL4  1.3VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  
0.48VL1  0.96VL3  0.48VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  


0.48VL1  0.96VL3  0.48VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  
0.48VL1  0.96VL3  0.48VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  
0.48VL1  0.96VL3  0.48VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  
0.48VL1  0.96VL3  0.48VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4  
0.48VL1  0.96VL3  0.48VL4  0.0VL2  1.6VL5  1.2DL1  0.0DL2  1.2DL3  1.2DL4 

The matrix is printed from within strUfCombinations() and the second matrix is printed from test class by iterating through the final combinationslist. The first matrix is the correct output. It seems to me that the strUfCombinations() method is populating combinationslist with the last combination added.

Can anyone help? Maybe I'm just missing something simple.

Regards, Jacques

The problem was that the LoadCases that I used were being recycled, ie every element in the arraylist was pointing to a single LoadCase which is why all element had the value of the final array.

I solved the problem by writing a static clone() method in class LoadCase that creates a copy of LoadCase . A clone is then created and put into combinations instead of the original LoadCase from allvariableloads and permanentloads .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM