Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Return String continuously - Data getting overwritten by ArrayList

Currently creating a tableview using JavaFX and came accross this problem where it would simply append the last element of the array (As all the other elements get overrwritten ..)

public  void companyTable() {
   for(CompanyData s: companydataList()){
       companyDataTableView.getItems().setAll(s);
   }
}

Where companyDataList is:

 private List<CompanyData> companydataList(){
        CompanyData company = new CompanyData("test",9,1);

        for(String i : sim.getCompanyNames()) {
           company.setPFCompanyName(i);
       }

       for(int j : sim.getCompanyValues()) {
           company.setPFShareValues(j);
       }
        List<CompanyData> companydata = new ArrayList<>();
        companydata.add(company);

        return companydata;
    }

The data gets added to this (Setters and getters of Strings)

 private final StringProperty PFCompanyName;
    private final IntegerProperty PFShareValues;

    public CompanyData(String CompanyName, int ShareValue, int ClosingPence) {
        this.PFCompanyName = new SimpleStringProperty(CompanyName);
        this.PFShareValues = new SimpleIntegerProperty(ShareValue);
    }

    public String getPFCompanyName() {
        return PFCompanyName.get();
    }

    public StringProperty PFCompanyNameProperty() {
        return PFCompanyName;
    }

    public void setPFCompanyName(String PFCompanyName) {
        this.PFCompanyName.set(PFCompanyName);
    }

    public int getPFShareValues(int j) {
        return PFShareValues.get();
    }

    public IntegerProperty PFShareValuesProperty() {
        return PFShareValues;
    }

    public void setPFShareValues(int PFShareValues) {
        this.PFShareValues.set(PFShareValues);
    }

Currently the output is:

CompanyName             CompanyValue
Samsung                   1093

But what I desire is:

CompanyName             CompanyValue
Nokia                        3
Apple                        1
HTC                          9
Samsung                     1093
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The method setAll(...) replaces all the elements currently in the list with the ones you provide (it "sets them all"). So each time you iterate through your loop, you replace all the elements with the current one. At the end you will just have one element in the table.

An ObservableList is a subtype of the standard java.util.List, so you can call any of the standard list methods. E.g. you can just add each element instead:

public  void companyTable() {
   for(CompanyData s: companydataList()){
       companyDataTableView.getItems().add(s);
   }
}

Of course, you don't really need to write the loop yourself, you can just add them all:

public  void companyTable() {
    companyDataTableView.getItems().addAll(companydataList());
}

or, if it's what you need, set them all:

public  void companyTable() {
    companyDataTableView.getItems().setAll(companydataList());
}

Furthermore, your companydataList() method only creates one CompanyData instance, and then constantly changes it. Here is your current implementation, with comments explaining what each line you wrote does:

private List<CompanyData> companydataList(){

    // create a single instance:
    CompanyData company = new CompanyData("test",9,1);

    // repeatedly change the name of that instance:
    for(String i : sim.getCompanyNames()) {
       company.setPFCompanyName(i);
   }

   // repeatedly change the value of that instance:
   for(int j : sim.getCompanyValues()) {
       company.setPFShareValues(j);
   }

   // create an empty list:
    List<CompanyData> companydata = new ArrayList<>();

    // add one object to the list
    companydata.add(company);

    // return the list containing the single object:
    return companydata;
}

You need to create a CompanyData instance for each of the name/value pairs, and add each instance to the list. Assuming sim.getCompanyNames() and sim.getCompanyValues() return lists (or arrays; I will assume they are lists) of the same length, you need to do something like

private List<CompanyData> companydataList(){


   List<String> companyNames = sim.getCompanyNames();
   List<Integer> companyValues = sim.getCompanyValues();

   List<CompanyData> companydata = new ArrayList<>();

   for (int i = 0 ; i < companyNames.size(); i++) {
       String name = companyNames.get(i);
       int value = companyValues.get(i);
       CompanyData company = new CompanyData();
       company.setPFCompanyName(name);
       company.setPFShareValues(value);
       companydata.add(company);
    }
    return companydata;
}

Obviously, it would be far more sensible to have sim, which I assume is some kind of data accessor, return a List<CompanyData> directly in the first place, instead of two different lists for the different properties.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!