0

I have a 2d array of double. I need to convert it into ArrayList<double []> so each row in the original array can represent an element in the ArrayList.

The problem is when I try to print the ArrayList content it just keeps printing the last row in the array.

How can I solve this problem?

public ArrayList<double []> archiveFitConstract(double [][] fitArchive)
    {
       double [] tempFit=new double [4];
           ArrayList<double []> archiveFitness=new ArrayList<double []>();

          for (int x=0;x<fitArchive.length;x++)
           {
              for(int y =0;y<4;y++)
               {
                 tempFit[y]= fitArchive[x][y];
                 System.out.print(tempFit[y]+"         ");
                 }
              System.out.println();
              archiveFitness.add(tempFit);
           }
          System.out.println();
          System.out.println("intial archive");
          printArchive(archiveFitness);
          return archiveFitness; 
    }

This the 2d array I would like to convert to ArrayList. As you can see later it just keeps printing the last line.

Fitness
   
60.0                219.71666666666667                0.18490938396984746                124.368696169                
61.0                155.76730190571715                0.13125463164401524                93.7106673432257                
62.0                195.78210894552723                0.16487933436614927                112.8983281476222                
63.0                172.07394540942929                0.14536074970791915                101.58210404696774                
65.0                203.10251256281407                0.17129050559257727                116.43772718840202                
66.0                153.63791374122368                0.1296451817308891                92.7160987213641                
67.0                226.977977977978                0.19146426027887184                127.89617891149551                
68.0                165.14566532258064                0.1387625314912389                98.15649544751814                
69.0                181.1142573273721                0.15296231218997905                105.91493516689121                
70.0                203.6655086848635                0.1716285585233586                116.69287491470966                
71.0                151.820806371329                0.12795824700977737                91.82175284299453                
72.0                254.11498257839722                0.21458682827303988                140.93654224981182                
73.0                164.41480377545952                0.13847785040993182                97.85100366623547                
74.0                149.65465465465465                0.12609544650795482                90.77683821827023                
75.0                288.0574541852402                0.24265368482941108                157.1753589652719                
76.0                247.961328705999                0.2089940208629129                137.94852911407042                
77.0                145.296875                0.12225987589989051                88.6607317501875                
78.0                158.8627254509018                0.13376831269214443                95.1824816904569                
79.0                160.6795443288757                0.13551520267957398                96.08478335697477                
80.0                176.28185907046478                0.14868345373249442                103.57125396525937                
81.0                145.4545905707196                0.122585391559813                88.76641840025809                
82.0                200.48725637181408                0.16923434552396546                115.19983416749625                
83.0                177.11773547094188                0.1489717667820992                103.91939441294389                
84.0                147.9602187966186                0.12460673053359415                89.95450722083342                
85.0                246.1454094292804                0.20790815415809186                137.11770216774192                
86.0                158.18762575452718                0.13359432412223482                94.91485849890745                
87.0                213.99343434343433                0.18013274330415796                121.62711220754545                
88.0                149.65589259075088                0.12621611751934492                90.79556545980905                
89.0                219.7357071213641                0.18515866929216437                124.4016367294223                
90.0                162.8432055749129                0.13731868967845387                97.1197535226202                
91.0                223.26102204408818                0.1881499445138285                126.09513421467133                
92.0                268.625                0.2258859896560145                147.81821620650004                
93.0                206.26036981509245                0.17357178775937412                117.91098175005698                
94.0                162.75236671649228                0.1371873166945113                97.06870347565919                
95.0                312.63992075284796                0.26419870501412723                169.02942509182168                
97.0                258.4793635007459                0.21827167555156368                143.03042665575728                
98.0                147.9395740465577                0.12471049003907862                89.96318732004161                
99.0                180.23282828282828                0.15148130611084                105.40034930627272                
        

intial archive
  
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
99.0        180.23282828282828        6.601474635215334        105.40034930627272        
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Diana
  • 3
  • 1
  • 1
    Hint: How many array objects do you create in this code? – Code-Apprentice Apr 18 '23 at 22:39
  • 1
    See https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list – Code-Apprentice Apr 18 '23 at 22:45
  • 1
    Assuming you need a copy of each inner array from the original 2D array and using a stream operation: `return Arrays.stream(fitArchive) .map(inner -> Arrays.copyOf(inner, 4)) .collect(Collectors.toCollection(ArrayList::new));`. If reusing the same inner arrays is OK, just `return new ArrayList<>(Arrays.asList(fitArchive));`. – Ole V.V. Apr 18 '23 at 23:25

0 Answers0